How to Rename a File or Directory in Linux or Unix - CentLinux

Latest

Thursday, 23 January 2020

How to Rename a File or Directory in Linux or Unix

How to Rename a File or Directory in Linux or Unix

This is a quick article that will solve a very common problem encountered by newbies: how to rename a file or directory in Linux or Unix?

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.

 

This Article Provides:

     

    Syntax of mv Command for Renaming Files or Directories on Linux:

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

    [root@centos8 ~]# 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 a File in Linux or Unix:

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

    [root@centos8 ~]# mv -v file1 file2 renamed 'file1' -> 'file2'

     

    How to Rename a directory in Linux or Unix:

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

    [root@centos8 ~]# 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 on Linux:

    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.

    [root@centos8 ~]# 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)

    That's all for now, stay tuned for our next article on how to bulk rename files or directories on Linux or Unix?

    No comments:

    Post a comment