HowTo: Linux Rename Files

last updated in Categories

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:


Moving A file

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


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:

              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.

              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

              output version information and exit


Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.

1 comment

  1. Is there a way to use copy and have the next behavior?
    original file: original.jpg
    after copy in the same directory generate a second file named
    original1.jpg or 1.jpg or 0001.jpg
    I need this because im generating pictures in a loop but they appear to be erasing the last one so I want to backup these images before I take the next image.

    Still, have a question? Get help on our forum!