Manipulating the UNIX File System

You know how to find out what files are where in the file system using ls, but there are also various incredibly useful commands that let you manipulate and modify files, directories, and the file system itself. That's what we'll focus on here.

Task 6.1: Creating New Directories Using mkdir

One important aspect of UNIX that has been emphasized continually in this book is that the UNIX file system is hierarchical. The UNIX file system includes directories containing files and directories, each directory of which can contain yet more files and directories. Your own home directory, however, probably doesn't contain any directories (except “.” and “..” of course), which prevents you from exploiting what I call the virtual file cabinet of the file system.


The command for creating directories is actually one of the least complex and most mnemonic (for UNIX, anyway) in this book: mkdir, called “make directory.”

Pronounce the mkdir command as “make dir.”


  1. Turn to your computer, move to your home directory, and examine the files and directories there. Here's an example:

    								% cd
    								% ls
    Archives/               OWL/                    rumors.26Oct.Z
    InfoWorld/              PubAccessLists.Z        rumors.5Nov.Z
    LISTS                   bin/                    src/
    Mail/                   educ
    News/                   mailing.lists.bitnet.Z
    
  2. To create a directory, you need to specify what you'd like to name the directory and where you'd like to locate it in the file system (the default location is your current working directory):

    % mkdir NEWDIR
    % ls
    Archives/               News/                mailing.lists.bitnet.Z
    InfoWorld/              OWL/                 rumors.26Oct.Z
    LISTS                   PubAccessLists.Z     rumors.5Nov.Z
    Mail/                   bin/                 src/
    NEWDIR/                 educ
    

Although UNIX is very flexible about file and directory names, as a general rule you'll want to avoid spaces, tabs, control characters, and the / character.


  1. That's all there is to it. You've created your first UNIX directory, and you can now list it with ls to see what it looks like:

    % ls -ld NEWDIR
    drwxrwx---  2 taylor         24 Nov  5 10:48 NEWDIR/
    % ls -la NEWDIR
    total 2
    drwxrwx---  2 taylor         24 Nov  5 10:48 ./
    drwx------ 11 taylor       1024 Nov  5 10:48 ../
    

    Not surprisingly, the directory is empty other than the two default entries of “.” (the directory itself) and “..” (the parent directory, your home directory).

  2. Look closely at the permissions of the directory. Remember that the permissions are a result of your umask setting. As you learned in the preceding hour, changing the umask setting changes the default directory permissions. Then, when you create a new directory, the new permissions will be in place:

    % umask
    07
    % umask 0
    % mkdir NEWDIR2
    % ls -ld NEWDIR2
    drwxrwxrwx  2 taylor         24 Nov  5 10:53 NEWDIR2/
    % umask 222
    % mkdir NEWDIR3
    % ls -ld NEWDIR3
    dr-xr-xr-x  2 taylor         24 Nov  5 10:54 NEWDIR3/
    

    What happens if you try to create a directory with a name that has already been used?

    % mkdir NEWDIR
    mkdir: NEWDIR: File exists
    

    To create a directory someplace other than your current location, prefix the new directory name with a location:

    % mkdir /tmp/testme
    % ls -l /tmp
    -rwx------  1 zhongqi     22724 Nov  4 21:33 /tmp/a.out*
    -rw-------  1 xujia       95594 Nov  4 23:10 /tmp/active.10122
    -rw-r--r--  1 beast         572 Nov  5 05:59 /tmp/anon1
    -rw-rw----  1 root            0 Nov  5 10:30 /tmp/bar.report
    -rw-------  1 qsc             0 Nov  5 00:18 /tmp/lh013813
    -rwx------  1 steen       24953 Nov  5 10:40 /tmp/mbox.steen*
    -rwx------  1 techman      3711 Nov  5 10:45 /tmp/mbox.techman*
    -rw-r--r--  1 root       997536 Nov  5 10:58 /tmp/quotas
    -rw-------  1 zhongqi    163579 Nov  4 20:16 /tmp/sp500.1
    drwxrwx---  2 taylor         24 Nov  5 10:56 testme/
    -rw-r--r--  1 aru            90 Nov  5 02:55 /tmp/trouble21972
    

