Three Ways Not to Lose Files
Now you’re probably quaking in your boots (or sandals, depending on where you live). You figure that, if you so much as touch the keyboard, you will do horrible, irreparable damage and spend the next week spinning tapes. It’s not that bad. This section tells you some tricks to avoid deleting files by mistake in the first place.
Are you sure you wanna clobber this one?
When you delete files with rm , use the -i (for interactive) switch:
rm -i s*
This line tells rm to ask you before it deletes each file, prompting you with the filename and a question mark. You press the y key if you want to delete it, and anything else to tell UNIX not to delete. (Remember that the question UNIX asks is, "Should I delete this?" and not "Do you want to keep this?") The main problem with -i is that it can become tedious when you want to delete a large number of files. When you do that, you probably use wildcards. To be safe, check that the wildcards refer to the files you think they do. To make that check, use the ls command with the same wildcard. If you want to delete all the files that start with section, for example, and you think that you can get away with typing only sec and an asterisk, you had better check what sec* refers to. First give this command:
ls sec*
UNIX responds with an appropriate list: second.version section04 section08 section12 section16
section01 section05 section09 section13 section17
section02 section06 section10 section14 section18
section03 section07 section11 section15 section19
Hey, look! There’s that file second.version . You don’t want to delete it, so it looks like you have to type section* to get the correct files in this case.
Idiot-proofing save files
The best way to make temporary backup copies of files is to make a directory named save and put all saved copies of files there, as shown in this example:
mkdir save
cp important.file save
These commands tell UNIX to make a directory named save and then to make a copy of important.file to save/important.file . If you reverse the order of the names, nothing happens. Suppose that you type this line instead:
cp save important.file
UNIX makes this observation: cp : <save> directory
UNIX is saying that you can copy a file to a directory but that you can’t copy a directory to a file. As a result, UNIX doesn’t copy anything. To copy a file back from the save directory, you have to use its full name:save/important.file .A variation of this process is a two-step delete. Suppose that you have a bunch of files you want to get rid of but some good files are mixed in the same directory. Make a directory named trash , and then use mv to move the files you plan to delete to the trash directory:
mkdir trash
mv thisfile thatfile these* trash
mv otherfile somefile trash
Then use the ls command to check the contents of trash . If something is in that directory you want, move it back to the current directory by using this command:
mv trash/these.are.still.good .
(The dot at the end means to put the file back in the current directory.) After you’re sure that nothing other than trash is in trash , you can use rm with the -r option:
rm -r trash
This line tells rm to get rid of trash and everything in it.
Don’t write on that!
Another thing you can do to avoid damage to important files is to make them read-only. When you make files read-only, you prevent cp and text editors from changing them. You can still delete them, although rm , mv , and ln ask you before doing so. The chmod command changes the mode of a file (as explained in Chapter 5). Here’s how to use chmod to make a file read-only: chmod -w crucial-file
The -w means not writable. To make changes to the file later, do another chmod but use +w instead. (This stuff doesn’t involve inspired command syntax, but the old syntax was even worse and used octal digits.) After a file is made not writable, editors can’t change it. The vi program and some versions of emacs even display a note on-screen that the file is read-only. If you try to delete it, rm , mv , or ln asks you in a uniquely user-hostile way whether that’s really what you had in mind. Suppose that you type the following line and crucial-file is a read-only file:
rm crucial-file
UNIX responds with this line: crucial-file: 444 mode ?
The number may not be 444 : It may be 440 or 400 (depending on whether your system administrator has set things up so that people can normally see the contents of other people’s files). As with rm -i , you press the y key if you want to delete the file, or anything else to say that you don’t want to delete this valuable data.