This article presents information about the following commands:
|cd||move into another directory|
|pwd||find out which directory you are in|
|ls -l||list the contents of a directory|
|mkdir||create a subdirectory|
|cp||make a duplicate copy of a file|
|mv||move a file to another directory|
|rmdir||remove a directory|
|pushd||change directory, remembering last directory|
|popd||companion to pushd|
A directory is a container for files and other directories. Directories are useful when you have lots of files. You can put related files into separate directories just as you would put related papers into separate folders in a filing cabinet. You can create directories within directories to subdivide large sets of files as much as necessary. When you create a directory inside another, it is called a subdirectory.
If the directory that you want to move into is a subdirectory of the one that you are in at the moment, then you can get into it like this:
You'll notice that the response to pwd changes as you move from one directory to another. You can always get back to your home directory by issuing the cd command without a directory name. By itself, the command
takes you back to your home directory. You can also specify the directory that you want to move into by a full pathname (as below).
In the typical situation where you create a subdirectory beneath your home directory, and you move into it with something like "cd letters", you can then create files and operate on them just as you would in your home directory. Notice that when you issue the command "ls" in a subdirectory, only the items contained in that directory are listed.
When you login you are placed in a directory called your home directory. Your home directory belongs to you. You are free to create files, rename them or remove them from that directory. If you login and then type the command pwd, the system will identify the location of your home directory. For example,
% pwd /u1/ee251f/ee251fan %
Pwd stands for "print working directory".
In a sense the response from the pwd command is your current address. It defines your location in a large tree-like arrangement of directories which constitutes the UNIX filesystem. The address is interpreted like this: the leading slash (/) means start at the root or trunk, from there go to the "u1" directory (a main branch), within that find subdirectory "ee251f", within that find "ee251fan" which in this example is the login home directory for user ee251fan. Whenever the name and location of a directory or file is specified with a leading slash, the specification is interpreted relative to the root of the filesystem and it is called a "full pathname".
The ls command is used to list the contents of a directory. If you use the -l option with ls, the system will display "long" information about each item. Try listing the contents of the root directory with this:
% ls -l /
Many of the items in the root directory are themselves directories and this is indicated by the letter "d" in the first column.
drwxr-xr-x 3 root 1024 Sep 10 03:21 bin drwxr-xr-x 2 root 3072 Aug 20 03:34 dev drwxr-xr-x 9 root 512 Sep 16 08:55 u1 drwxr-xr-x 9 root 512 Sep 16 08:57 u2
You can create a new directory with the mkdir command.
Let's say you want to move a file "reservation" from your home directory into a subdirectory "letters". The safest way to proceed until you are really comfortable with directories is to use the cp command to create a duplicate of the file, verify that it made it to the destination, then go back and remove it from the original location.
% mkdir letters % cp reservation letters/reservation % cd letters % ls see the name of the file % vi reservation examine the content of the file % cd go back to your home directory % pwd get your bearings % ls % rm reservation
The following variations on the cp command are possible. In the process of creating the duplicate you can give it a different name.
cp reservation letters/resv.hotel
If you only specify the destination directory, the system assumes that you want the duplicate file to have the same name as the original. This shortcut saves you the trouble of spelling it out.
cp reservation letters
CAUTION: if you use the shortcut shown above, make sure that you have created the "letters" directory. Otherwise the system will simply create a file by that name.
In cases where the destination directory is not an immediate subdirectory there are often ways to specify the location concisely relative to where you are. In any case you can always specify the destination with a full pathname. And the full pathname can be determined by getting into the destination directory and issuing the pwd command.
cp reservation /u1/ee251f/ee251fanThe mv command may be used to move a file to another directory.
mv reservation letters
There is a little more risk involved when you use mv instead of cp because unlike cp, mv does not leave original file in place.
WARNING: If you use mv to move several files to a directory, be very careful that the directory exists and that you are specifying it correctly. If the directory does not exist, it is quite likely that the system will treat each mv command as a file rename. Each successive move will wipe out the the previous content of the destination file and all files but the last will be lost.
The system will not allow you to remove a directory unless it is empty. First remove all files and subdirectories, then issue the rmdir command.
If the system complains that a directory is not empty, but you think it is, there may be hidden files. It is a UNIX convention that files which have names that begin with a period (.) are not displayed by ls unless you use the -a option. The command
looks for hidden files.
Ignore the directory entries "." and "..". They are not obstacles to rmdir.
The pushd command changes your working directory, just as cd does. However, pushd keeps track of the directories you have visited, creating a list (called a stack) of directories. Pushd is very useful when your work requires that you switch back and forth between two directories. When you use pushd with no arguments, instead of changing your working directory to your home directory, it will exchange the top two directories on the stack, and change you working directory. Each time you use pushd, it will show you the current state of the stack. The directory on top of the stack is your current directory. To remove a directory from the stack, use thepopd
command. The top directory on the stack will be removed and forgotten. Here is an example session which uses pushd and popd to maneuver the directory tree.
% pwd /a/ieng9/u/disk10/oce01/efudd home directory = ~ % pushd /tmp change to /tmp /tmp ~ home directory is on the stack % mkdir myfiles % cd myfiles cd changes the directory % pwd on the top of the stack but /tmp/myfiles does not add to the stack % pushd ~ /tmp/myfiles % pushd /tmp/myfiles ~ % cp ~/myfile.c . % pwd /tmp/myfiles % ls myfile.c % pushd ~ /tmp/myfiles % popd remove ~ from the stack /tmp/myfiles only one directory on the stack % pushd /u/disk11/am163f/efudd add a new dir. to the stack /u/disk11/am163f/efudd /tmp/myfiles % pushd /tmp/myfiles /u/disk11/am163f/efudd
There are other options to pushd, and other ways to use it. For more information, read the section on pushd in the man page for csh (type "man csh").