Like other basic UNIX utilities, mkdir has no command arguments, so it is quite easy to use. There are two things to keep in mind: You must have write permission to the current directory if you're creating a new directory, and you should ensure that the name of the directory is not the same as (or, to avoid confusion, similar to) a directory name that already exists.


Task 6.2: Copying Files to New Locations Using cp

One of the most basic operations in any system is moving files, the modern-office computer equivalent of paper shuffling. On a computer, moving files is a simple matter of using one or two commands: You can move a file to a different location, or you can create a copy of the file and move the copy to a different location.


The Macintosh has an interesting strategy for differentiating between moving and copying. If you drag a file to another location on the same device (a hard disk, for example), by default the computer moves the file to that location. If you drag the file to a location on a different device (from a floppy to a hard disk, for instance), the computer automatically copies the file, placing the new, identically named copy on the device.

UNIX lacks this subtlety. Instead, UNIX lets you choose which of the two operations you'd like to perform. The two commands are typically succinct UNIX mnemonics: mv to move files, and cp to copy files. The mv command also serves the dual duty of enabling you to rename files.

Pronounce cp as “sea pea.” When you talk about copying a file, however, say “copy.” Similarly, pronounce mv as “em vee,” but when you speak of moving a file, say “move.”


I find myself using cp more than mv because it offers a slightly safer way to organize files: If I get confused and rename it such that it steps on another file (you'll see what I mean in a moment), I still have original copies of all the files.

  1. The format of a cp command is to specify first the name of the file you want to copy and then the new filename. Both names must be either relative filenames (that is, without a leading slash or other indication of the directory) or absolute filenames. Start out by making a copy of your .login file, naming the new copy login.copy:

    % cp .login login.copy
    % ls -ld .login login.copy
    -rw-------  1 taylor       1858 Oct 12 21:20 .login
    -rw-------  1 taylor       1858 Nov  5 12:08 login.copy
    

    You can see that the new file is identical in size and permissions but that it has a more recent creation date, which certainly makes sense.

  2. What happens if you try to copy a directory?

    % cp olddir newdir
    cp: olddir: Is a directory (not copied).
    

    Generally, UNIX won't permit you to use the cp command to copy directories.

I found that this command worked—sort of—on one machine I have used. The system's response to the cp command indicated that something peculiar was happening with the following message:

cp: .: Is a directory (copying as plain file)

But, the system also created newdir as a regular, executable file. You may find that your system reacts in this manner, but you probably do not have any use for it.


  1. The cp command is quite powerful, and it can copy many files at once if you specify a directory as the destination rather than specifying a new filename. Further, if you specify a directory destination, the program automatically will create new files and assign them the same names as the original files.

    First, you need to create a second file to work with:

    								% cp .cshrc cshrc.copy
    							

    Now try it yourself. Here is what I did:

    % cp login.copy cshrc.copy NEWDIR
    % ls -l NEWDIR
    total 4
    -rw-------  1 taylor       1178 Nov  5 12:18 cshrc.copy
    -rw-------  1 taylor       1858 Nov  5 12:18 login.copy
    

You can use the cp command to copy an original file as a new file or to a specific directory (the format being cp original-file new-file-or-directory), and you can copy a bunch of files to a directory (cp list-of-files new-directory). Experiment with creating new directories using mkdir and copying the files into the new locations. Use ls to confirm that the originals aren't removed as you go along.


Task 6.3: Moving Files to New Locations Using mv

Whereas cp leaves the original file intact, making a sort of electronic equivalent of a photocopy of a paper I may pick up at my desk, mv functions like a more traditional desk: Papers are moved from one location to another. Rather than creating multiple copies of the files you're copying, mv physically relocates them from the old directory to the new.


  1. You use mv almost the same way that you use cp:

    % ls -l login.copy
    -rw-------  1 taylor       1858 Nov  5 12:08 login.copy
    % mv login.copy new.login
    % ls -l login.copy new.login
    login.copy not found
    -rw-------  1 taylor       1858 Nov  5 12:08 new.login
    
  2. Also, you move a group of files together using mv almost the same way you do it using cp:

    % cd NEWDIR
    % ls
    cshrc.copy  login.copy
    % mv cshrc.copy login.copy ..
    % ls -l
    total 0
    % ls ..
    Archives/               OWL/                   mailing.lists.bitnet.Z
    InfoWorld/              PubAccessLists.Z        new.login
    LISTS                   bin/                    rumors.26Oct.Z
    Mail/                   cshrc.copy              rumors.5Nov.Z
    NEWDIR/                 educ                    src/
    News/                   login.copy
    
  3. Because you can use mv to rename files or directories, you can relocate the new directory NEWDIR. However, you cannot use mv to relocate the dot directory because you're inside it:

    % mv . new.dot
    mv: .: rename: Invalid argument
    
  4. Both mv and cp can be dangerous. Carefully consider the following example before trying either mv or cp on your own computer:

    % ls -l login.copy cshrc.copy
    -rw-------  1 taylor       1178 Nov  5 12:38 cshrc.copy
    -rw-------  1 taylor       1858 Nov  5 12:37 login.copy
    % cp cshrc.copy login.copy
    % ls -l login.copy cshrc.copy
    -rw-------  1 taylor       1178 Nov  5 12:38 cshrc.copy
    -rw-------  1 taylor       1178 Nov  5 12:38 login.copy
    

    Without bothering to warn me, UNIX copied the file cshrc.copy over the existing file login.copy. Notice that after the cp operation occurred, both files had the same size and modification dates.

    The mv command will cause the same problem:

    % ls -l cshrc.copy login.copy
    -rw-------  1 taylor       1178 Nov  5 12:42 cshrc.copy
    -rw-------  1 taylor       1858 Nov  5 12:42 login.copy
    % mv cshrc.copy login.copy
    % ls -l cshrc.copy login.copy
    cshrc.copy not found
    -rw-------  1 taylor       1178 Nov  5 12:42 login.copy
    

The good news is that you can set up UNIX so it won't overwrite files. The bad news is that for some reason many systems don't default to this behavior. If your system is configured reasonably, when you try either of the two preceding dangerous examples, the system's response is remove login.copy? You can either press the Y key to replace the old file or press Enter to change your mind. If your system cannot be set up to respond this way, you can use the -i flag to both cp and mv to avoid this problem. Later, you learn how to permanently fix this problem with a shell alias.


Together, mv and cp are the dynamic duo of UNIX file organization. These commands enable you to put the information you want where you want it, leaving duplicates behind if desired.


Task 6.4: Renaming Files with mv

Both Windows and Macintosh systems have easy ways to rename files. In DOS, you can use RENAME to accomplish the task. On the Mac, you can select the name under the file icon and enter a new filename. On Windows, you right-click the icon and choose 'Rename' from the pop-up menu.


UNIX has neither option. To rename files, you use the mv command, which, in essence, moves the old name to the new name. It's a bit confusing, but it works.

  1. Rename the file cshrc.copy with your own first name. Here's an example:

    % ls -l cshrc.copy
    -rw-------  1 taylor       1178 Nov  5 13:00 cshrc.copy
    % mv cshrc.copy dave
    % ls -l dave
    -rw-------  1 taylor       1178 Nov  5 13:00 dave
    
  2. Rename a directory too:

    % ls -ld NEWDIR
    drwxrwx---  2 taylor        512 Nov  5 12:32 NEWDIR/
    % mv NEWDIR New.Sample.Directory
    % ls -ld New.Sample.Directory
    drwxrwx---  2 taylor        512 Nov  5 12:32 New.Sample.Directory/
    
  3. Be careful! Just as moving files with cp and mv can carelessly overwrite existing files, renaming files using mv can overwrite existing files:

    								% mv dave login.copy
    							

    If you try to use mv to rename a directory with a name that already has been assigned to a file, the command fails:

    % mv New.Sample.Directory dave
    mv: New.Sample.Directory: rename: Not a directory
    

    The reverse situation works fine because the file is moved into the directory as expected. It's the subtlety of using the mv command to rename files.

  4. If you assign a new directory a name that belongs to an existing directory, some versions of mv will happily overwrite the existing directory and name the new one as requested:

    % mkdir testdir
    								% mv New.Sample.Directory testdir
    							

Being able to rename files is another important part of building a useful UNIX virtual file cabinet for yourself. There are some major dangers involved, however, so tread carefully and always use ls in conjunction with cp and mv to ensure that in the process you don't overwrite or replace an existing file.


Task 6.5: Removing Directories with rmdir

Now that you can create directories with the mkdir command, it's time to learn how to remove directories using the rmdir command.


  1. With rmdir, you can remove any directory for which you have appropriate permissions:

    % mkdir test
    % ls -l test
    total 0
    % rmdir test
    							

    Note that the output of ls shows there are no files in the test directory.

  2. The rmdir command removes only directories that are empty:

    % mkdir test
    % touch test/sample.file
    % ls -l test
    total 0
    -rw-rw----  1 taylor          0 Nov  5 14:00 sample.file
    % rmdir test
    rmdir: test: Directory not empty
    

    To remove a directory, you must first remove all files therein using the rm command. In this example, test still has files in it.

  3. Permissions are important, too. Consider what happens when I try to remove a directory that I don't have permission to touch:

    % rmdir /tmp
    rmdir: /tmp: Permission denied
    % ls -l /tmp
    drwxrwxrwt 81 root        15872 Nov  5 14:07 /tmp/
    

    The permissions of the parent directory, rather than the directory you're trying to remove, are the important consideration.

There's no way to restore a directory you've removed, so be careful and think through what you're doing. The good news is that, because with rmdir you can't remove a directory having anything in it (a second reason the attempt in the preceding example to remove /tmp would have failed), you're reasonably safe from major gaffes. You are not safe, however, with the next command, rm, because it will remove anything.


Task 6.6: Removing Files Using rm

The rm command is the most dangerous command in UNIX. Lacking any sort of archival or restoration feature, the rm command removes files permanently. It's like throwing a document into a shredder instead of into a dustbin.


  1. Removing a file using rm is easy. Here's an example:

    % ls -l login.copy
    -rw-------  1 taylor       1178 Nov  5 13:00 login.copy
    % rm login.copy
    % ls -l login.copy
    login.copy not found
    

    If you decide that you removed the wrong file and actually wanted to keep the login.copy file, it's too late. You're out of luck.

  2. You can remove more than one file at a time by specifying each of the files to the rm command:

    % ls
    Archives/               PubAccessLists.Z        new.login
    InfoWorld/              bin/                    rumors.26Oct.Z
    LISTS                   cshrc.copy              rumors.5Nov.Z
    Mail/                   educ                    src/
    News/                   login.copy              test/
    OWL/                    mailing.lists.bitnet.Z  testdir/
    % rm cshrc.copy login.copy new.login
    % ls
    Archives/               OWL/                    rumors.26Oct.Z
    InfoWorld/              PubAccessLists.Z        rumors.5Nov.Z
    LISTS                   bin/                    src/
    Mail/                   educ                    test/
    News/                   mailing.lists.bitnet.Z  testdir/
    
  3. Fortunately, rm does have a command flag that to some degree helps avoid accidental file removal. When you use the -i flag to rm (the i stands for interactive in this case), the system will ask you whether you're sure you want to remove the file:

    % touch testme
    % rm -i testme
    rm: remove testme? n
    % ls testme
    testme
    % rm -i testme
    rm: remove testme? y
    % ls testme
    testme not found
    

    Note that n is no and y is yes. Delete the file.

  4. Another flag that is often useful for rm, but is very dangerous, is the -r flag for recursive deletion of files (a recursive command repeatedly invokes itself). When the -r flag to rm is used, UNIX will remove any specified directory along with all its contents:

    % ls -ld test ; ls -lR test
    drwxrwxrwx  3 taylor        512 Nov  5 15:32 test/
    total 1
    -rw-rw----  1 taylor          0 Nov  5 15:32 alpha
    drwxrwx---  2 taylor        512 Nov  5 15:32 test2/
    
    test/test2:
    total 0
    -rw-rw----  1 taylor          0 Nov  5 15:32 file1
    % rm -r test
    % ls -ld test
    test not found
    

    Without any warning or indication that it was going to do something so drastic, entering rm -r test caused not just the test directory, but all files and directories inside it as well, to be removed.

This latest example demonstrates that you can give several commands in a single UNIX command line. To do this, separate the commands with a semicolon. Instead of giving the commands ls -ld test and ls -lR test on separate lines, I opted for the more efficient ls -ld test; ls -lR test, which uses both commands at once.


The UNIX equivalent of the paper shredder, the rm command allows easy removal of files. With the -r flag, you can even clean out an entire directory. Nothing can be retrieved after the fact, however, so use great caution.


Task 6.7: Minimizing the Danger of the rm Command

At this point, you might be wondering why I am making such a big deal of the rm command and the fact that it does what it is advertised to do—ƒthat is, remove files. The answer is that learning a bit of paranoia now can save you immense grief in the future. It can prevent you from destroying a file full of information you really needed to save.


For Windows, there are commercial programs (Norton Utilities, for instance) that can retrieve accidentally removed files. The trash can on the Macintosh can be clicked open and the files retrieved with ease. If the trash can is emptied after a file is accidentally discarded, a program such as Symantec Utilities for the Macintosh can be used to restore files.

UNIX just doesn't have that capability, though, and files that are removed are gone forever.

The only exception is if you work on a UNIX system that has an automatic, reliable backup schedule. In such a case, you might be able to retrieve from a storage tape an older version of your file (maybe).

That said, there are a few things you can do to lessen the danger of using rm and yet give yourself the ability to remove unwanted files.

  1. You can use a shorthand, a shell alias, to attach the -i flag automatically to each use of rm. To do this, you need to ascertain what type of login shell you're running, which you can do most easily by using the following command. (Don't worry about what it all does right now. You learn about the grep command a few hours from now.)

    % grep taylor /etc/passwd
    taylor:?:19989:1412:Dave Taylor/users/taylor:/bin/csh
    

    The last word on the line is what's important. The /etc/passwd file is one of the database files UNIX uses to track accounts. Each line in the file is called a password entry or password file entry. On my password entry, you can see that the login shell specified is /bin/csh. If you try this and you don't have an identical entry, you should have /bin/sh or /bin/ksh.

  2. If your entry is /bin/csh, enter exactly what is shown here:

    % echo "alias rm /bin/rm -i" >> ~/.cshrc
    % source ~/.cshrc
    							

    Now rm includes the -i flag each time it's used:

    % touch testme
    % rm testme
    rm: remove testme? n
    
  3. If your entry is /bin/ksh, enter exactly what is shown here, paying particular attention to the two different quotation-mark characters used in the example:

    $ echo 'alias rm="/bin/rm -i"' >> ~/.profile
    $ .  ~/.profile
    							

    Now rm includes the -i flag each time it's used.

