Moving Files with mv

Moving Files with mv

Moving directories and files means moving them from one location (think of location as an absolute file path, like /home/ejr/aFile) in your system to another location (say, /temp/File or /home/ejr/AnotherFile). Essentially, you have only one version of a file, and you change the location of that version. For example, you might move a directory when you're reorganizing your directories and files. Or, you might move a file to rename itthat is, move a file from one name to another name.

You move directories and files using mv, as shown in Code Listing 2.8.

To move a file or directory:



To begin, use ls to verify the name of the file you want to move. If you're changing the name of the file, you'll want to ensure that the new filename isn't yet in use. If you move a file to an existing filename, the contents of the old file will be replaced with the contents of the new file.


mv existingfile newfile

Type mv plus the existing filename and the new filename. Say goodbye to the old file and hello to the new one (Code Listing 2.8).

You use the same processexactlyto move directories; just specify the directory names, as in mv ExistingDirectory NewDirectory.



Verify that the file is now located in the location you intended.

Code Listing 2.8. List files to see the current files, then use mv to rename one of the files.

$ ls Complete    existingfile   oldfile $ mv existingfile newfile $ ls Completed   newfile        oldfile $ 


  • You can also use mv to move files into or out of directories. For example, mv Projects/temp/testfile /home/deb/ testfile moves testfile from the Projects and temp subdirectories of the current directory to Deb's home directory, also using the name testfile.

  • Use mv -i oldfilename newfilename to require the system to prompt you before overwriting (destroying) existing files. The -i is for "interactive," and it also works with the cp command.

  • Visit Chapter 8 to find out about using aliases with mv so that the system always prompts you before overwriting files and you don't have to remember the -i flag.

  • If you use mv and specify an existing directory as the target (as in, mv something ExistingDirectory), "something," in this case, will be placed into ExistingDirectory. "Something" can be either a file or a directory.

Unix(c) Visual Quickstart Guide
UNIX, Third Edition
ISBN: 0321442458
EAN: 2147483647
Year: 2006
Pages: 251

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: