Site icon CentLinux

How to Rename File in Linux CLI

Share on Social Media

This is a short article that will solve a very common problem encountered by Linux newbies about how to rename file in Linux CLI? #centlinux #linux #unix

Problem Statement

In fact, Linux or Unix do not shipped with a native command to rename files or directories.

But, GNU Coreutils provides mv (short for move) command, that is used to move files or directories from one location to another.

By leveraging this feature in mv command we can rename files and directories.

Recommended Online Training: Learn Bash Shell in Linux for Beginners

Syntax of mv command

To obtain help on command line syntax of mv command, execute following command on a Linux or Unix shell.

# mv --help
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: <https://www.gnu.org/software/coreutils/>
Full documentation at: <https://www.gnu.org/software/coreutils/mv>
or available locally via: info '(coreutils) mv invocation'

How to Rename File in Linux CLI

To rename a file, we can use mv command as follows.

# mv -v file1 file2
renamed 'file1' -> 'file2'

How to Rename directory in Linux CLI

To rename a directory, we can use mv command as follows.

# mv -v dir1 dir2
renamed 'dir1' -> 'dir2'

We have used –v switch in above commands to display the output of above mv command on the console. However, if we omit –v switch, the mv command won’t display any output, despite of successful execution.

Access Help Manual for mv Command

To keep things simple and limit the scope of this article, we are not discussing each command line switch here. You can refer to GNU official documentation of mv command or use the following command to access the manual of the mv command.

# man mv

A snapshot of the manual on a CentOS 8 based Linux system is as follows.

MV(1)                            User Commands                           MV(1)

NAME
mv - move (rename) files

SYNOPSIS
mv [OPTION]... [-T] SOURCE DEST
mv [OPTION]... SOURCE... DIRECTORY
mv [OPTION]... -t DIRECTORY SOURCE...

DESCRIPTION
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
Manual page mv(1) line 1 (press h for help or q to quit)

If you are new to Linux and facing difficulty in working at Linux Bash prompt. We recommend that, you should read The Linux Command Line, 2nd Edition: A Complete Introduction by William Shotts.

Final Thoughts

Renaming files in the Linux command line can be straightforward with the right commands. Whether you’re using mv for single files or more advanced tools for bulk renaming, mastering these techniques can enhance your efficiency in managing files on a Linux system.

For a detailed, step-by-step guide on how to rename files in the Linux CLI, check out my Fiverr gig: Linux System Admin. I offer expert guidance to ensure you can handle file renaming tasks with ease.

If you have any questions or need further assistance, feel free to reach out!

Exit mobile version