One thing to pay special attention to is the difference between the single quote ('), the double quote ("), and the backquote (`). UNIX interprets each differently, although single and double quotes are often interchangeable. The backquotes, also known as grave accents, are more unusual and delineate commands within other commands.


  1. If your entry is /bin/sh, you cannot program your system to include the -i flag each time rm is used. The Bourne shell, as sh is known, is the original command shell of UNIX. The Bourne shell lacks an alias feature, a feature that both the Korn shell (ksh) and the C shell (csh) include. As a result, I recommend that you change your login shell to one of these alternatives, if available.

    To see what's available, look in the /bin directory on your machine for the specific shells:

    % ls -l /bin/sh /bin/ksh /bin/csh
    -rwxr-xr-x  1 root       102400 Apr  8  1991 /bin/csh*
    -rwxr-xr-x  1 root       139264 Jul 26 14:35 /bin/ksh*
    -rwxr-xr-x  1 root        28672 Oct 10  1991 /bin/sh*
    

    Most of the examples in this book focus on the C Shell because I think it's the easiest of the three shells to use. To change your login shell to csh, you can use the chsh—change login shell—command:

    % chsh
    Changing login shell for taylor.
    Old shell: /bin/sh
    New shell: /bin/csh
    

    Now you can go back to instruction 2 and set up a C shell alias. This will help you avoid mischief with the rm command.

The best way to avoid trouble with any of these commands is to learn to be just a bit paranoid about them. Before you remove a file, make sure it's the one you want. Before you remove a directory, make doubly sure that it doesn't contain any files you might want. Before you rename a file or directory, double-check to see whether renaming it is going to cause any trouble.


Take your time with the commands you learned in this hour, and you should be fine. Even in the worst case, you might have the safety net of a system backup performed by a system administrator, but don't rely on it.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset