Basic operations on directories are creating new directories, deleting directories, and moving from one directory to another in a directory hierarchy. Commands used for these operations are presented in this section. As far as names of directories are concerned , rules that apply to ordinary files also apply here.
A directory can be considered a special type of file used as a folder to contain other files and directories. Directories are used to organize files in a more logical and manageable way. A directory can be created with the mkdir command.
$ mkdir newdir $
After creating a directory, verify its existence with the ls or ll command. Note that when we use the ll command for the long listing, the first character in the file permissions is " d " instead of " - ", showing that it is a directory, not an ordinary file.
$ ll total 3 -rw-rw-rw- 1 boota users 0 Aug 30 20:47 myf drwxrwxrwx 1 boota users 96 Aug 27 19:04 newdir -rw-rw-rw- 1 boota users 72 Aug 30 20:47 newfile $
Using the ls command without the -l option shows all names of files and directories, and you are not able to distinguish between them. If you don't want to display the long listing and still need to distinguish between files and directories, you can use the lsf or ls -F command. These are equivalent commands, and the screen output just appends a " / " symbol at the end of the directory name .
$ lsf mydir/ newfile testfile.zip $
Here you can see that mydir is a directory, whereas the other two are ordinary files.
Directories are deleted with rmdir command. This command deletes only empty directories. If the directory contains another file or directory, first that file or directory must be deleted. In case a user needs to delete a directory that is not empty, it is possible to use rm -rf command, which can delete a nonempty directory.
Warning
Be careful in using rm -rf , as it removes the entire directory tree without any warning to the user.
The UNIX file system is composed of directories and files. The top-level directory is called the root directory and is represented by "/" symbol. All other directories and files may be considered inside the root directory. A directory one level above is called a parent directory , while a directory one level below is called a child directory. For example, the root directory "/" is the parent directory for home directory, and boota is a child directory of home directory (see sample directory tree in Figure 2-2).
Parent and child directories are just relative to each other. For example, home directory is a child directory of the root directory but it is a parent directory for the boota directory.
The directory names are referenced relative to the root directory. A complete reference name to a directory is called a path name . For example, the path name of the home directory is /home . Similarly, the path name of directory boota is /home/boota . It is easy to judge from the path name that boota is a child directory of home , which in turn is a child directory of the root directory. Files also have path names similar to directories. For example, a complete path name for a file created in directory /home/boota with name myfile is /home/boota/myfile . A path name that starts with the "/" symbol is called the absolute path name. We can also use relative path names, which start from the current directory. For example, to refer to a file with the name alpha in the parent directory of the current directory, we may use a path name ../alpha .
Whenever a new directory is created, two entries are created in the new directory automatically. These are " . " and " .. " where " . " is a reference to the current directory and " .. " is a reference to the parent directory of the current directory.
You used the pwd command in Chapter 1. This command was used to check the current directory. The cd (change directory) command is used to move to some other directory in the directory tree. This command, like other UNIX commands, can be used both with absolute and relative path names. You already know that a user automatically goes to the home directory just after the login process. We again consider the example of user boota who has just logged in and is in home directory, /home/boota . To confirm that she is indeed in her home directory and then move to the /etc directory, the user issues the following commands.
$ pwd /home/boota $ cd /etc $ $ pwd /etc $
The last pwd command showed that the user has moved to the destination directory /etc . In this example, we used an absolute path. In an example of using a relative path, consider the user boota is in her home directory /home/boota and wants to move to the /home (the parent) directory. She can use the cd .. or cd /home command, and either will have the same effect. In cd .. , she asked the shell to move to the parent directory of the current directory. What if you use cd ../.. ?
Study BreakBasic Operations on Files and Directories
|
Top |