Home > Articles

  • Print
  • + Share This

Moving and Renaming Files and Directories

Moving and renaming files and directories, like file navigation and copying, is a basic skill you'll need when organizing information within Linux. The following sections demonstrate how to use the mv command to move and rename files and directories.

Moving and Renaming Files and Directories with the mv Command

The mv (move) command is used to move or rename files and directories. To rename a file or directory, specify the old filename and the new filename on the command line (unlike the cp command, mv does not leave a copy of the original file or directory):

$ mv error.log error.log.oct99

Use the mv command with caution. You can easily overwrite existing files unless you use the -i (interactive) command-line option. When you use the -i option, the mv command asks for permission to overwrite an existing file, like this:

$ mv -i error.log error.log.oct99 
mv: replace ´error.log.oct99'? y 

A safer way to use the mv command is with the -b (backup) option. When combined with the -i option, the mv -b command asks for permission to overwrite a file, and also creates a backup, like this:

$ mv -bi error.log error.log.oct99 
mv: replace ´error.log.oct99'? y 
$ ls err* 
error.log.oct99 error.log.oct99~ 

If you use the -b (backup) option, the mv command creates a backup of the file being overwritten. The original file remains, with a tilde (~) appended to its filename.

The mv command will move files from one directory to another using either of the following methods. mv will only move files (not directories) from one file system to another.

 $ mv error.log /1999_errorlogs/ 
$ mv error.log /1999_errorlogs/error.log.oct99 

Using the mv command, you can move entire directories. If the destination directory does not exist, mv renames the directory:

$ ls -F 
school/    science/ 
$ mv school homework 
$ ls -F 
homework/    science/ 

If the destination directory exists, the entire directory is moved inside thedestination directory.

$ ls -F 
homework/    science/ 
$ ls -F homework 
compsci/    biology/ 
$ mv science homework 
$ ls -F 
homework/ 
$ ls -F homework 
science/  compsci/  biology/ 
  • + Share This
  • 🔖 Save To Your Account

Related Resources

There are currently no related titles. Please check back later.