How To: Rename A File In Bash

last updated in Categories , , , , , , , , , , , ,

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 SOURCE DEST
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]... SOURCE... DIRECTORY
  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
                                 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
  -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: <http://www.gnu.org/software/coreutils/>
Report mv translation bugs to <http://translationproject.org/team/>
Full documentation at: <http://www.gnu.org/software/coreutils/mv>
or available locally via: info '(coreutils) mv invocation'

Conclusion

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

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.

Start the discussion at www.nixcraft.com

Historical Comment Archive

4 comment

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