Commands covered: pwd, ls, cd, mv, cp, rm, chmod
Like most operating systems, Unix collects files in directories which are organized into a hierarchical directory tree. The figure at right illustrates a prototype directory tree: dick and jane are subdirectories of home, and spot is a subdirectory of jane.
Unix filenames are case-sensitive, so dog.jpg is different from Dog.jpg. Extensions are just a parts of filenames as far as Unix is concerned, so dog.jpg is not guaranteed to contain a jpeg image, while dog (without any extenstion), dog.jpg.txt (with several extensions), and dob.jpeg-image (with a non-standard extension) are perfectly legal. Of course, you'd be crazy to misuse this freedom to confuse yourself.
The following text explains how to use shell commands to navigate and manipulate files.
The pwd command returns the current directory.
# pwd /home/jane
Slashes (/) are used to separate directory names in Unix; therefore it is a bad idea to use them in filenames.
ls command to list the contents of a directory.
# ls dog.htm dog.jpg spot
The list is alphabetical. Subdirectories appear as well as files; to ease the speration of files and subdirectories, many systems are configured to display subdirectory names in a different color.
ls command accepts as an argument the pattern of filenames to search for, using * as a wildcard.
# ls *.htm dog.htm
.) are called hidden, because they are not displayed by the ls command. To display hidden files, use the -a option.
# ls -a . .login dog.jpg .. dog.htm spot
Often configuration files are hidden, so that you needn't see them every time you list a directory's contents.
-l to get the the long form of ls output.
# ls -l -rw-r--r-- 1 jane users 5215 May 1 13:15 dog.htm -rw-r----- 1 jane users 23955 Feb 14 10:12 dog.jpg drwxr-x--- 2 jane users 4096 Dec 25 2000 spot
The long form of ls output contains the file permissions, followed by the owning user and group, the filesize in bytes, the time the file was last modified, and finally the filename.
r), write (w), and execute (x) permissions for the owning user, owning group, and the "world" (i.e. all others). In the example shown above, the user jane can read and write the files dog.htm and dog.jpg; all members of the group users can read dog.htm and dog.jpg, but cannot write to them (i.e. change their contents). Others can read dog.htm but not dog.jpg.To list a directory's contents, a user must have execute permission as well as read permission. If a user has write permission to a directory, she can replace a file in that directory by deleting it and creating a new file with the same name, even if she did not have write permission for the original file.For information on changing permissions, see the chmod command below.
cd command to change the current direcory.
# pwd /home/jane # cd spot # pwd /home/jane/spot
By beginning the directory specification with a slash (/), you can indicate the absolute path to a directory instead of the path relative to the current directory.
# pwd # /home/jane/spot # cd /home/dick # pwd /home/dick
You can also use the directory specifiers . and .. to navigate the directory tree.
# pwd # /home/dick # cd ../jane/spot # pwd /home/jane/spot
mv command to specify the source and target.
# ls dog.htm dog.jpg spot # mv dog.* spot/ # ls spot # ls spot dog.htm dog.jpg
Note the use of a wildcard (*) to specify multiple sources.
# mv spot /home/dick/
will move directory spot into directory /home/dick/ (assuming you have sufficient permissions).
mv to "move" it within its own directory.
# ls *.jpg dog.jpg # mv dog.jpg my_dog.jpg # ls *.jpg my_dog.jpg
Of couse, you can combine a move between directories with a rename.
# mv dog.jpg /home/jane/spot/myDog.jpg
To modify the contents of a file while preserving a copy of the original, you may want to copy a file. To do so, use the cp command.
# ls *.htm dog.htm # cp dog.htm new_dog.htm # ls *.htm dog.htm new_dog.htm # cp dog* spot/
rm command.
# ls dog* dog.htm dog.jpg # rm dog.htm # ls dog* dog.jpg
rm naively to a directory, Unix will not co-operate.
# rm spot rm: `spot' is a directory
Instead, use rm with the -r option to delete directories and their contents recursively.
# rm -r spot
You can also use the rmdir command, but only if the directory is empty.
ls command above. Use the chmod command to change permissions. For example, to remove all group and world permissions from a file,
# ls -l dog.htm -rw-r--r-- 1 jane users 5215 May 1 13:15 dog.htm # chmod go-rwx dog.htm # ls -l dog.htm -rw------- 1 jane users 5215 May 1 13:15 dog.htm
The relevant syntax follows. First, specify the relevant parties
u | owning user |
g | owning group |
o | other (world) |
a | all |
Next, specify whether permissions are to be granted or revoked ||
+ | grant permissions | |
- | revoke permissions | Finally, specify the permissions in question. |
Use the chmod's -R option. For example, to add group read permissions to spot and all its contents,
# chmod -R g+rX spot
adds group read permissions to spot and all its contents. The capital X indicates that execute (x) permission should be added only when a file is a directory.