Managing Directories: mkdir, rmdir, ls, cd, and pwd

 < Day Day Up > 



You can create and remove your own directories, as well as change your working directory, with the mkdir, rmdir, and cd commands. Each of these commands can take as its argument the pathname for a directory. The pwd command displays the absolute pathname of your working directory. In addition to these commands, the special characters represented by a single dot, a double dot, and a tilde can be used to reference the working directory, the parent of the working directory, and the home directory, respectively. Taken together, these commands enable you to manage your directories. You can create nested directories, move from one directory to another, and use pathnames to reference any of your directories. Those commands commonly used to manage directories are listed in Table 10-3.

Table 10-3: Directory Commands

Command

Execution

mkdir directory

Creates a directory.

rmdir directory

Erases a directory.

ls -F

Lists directory name with a preceding slash.

ls -R

Lists working directory as well as all subdirectories.

cd directory name

Changes to the specified directory, making it the working directory. cd without a directory name changes back to the home directory:
$ cd reports

pwd

Displays the pathname of the working directory.

directory name/filename

A slash is used in pathnames to separate each directory name. In the case of pathnames for files, a slash separates the preceding directory names from the filename.

..

References the parent directory. You can use it as an argument or as part of a pathname:
$ cd ..
$ mv ../larisa oldletters

.

References the working directory. You can use it as an argument or as part of a pathname:
$ ls .

~/pathname

The tilde is a special character that represents the pathname for the home directory. It is useful when you need to use an absolute pathname for a file or directory:
$ cp monday ~/today

Creating and Deleting Directories

You create and remove directories with the mkdir and rmdir commands. In either case, you can also use pathnames for the directories. In the next example, the user creates the directory reports. Then the user creates the directory letters using a pathname:

$ mkdir reports $ mkdir /home/chris/letters 

You can remove a directory with the rmdir command followed by the directory name. In the next example, the user removes the directory reports with the rmdir command:

$ rmdir reports 

To remove a directory and all its subdirectories, you use the rm command with the -r option. This is a very powerful command and could easily be used to erase all your files. You will be prompted for each file. To simply remove all files and subdirectories without prompts, add the -f option. The following example deletes the reports directory an all its subdirectories:

rm -rf reports

Displaying Directory Contents

You have seen how to use the ls command to list the files and directories within your working directory. To distinguish between file and directory names, however, you need to use the ls command with the -F option. A slash is then placed after each directory name in the list.

$ ls weather reports letters $ ls -F weather reports/ letters/

The ls command also takes as an argument any directory name or directory pathname. This enables you to list the files in any directory without first having to change to that directory. In the next example, the ls command takes as its argument the name of a directory, reports. Then the ls command is executed again, only this time the absolute pathname of reports is used.

$ ls reports monday tuesday $ ls /home/chris/reports monday tuesday $

Moving Through Directories

The cd command takes as its argument the name of the directory to which you want to change. The name of the directory can be the name of a subdirectory in your working directory or the full pathname of any directory on the system. If you want to change back to your home directory, you only need to enter the cd command by itself, without a filename argument.

$ cd props $ pwd /home/dylan/props

Referencing the Parent Directory

A directory always has a parent (except, of course, for the root). For example, in the preceding listing, the parent for thankyou is the letters directory. When a directory is created, two entries are made: one represented with a dot (.), and the other represented by double dots (..). The dot represents the pathnames of the directory, and the double dots represent the pathname of its parent directory. Double dots, used as an argument in a command, reference a parent directory. The single dot references the directory itself.

You can use the single dot to reference your working directory, instead of using its pathname. For example, to copy a file to the working directory retaining the same name, the dot can be used in place of the working directory's pathname. In this sense, the dot is another name for the working directory. In the next example, the user copies the weather file from the chris directory to the reports directory. The reports directory is the working directory and can be represented with the single dot.

$ cd reports $ cp /home/chris/weather . 

The .. symbol is often used to reference files in the parent directory. In the next example, the cat command displays the weather file in the parent directory. The pathname for the file is the .. symbol followed by a slash and the filename.

$ cat ../weather raining and warm 
Tip 

You can use the cd command with the .. symbol to step back through successive parent directories of the directory tree from a lower directory.



 < Day Day Up > 



Red Hat(c) The Complete Reference
Red Hat Enterprise Linux & Fedora Edition (DVD): The Complete Reference
ISBN: 0072230754
EAN: 2147483647
Year: 2004
Pages: 328

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net