-->
Previous Table of Contents Next


Moving and Renaming Files with Mv

The mv command is used to move files from one directory to another. This command doesn’t leave a copy of the original file in the original location (for that, use the cp command); it deletes the original copy and inserts the new copy in the new location.

The following command line would move the textfile file to the new home (~) location:


     gilbert:/usr$ mv textfile ~

If you were to run the ls, you’d find that textfile didn’t appear in /usr, but was now located in your home directory.

In this example, textfile retains its current filename, no matter where you move it. You can also use the mv command to rename a file. (In fact, it’s one of the few ways to actually rename a file, because there’s no command for doing so within Linux.) The following command changes the textfile filename to aardvark:


     gilbert:~$ mv textfile aardvark

The following command line would move textfile to a new directory and give it a new filename of aardvark:


     gilbert:/usr$ mv textfile ~/aardvark

Linux can be fairly harsh when you’re moving and renaming files. For example, the mv command will overwrite an existing file with a renamed file and not warn you. If you ran the following command line and a file named aardvark already existed in your home directory, you’d be in trouble:


     gilbert:/usr$ mv textfile ~/aardvark

as mv would overwrite the original aardvark file with the new aardvark file. To avoid this problem, use the -i option with the mv command:


     gilbert:/usr$ mv -i textfile ~/aardvark



     mv: overwrite 'aardvark'?

Type y if you want to overwrite aardvark, n (or any other key) if you do not.

A summary of the options to mv are listed in Table 4.9.

Table 4.9 A Summary of the Mv Command Options
Option Result

-f Overwrites existing file.
-I Checks before overwriting existing files.

Removing Files with Rm

The rm (short for remove) command removes files. Simple enough, right? To remove a file, simply list it on the command line:


     gilbert:~$ rm aardvark

Aardvark will then be swiftly and painlessly removed—so swiftly that you won’t have a chance to confirm your choice. However, like the other commands listed in this chapter, you can tell Linux to confirm your file deletions, in the form of the -i option:


     gilbert:~$ rm -i aardvark

     rm: remove 'aardvark'?

Type y if you want to remove aardvark, n (or any other key) if you do not.

Other options to the rm command are listed in Table 4.10.

Table 4.10 Options to the Rm Command
Option Result

-f Removes the file without any input from you.
-i Runs in interactive mode.
-v Runs in verbose mode, which means files are listed as they are removed.


WARNING:  Be warned that when you remove a file under Linux, you’re really removing the file from existence.

If you’re a PC or Macintosh user, you may have gotten spoiled by utilities like The Norton Utilities, which can “unerase” files that have been erased. At this time, no such utilities exist for Linux.



WARNING:  Be careful when you combine the rm command and wildcards, because a wildcard—especially an asterisk—in the wrong spot can wreak havoc with your system. For example, let’s say that you wanted to delete all the files ending with .golf on your system (let’s say the boss is beginning to be a little suspicious about your afternoon field trips and you want to remove any incriminating evidence). So you tell Linux to remove all files ending with golf—or you think you are, anyway:

     gilbert:~/memos$ rm * golf

Disaster ensues. Because you placed a space between the asterisk wildcard and the rest of the command line, the rm command uses only the asterisk as an argument, ignoring the golf part of the command line. Since every file is returned by the asterisk wildcard, you’ve just removed all the files in your current directory. (By the way, the chance of this happening is an excellent argument for using the -i option at all times and setting it up as an alias.)



Previous Table of Contents Next