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 DOS, 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.
JUSTA MINUTE
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.)
6
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 interchange- able. The backquotes, also known as grave accents, are more unusual and delineate commands within other commands.
4. 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.
5
6
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 if 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.
Summary
You now have completed six hours of UNIX instruction, and you are armed with enough commands to cause trouble and make UNIX do what you want it to do. In this hour, you learned the differences between cp and mv for moving files and how to use mv to rename both files and directories. You also learned how to create directories with the mkdir command and how to remove them with the rmdir command. And you learned about the rm command for removing files and directories, and how to avoid getting into too much trouble with it. Finally, if you were really paying attention, you learned how to identify which login shell you’re using (csh, ksh, or sh) and how to change from one to another using the chsh command.
Workshop
The Workshop summarizes the key terms you learned and poses some questions about the topics presented in this chapter. It also provides you with a preview of what you will learn in the next hour.
Key Terms
password entry For each account on the UNIX system, there is an entry in the account
database known as the password file. This also contains an encrypted copy of the account password. This set of information for an individual account is known as the password entry.
6
shell alias Most UNIX shells have a convenient way for you to create abbreviations for commonly used commands or series of commands, known as shell aliases. For example, if I always found myself typing ls-CF, an alias can let me type just ls and have the shell automatically add the -CF flags each time.
Questions
1. What are the differences between cp and mv?
2. If you were installing a program from a floppy disk onto a hard disk, would you use cp or mv?
3. If you know DOS, this question is for you. Although DOS has a RENAME command, it doesn’t have both COPY and MOVE. Which of these two do you think DOS
includes? Why?
4. Try using mkdir to create a directory. What happens and why?
5. You’ve noticed that both rmdir and rm -r can be used to remove directories. Which is safer to use?
6. The rm command has another flag that wasn’t discussed in this hour. The -f flag forces removal of files regardless of permission (assuming you’re the owner, that is). In combination with the -r flag, this can be amazingly destructive. Why?
Preview of the Next Hour
The seventh hour introduces the useful file command, which indicates the contents of any file in the UNIX file system. With file, you will explore various directories in the UNIX file system to see what it reveals about different system and personal files. Then, when you’ve found some files worth reading, you will learn about cat, more, and pg, which are different ways of looking at the contents of a file.
7
Hour
7
Looking into Files
By this point, you’ve learned a considerable number of UNIX commands and a lot about the operating and file systems. This hour focuses on UNIX tools to help you ascertain what type of files you’ve been seeing in all the different directories. It then introduces five powerful tools for examining the content of files.
Goals for This Hour
In this hour, you learn how to■ Use file to identify file types
■ Explore UNIX directories with file
■ Peek at the first few lines with head
■ View the last few lines with tail
■ View the contents of files with cat
■ View larger files with more
7