How To: Rename A File In Bash

How do I rename a file in bash under UNIX / macOS (OS X) / Linux / BSD operating systems?

You need to use the mv command or rename command to rename a file in bash shell.

Rename a file in bash using mv

We need to give SOURCE file to DESTINATION file using the following mv command syntax:

mv oldname newname
mv olddir newdir
mv old-file new-file

Rename SOURCE to DEST, or move SOURCE(s) to DIRECTORY

Task: Rename A File Called /tmp/foo To /tmp/bar

Type the following command (open terminal and issue the following commands):

# create /tmp/foo 
touch /tmp/foo
ls -l /tmp/foo
mv /tmp/foo /tmp/bar
ls -l /tmp/bar
ls -l /tmp/foo

Rename A File In Bash using mv command
The ls command lists files in the current working directory or given directory in Linux or Unix-like systems.

Task: Rename A Directory Called offfer To offers

Type the following command:

mv offfer offers
## or tell us what mv is doing by passing the -v option ##
mv -v offfer offers

Task: Prompt Before Overwrite

The -i option is interactively file processing option. You get an error message before moving a file that would overwrite an existing file. If the response from the user begins with the character y or Y, the move / rename is attempted.

touch /tmp/test
mv -i /tmp/test /tmp/bar

Sample outputs:

mv: overwrite `/tmp/bar'? y

The -u Option

The -u option move only when the SOURCE file is newer than the destination file or when the destination file is missing:

mv -u data.txt /mnt/floppy/backup.txt

The -v Option

The -v option explain what is being done:

mv -v /tmp/bar /tmp/output.txt

Sample outputs:

`/tmp/bar' -> `/tmp/output.txt'

Task: Rename multiple files

Use rename command renames multiple files. For example rename all *.perl file as *.pl, enter:

rename .perl .pl *.perl

See howto rename multiple files at a shell prompt for further details.

Summary of all mv command options

To rename a file in bash we use mv command:

  • -v : Verbose option. In other words, display the progress of the files as they are being moved or renamed in a bash shell
  • -i : Prompt before overwriting files
  • -u : Move only when the SOURCE file is newer than the destination file or when the destination file is missing in a bash shell
  • -f : Do not prompt before overwriting files

To see list of all mv command options, type:
man mv
mv --help

Sample outputs:

Usage: mv [OPTION]... [-T] SOURCE DEST
  or:  mv [OPTION]... -t DIRECTORY SOURCE...
Rename SOURCE to DEST, or move SOURCE(s) to DIRECTORY.
Mandatory arguments to long options are mandatory for short options too.
      --backup[=CONTROL]       make a backup of each existing destination file
  -b                           like --backup but does not accept an argument
  -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
  -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
  -Z, --context                set SELinux security context of destination
                                 file to default type
      --help     display this help and exit
      --version  output version information and exit
The backup suffix is '~', unless set with --suffix or SIMPLE_BACKUP_SUFFIX.
The version control method may be selected via the --backup option or through
the VERSION_CONTROL environment variable.  Here are the values:
  none, off       never make backups (even if --backup is given)
  numbered, t     make numbered backups
  existing, nil   numbered if numbered backups exist, simple otherwise
  simple, never   always make simple backups
GNU coreutils online help: <>
Report mv translation bugs to <>
Full documentation at: <>
or available locally via: info '(coreutils) mv invocation'


To rename a file or directory in bash, use the mv command. The third word on the mv command line must end in the new filename. Hence, the syntax is a follows to renames the file cakeday.png to birthday.png:
mv cakeday.png birthday.png

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

🐧 4 comments so far... add one

CategoryList of Unix and Linux commands
Disk space analyzersncdu pydf
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
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 jobs killall kill pidof pstree pwdx time
Searchinggrep 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
4 comments… add one
  • Philippe Petrinko Mar 6, 2010 @ 21:59

    typo here:
    ename .perl .pl *.perl
    instead of

    • Other 1 Philippe Dec 19, 2013 @ 6:24

      I do not see any typo here.

      • Other 1 Philippe Dec 19, 2013 @ 6:25

        Ah, never mind. I see last updated date at the top of the page. You can delete this comment.

  • ramarasan.manickam Aug 23, 2015 @ 12:38

    nice post

    great that

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