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:
- Open the Linux Terminal application or bash prompt
- Type the following Linux command to rename “delta” folder to “data”:
mv delta data - 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
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.
🐧 1 comment so far... add one ↓
Category | List of Unix and Linux commands |
---|---|
File Management | cat |
Firewall | Alpine Awall • CentOS 8 • OpenSUSE • RHEL 8 • Ubuntu 16.04 • Ubuntu 18.04 • Ubuntu 20.04 |
Network Utilities | dig • host • ip • nmap |
OpenVPN | CentOS 7 • CentOS 8 • Debian 10 • Debian 8/9 • Ubuntu 18.04 • Ubuntu 20.04 |
Package Manager | apk • apt |
Processes Management | bg • chroot • cron • disown • fg • jobs • killall • kill • pidof • pstree • pwdx • time |
Searching | grep • whereis • which |
User Information | groups • id • lastcomm • last • lid/libuser-lid • logname • members • users • whoami • who • w |
WireGuard VPN | Alpine • CentOS 8 • Debian 10 • Firewall • Ubuntu 20.04 |
Have a question or comment? Post it on the forum thread here.