You need to use the mv command to rename and move files and directories/folders. Everything is a file under Linux or Unix-like operating system including folders.
Linux Rename Folder Command
The procedure to rename a folder or directory on Linux:
- Open the Terminal application.
- Type the following command to rename foo folder to bar:
mv foo bar
You can use full path too:
mv /home/vivek/oldfolder /home/vivek/newfolder
Let us see examples and syntax in details.
How to rename folders in Linux
The pwd command shows current working directory:
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 called pics to mypictures in the current directory:
mv pics mypictures
Verify it with the ls command:
If the mypictures folder is located in the same directory as the pics folder, then the pics folder can only be renamed. Hence always use pwd command and ls command to find out your current working directory.
How to move folders in Linux
The following command would move a folder named accounting, without changing its name, from the current directory to an existing directory named /mnt/backups/:
mv accounting /mnt/backups/
You can give full path too:
mv /home/vivek/accounting/ /mnt/backups/
Let us move multiple files/folders. The following mv command moves all files and directories, including all the contents of those directories, from the current directory to the directory /raid/home/new/vivek/:
mv * /raid/home/new/vivek/
The asterisk is a wildcard character that represents any or all files in the current directory. To get confirmation when renaming or moving folders pass the -i too mv command to avoid overwriting files/folders:
mv -i file.doc ~/Documents/
mv -i ExcelFiles ~/Documents/
mv: overwrite '/home/vivek/Documents/ExcelFiles'?
Pass the -v to explain what is being done i.e. show whether file being moved or renamed and so on:
$ mv -v old new
$ mv -v file.doc /backups/
Pass the -n option to mv to not overwrite an existing file/folder:
$ mv -v -n file.doc /backups/
Sample session from above commands:
Issue the following command at the cli to read man page of mv for all option:
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
A note about rename command
Many Linux distros have rename command that will rename the specified files by replacing the first occurrence of an expression in their name by replacement. For example, the following command would fix extension of your pl files. In other words, rename all *.perl to *.pl file:
rename .perl .pl *.perl
See “Linux Rename Multiple Files At a Shell Prompt” for more examples and usage.
You learned how to rename directories in Linux using the mv command. See mv command docs here for more info.