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:


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

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 1 comment so far... add one

CategoryList of Unix and Linux commands
Disk space analyzersdf duf ncdu pydf
File Managementcat cp mkdir tree
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Modern utilitiesbat exa
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg glances gtop jobs killall kill pidof pstree pwdx time vtop
Searchingag grep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
1 comment… add one
  • David Rodriguez Jun 15, 2015 @ 15:22

    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.

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum