How to Rename Directories in Linux

Renaming directories is one of the most common file management operations on a Linux system. You can rename directories from the GUI file manager or from the command-line terminal.
This article explains how to rename directories using the command line.
Renaming a Directory with mv
In Linux and Unix-like operating systems, you use the mv
(move) command to rename or move files and directories from one location to another.
The basic syntax is:
mv [OPTIONS] source destinationTo rename a directory called dir1 to dir2, run:
mv dir1 dir2The first argument is the current name of the directory, and the second is the new name.
If dir2 already exists as a directory, mv moves dir1 inside it rather than renaming it. To avoid surprises, pass the -i flag, which prompts for confirmation before overwriting:
mv -i dir1 dir2Use the -v (verbose) flag to have mv print the operation as it happens:
mv -v dir1 dir2renamed ‘dir1’ -> ‘dir2’To rename a directory that is not in the current working directory, specify either the absolute or relative path:
mv /home/user/dir1 /home/user/dir2Renaming Multiple Directories
Renaming a single directory is straightforward, but renaming several directories at once requires a bit of scripting.
Using mv in a Loop
The mv command can rename only one directory at a time, but you can combine it with a loop or with find
to process many directories in one go.
The following example uses a Bash for
loop to append the current date
to every directory in the current working directory:
for d in *; do
if [ -d "$d" ]; then
mv -- "$d" "${d}_$(date +%Y%m%d)"
fi
doneThe loop iterates over all items in the directory. The [ -d "$d" ] test filters out regular files, and the mv command appends a date stamp to each directory name.
Here is the same task using mv in combination with find:
find . -mindepth 1 -prune -type d -exec sh -c ‘d="{}"; mv -- "$d" "${d}_$(date +%Y%m%d)"’ \;The find command passes each directory to mv one by one using the -exec option. The string {} is the name of the directory currently being processed.
Using the rename Command
The rename command renames multiple files and directories using Perl regular expressions, which makes pattern-based renaming simpler than writing a loop.
The following example replaces spaces in the names of all directories in the current working directory with underscores:
find . -mindepth 1 -prune -type d | rename ‘y/ /_/’Before running rename on real data, pass the -n flag to preview the changes without applying them:
find . -mindepth 1 -prune -type d | rename -n ‘y/ /_/’Here is another example that converts directory names to lowercase:
find . -mindepth 1 -prune -type d | rename ‘y/A-Z/a-z/’Conclusion
The mv command handles single renames, while a for loop or the rename utility covers batch operations. For more on moving and renaming files, see the mv command guide
.
Tags
Linuxize Weekly Newsletter
A quick weekly roundup of new tutorials, news, and tips.
About the authors

Dejan Panovski
Dejan Panovski is the founder of Linuxize, an RHCSA-certified Linux system administrator and DevOps engineer based in Skopje, Macedonia. Author of 800+ Linux tutorials with 20+ years of experience turning complex Linux tasks into clear, reliable guides.
View author page