-->
Previous | Table of Contents | Next |
In Linux, moving and renaming files are accomplished with the same command: mv. The syntax and rules are the same for mv as they are for the copy command, cp. That is, you can move as many files as you want to a directory, but the directory name must be last in the list and you must have write permission to that directory.
One thing you can do with mv that you cant do with cp is move or rename directories. When you move or rename a file, the only thing that happens is the entry in the directory file is changed. Unless the new location is on another physical disk or partition, the file and the contents of the directory are physically moved.
If you try to use rm (for remove) or cp without options on a directory, the command fails and displays a message telling you that the item youre dealing with is a directory. To remove or copy directories, you must use the -r flag (for recursive) with rm and cp. The mv command, however, moves directories quite happily.
The command to remove a file is rm. To delete a file you dont own, you need read and write permission. If you own the file, youre allowed to delete it, provided that you havent closed off your own permission to the file. For example, if you turn off write permission to a file by typing chmod 000 file, you must open permission again with the chmod command (by typing chmod 644 file) before you can delete it.
If you accidentally type rm *, you delete all the files you have permission to delete in the current directory; you dont delete the subdirectories. To delete subdirectories, you must use the recursive option (-r).
Some versions of rm stop and ask whether you really want to delete files that you own but dont have at least write permission for. Other versions of rm prompt you for any files marked for removal with wildcards. Indeed, you can write a macro or shell script that gives you a second chance before actually deleting a file.
If your version of rm balks at removing files you own but dont have write permission for, you can partially protect yourself from accidentally deleting everything in your directory by following these steps:
If your version of rm balks at removing the 0 file when you type rm *, you have the chance to think about what you just did. If you didnt intend to delete everything in your directory, press <Del> or <Ctrl-c> to kill the rm process. To test this out, try removing just the file named 0. Dont use rm * because if your version of rm doesnt stop at the file 0, youll erase all the files in your directory.
A better way to protect yourself from accidentally deleting files is to use the -i flag with rm. The -i flag stands for interactive. If you give the command rm -i filename, youre asked whether you really want to delete the file. You must answer yes before the file is actually deleted. If you type the command rm -i *, you must answer yes for every file in your directory. This should give you enough time to think about what you really want to do.
CAUTION:
Think before you delete files. Unlike in Windows, DOS, or MAC, when you delete a file (in most versions of Linux), its gone and the only way to recover a lost file is from a backup. You did make a backup, didnt you?
See Performing Backups and Restoring Files, p. 229
If you use the rm -i command frequently, you can implement it in two ways: by writing a shell script or by creating a shell function. If you write a shell script, remember that the shell searches for commands in the directories listed in your PATH variable in the order in which theyre listed. If your $HOME/bin directory is listed last, a shell script named rm will never be found. You can place your $HOME/bin directory first in the PATH variables list or create a new command, such as del. If you create a shell script called del, you must mark it as executable with the chmod command for the shell to recognize it. When you create your del command, you need to give it only one command: rm -i $*. If you then type the command del *, the shell translates it into rm -i *.
See Editing and Aliasing Shell Commands, p. 364
Another way to accomplish the same task is with an alias, which takes precedence over commands that must be looked up. You can think of an alias as an internal shell command (similar to the doskey commands introduced in MS-DOS version 5.0).
To add an alias if youre using the C shell, you must edit the file named .cshrc. You can use any text editor, such as vi (see Chapter 8, Using the vi Editor), to edit this file. For the C shell, add the following lines to the top of your .cshrc file:
rm () { /bin/rm -i $* }
To add an alias to the Korn shell, add the following line to your $HOME/.kshrc file:
alias rm rm -i $*
Previous | Table of Contents | Next |