Can you rename a directory in Linux using the CLI?

last updated in Categories , , , ,

Can you rename a directory in Linux using the command line options?

Yes, you can rename a directory (also called “folder”) in Linux bash shell. One need to use the mv command to rename and move files and directories or folders. Everything is a file under Linux operating system including folders.

Can you rename a directory in Linux?

The procedure to rename a directory in Linux is as follows:

  1. Open the Linux Terminal application or bash prompt
  2. Type the following Linux command to rename “delta” folder to “data”:
    mv delta data
  3. You can use full path too:
    mv /home/vivek/old-folder-name /home/vivek/new-folder-name

Let us see the mv command examples and syntax in details to rename a folder in Linux.

Examples

The pwd command shows current working directory:
pwd
Sample outputs:

/home/nixcraft/

The mv command syntax form is as follows:
mv old-name new-name
mv [options] old-name new-name
mv [options] source target

To rename a directory named “sales” to “SalesData” in the current directory:
pwd
ls -l
mv sales SalesData

Verify it with the ls command:
ls -l
Command to rename a directory in Linux
If the SalesData folder is located in the same directory as the sales directory, then the pics directory can only be renamed. Hence always use pwd command and ls command to find out your current working directory.

Getting help about mv command on Linux

Run the following mv command at the cli to read man page of mv for all option:
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: <https://www.gnu.org/software/coreutils/>
Full documentation at: <https://www.gnu.org/software/coreutils/mv>
or available locally via: info '(coreutils) mv invocation'

Conclusion

You learned how to rename a directory in Linux using the mv command. See mv help page here for more info.

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

1 comment

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