Sunday, November 25, 2012

HowTo: Linux Rename Files

How do I rename a file under Linux operating systems using command line (bash shell prompt)?

You need to use the mv command to rename files or directories under Linux operating systems. The same command is also used to move files to different directories.

The mv Command Syntax

The syntax is as follows:
 
mv  source target
 
mv [Options]  source target
 

Take: Rename a File Under Linux

Rename a file called curriculum-vitae.txt as resume.txt, enter:
$ mv curriculum-vitae.txt resume.txt
If the target file (resume.txt) is located in the same directory as the source file (curriculum-vitae.txt), then the source file (curriculum-vitae.txt) can only be renamed.

Rename / Move Confirmation (Interactive) Option

You can force to display prompt before overwriting files. The -i option enables interactive option. So if file or directories with the same name already exists in the destination directory, mv will prompt the user:
$ mv -i file2.txt /tmp/
Sample outputs:
mv: overwrite `/tmp/file2.txt'? 

Move / Rename Verbose Option

Pass the -v option to display the name of each file before renaming and/or moving it:
$ mv -v file3.txt /tmp/
Sample outputs:
`file3.txt' -> `/tmp/file3.txt'
To rename file3.txt as file10.bak, enter:
$ mv -v file3.txt file10.bak
Sample outputs:
`file3.txt' -> `file10.bak'

Backup a File

To make a backup of each existing destination file pass the -b option. This option will tell mv to make a backup copy of each file that may be overwritten or removed:
$ touch file10.txt
$ mv -v -b file10.txt /tmp

Sample outputs:
`file10.txt' -> `/tmp/file10.txt'
To view a backup file called file10.bak, enter:
$ ls
Sample outputs:
file10.bak

Moving A file

In this example, move a file called file1.txt to /tmp/ directory, enter:
$ mv file1.txt /tmp/

Wildcards

In this example, move all files and directories, including all the contents of those directories, from the current directory to the directory /home/newdir:
# cd /home/olddir/
# mv * /home/newdir/

Please note that the asterisk (symbol) is nothing but a shell wildcard character that represents all files.

Other mv Command Options

From the mv command man page:
       --backup[=CONTROL]
              make a backup of each existing destination file
      -f, --force
              do not prompt before overwriting
       -i, --interactive
              prompt before overwrite
       -n, --no-clobber
              do not overwrite an existing file
       If you specify more than one of -i, -f, -n, only the final one takes effect.
       --strip-trailing-slashes
              remove any trailing slashes from each SOURCE argument
       -S, --suffix=SUFFIX
              override the usual backup suffix
       -t, --target-directory=DIRECTORY
              move all SOURCE arguments into DIRECTORY
       -T, --no-target-directory
              treat DEST as a normal file
       -u, --update
              move only when the SOURCE file is newer than the destination file or when the destination file is missing
       -v, --verbose
              explain what is being done
       --help display this help and exit
       --version
              output version information and exit