UNIX Tutorial 4. Managing files  

Commands covered in this section:   mkdir, rmdir, cp, mv, rm,chmod

Now that you have a basic understanding of the filesystem and how to navigate it, you can begin creating and managing your own files and directories. Let's begin with directories. You create directories with mkdir and remove them with rmdir.

Exercise 4.1

Create two new directories with the mkdir command (use the ls command to confirm the new directories were created).
          % mkdir test test2 
Now remove the second directory you just created (and list the files in the current directory again to see if the second directory was removed).
          % rmdir test2 
rmdir will only remove empty directories. If a directory contains files, you must remove them first before using rmdir. Alternatively, you can recursively delete the directory and its contents using the appropriate option with the rm command as discussed later in this section.

Let's move on to working with files. First, you'll need to copy some existing files from elsewhere in the filesystem into your test directory. You use the cp command to do this.

Exercise 4.2

Before going on, move into the new "test" directory you just created. You will use this area to experiment (thus minimizing any potential damage to other files in your home directory!).
          % cd test
          % pwd
          /home/natasha/test 
Now you can copy some files into your test directory with cp.
          % cp /etc/motd .
          % cp /etc/hosts .
          % cp /etc/group .
          % ls
          group   hosts   motd 
The three cp commands above copy existing files from the "/etc" directory into the current directory (repesented by ".").

Now remove (delete) a file using the rm command.

% rm group
          % ls
          hosts   motd 
Finally, move (rename) one of the files using mv.
          % mv motd message-of-the-day
          % ls
          hosts   message-of-the-day 
Since cp, mv, and rm have the potential to destroy data, they offer an interactive option which prompts you before proceeding. This is invoked with the "-i" option. Your system administrator may have already configured your account to use this option by default.

Both cp and mv require two arguments: the existing location and the destination of the file to be copied or moved. The destination can be another file or a directory. Let's compare the results in each case.

Exercise 4.3

First, let's make a new directory within the test directory.
          % mkdir junk
          % ls
          hosts     junk/     message-of-the-day 
Then try the following command:
          % cp hosts hosts2
          % ls
          hosts           junk/
          hosts2          message-of-the-day 
The result in this case is a second copy of the file (called "hosts2") in the current directory. Now try the following:
          % cp hosts junk
          % ls
          hosts           junk/
          hosts2          message-of-the-day
          % ls junk
          hosts 
This time, the file was copied to the "junk" directory, keeping the same name as the original ("hosts").
The shell allows the use of wildcards to reference more than one file at a time. The "?" character stands for a single character with any value and the "*" character stands for any number of characters with any value. For example, "host*" would match "hosts" and "hosts2" in the above example.

Exercise 4.4

Make yet another directory.
          % mkdir stuff
          % ls
          hosts                junk/                stuff/
          hosts2               message-of-the-day 
Now repeat the cp command from the last exercise using a wildcard.
          % cp hosts* stuff
          % ls stuff
          hosts     hosts2 
Both "hosts" and "hosts2" were copied this time.
You can recursively copy a directory and its contents to another location. It is also possible to delete a directory and its contents. These can be accomplished by using the "-r" option with cp and rm, respectively.

Exercise 4.5

Copy the "junk" directory and its contents to a new location.
          % cp -r junk more-junk
          % ls
          hosts                junk/                more-junk/
          hosts2               message-of-the-day   stuff/
          % ls more-junk
          hosts 
The result in this case is a new directory ("more-junk") whose contents are the same as the "junk" directory. Next, remove the "more-junk" directory and its contents.
          % rm -r more-junk
          % ls
          hosts                junk/               stuff/
          hosts2               message-of-the-day 
Be careful with rm -r; using this command in the wrong place (e.g., your home directory) can have disastrous consquences!

UNIX provides permissions (also known as modes) to restrict access to your files and directories. These are manipulated with the chmod ("change modes") command. Recall that ls -l shows a long listing of files and directories. The first column in this long listing is a 10-character string indicating the type of file (regular file, directory, link, etc.) and the access permissions for the file's owner, group, and other users. This is broken down in Table 4.1.

Table 4.1

File Type
(character 1)
Owner Access
(characters 2-4)
Group Access
(characters 5-7)
Other Access
(character 8-10)
- = regular file
d = directory
r = read
w = write
x = execute
r = read
w = write
x = execute
r = read
w = write
x = execute
The first character identifies the file type. The next three characters indicate access rights for the file's owner, the following three show access rights for the group, and the final three show access rights for all other users. A hyphen in a given access right's position means that right is denied. The following example shows a directory for which the owner has read/write/execute access, the group has read/execute access, and all others have no access:
        drwxr-x--- 

The chmod command uses a special argument (composed of three parts as summarized in Table 4.2) to assign the desired rights to files or directories.

Table 4.2

Entity Operator Access Right
u = user (owner)
g = group
o = others
a = all of the above
+ = grant
- = revoke
= = set
r = read
w = write
x = execute
- = no access
By putting together the entity, operator, and access rights, you can construct an argument for chmod which will assign the desired rights. In the following example, the user will have read/write access, the group will have read access, and others will have no access:
        u=rw-,g=r--,o=--- 
Exercise 4.6
Add all rights for owner and revoke all rights for group and others on the "hosts" file.
          % ls -l hosts
          -rw-r--r--  1 natasha  staff   983 Apr 12 01:08 hosts
          % chmod u+rwx,g-rxw,o-rwx hosts
          % ls -l
          -rwx------  1 natasha  staff   983 Apr 12 01:08 hosts
          
Now add read access for the group.
          % chmod g+r hosts
          % ls -l
          -rwxr-----  1 natasha  staff   983 Apr 12 01:08 hosts
          

<   previous   |   table of contents   |   next   >