CHAPTER 4
In addition to referencing directories by their full or absolute paths, you can reference directories by their relative paths. An absolute path starts with a forward slash. An example of a full path is /home/jason/Music. A relative path does not start with a forward slash. When using relative paths, the paths are relative to the current working directory. To change into the Music directory from /home/jason, you would simply type cd Music.
$ cd /home $ pwd /home $ cd jason/Music $ pwd /home/jason/Music $ cd JohnColtrane $ pwd /home/jason/Music/JohnColtrane |
Linux uses a . to represent the current directory and .. to represent the parent directory. Also, directories end in a trailing forward slash, but this is often assumed. The following commands place you in the same directory.
$ pwd /home/jason $ cd .. $ pwd /home $ cd /home $ pwd /home $ cd /home/ $ pwd /home |
To quickly return to your previous working directory, use the cd - command.
$ cd /var/log $ pwd /var/log $ cd /etc/init.d $ pwd /etc/init.d $ cd - /var/log $ pwd /var/log $ |
To create a directory, use the mkdir command. Directories can be deleted with the rmdir and rm commands.
mkdir [-p] directory Create a directory. When used with the -p (parents) option, intermediate directories are created.
rmdir [-p] directory Remove a directory. When used with the -p (parents) option, all the specified directories in the path are removed. The rmdir command only removes empty directories. To remove directories and their contents, use rm.
rm -rf directory The rm command removes files, directories, or both. To have rm recursively remove a directory and all of its contents, use the -r (recursive) and -f (force) options. Multiple options can be combined by using a dash followed by all the options without a space. Order does not matter. The commands rm -r -f dir, rm -rf dir, and rm -fr dir are all equivalent.
Use the rm command with caution, especially rm -rf. The command line doesn't have a trash container where you can restore accidentally deleted files. When you delete something at the command line it is gone. The following demonstrates the use of mkdir, rmdir, and rm.
$ mkdir newdir $ mkdir newdir/one/two mkdir: cannot create directory ‘newdir/one/two’: No such file or directory $ mkdir -p newdir/one/two $ rmdir newdir rmdir: directory "newdir": Directory not empty $ rm -rf newdir $ ls newdir ls: newdir: No such file or directory $ mkdir newerdir $ rmdir newerdir $ ls newerdir ls: cannot access newerdir: No such file or directory $ |