How to Rename a Directory or Folder in Linux

By 

•

Updated on

•

5 min read

Rename Directories in Linux title on a yellow background

Renaming directories is one of the most common file management operations on a Linux system. You might need to give a project folder a clearer name, mark an old release with a date, or clean up folder names that contain spaces. You can rename directories from the GUI file manager or from the command-line terminal.

This article explains how to rename a directory (or folder, as it is called in most file managers) using the command line. If you need to rename regular files, see our guide on how to rename files in Linux .

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:

txt
mv [OPTIONS] source destination

To rename a directory called dir1 to dir2, run:

Terminal
mv dir1 dir2

The first argument is the current name of the directory, and the second is the new name.

Unlike the cp command , mv does not need a recursive option for directories. The files inside the directory are not touched, and only the directory name changes.

There is one catch. If dir2 already exists as a directory, mv does not rename dir1. It moves dir1 inside dir2, and you end up with dir2/dir1. The -i option does not help here, because nothing is being overwritten.

To make mv treat the destination strictly as a new name, use the -T option:

Terminal
mv -T dir1 dir2

If dir2 exists and is not empty, the command fails instead of moving dir1 inside it:

output
mv: cannot overwrite 'dir2': Directory not empty

Be aware that if dir2 exists and is empty, -T replaces it with dir1 without asking.

Use the -v (verbose) flag to have mv print the operation as it happens:

Terminal
mv -v dir1 dir2
output
renamed 'dir1' -> 'dir2'

To rename a directory that is not in the current working directory, specify either the absolute or relative path:

Terminal
mv /home/user/dir1 /home/user/dir2

When both names share a long path, you can use Bash brace expansion to avoid typing the path twice. The following command renames /var/www/project to /var/www/project_old:

Terminal
mv /var/www/project{,_old}

The shell expands project{,_old} into project project_old before mv runs.

To rename a directory, you normally need write and execute permissions on its parent directory. If the parent directory requires administrative access, run the command with sudo :

Terminal
sudo mv /opt/app /opt/app-1.0

Renaming 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 each non-hidden directory in the current working directory. The -T option prevents moving a directory inside an existing destination, and -n skips destinations that already exist without prompting:

sh
for d in *; do
  if [ -d "$d" ]; then
    mv -nT -- "$d" "${d}_$(date +%Y%m%d)"
  fi
done

The * pattern matches non-hidden items in the directory. The [ -d "$d" ] test filters out regular files, and the mv command appends a date stamp to each directory name whose destination does not already exist.

To include hidden directories too, use mv in combination with find:

Terminal
find . -mindepth 1 -prune -type d -exec sh -c 'mv -nT -- "$1" "${1}_$(date +%Y%m%d)"' sh {} \;

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, and sh -c receives it as $1. Passing the name as an argument, instead of placing {} inside the quoted script, keeps directory names with quotes or other special characters from breaking the command.

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.

There are two versions of rename with different syntax, and the examples below use the Perl version. It is not installed by default on Ubuntu and Debian. To install it, run:

Terminal
sudo apt install rename

On Fedora, the Perl version is provided by the prename package, and the executable is named prename. Use prename in place of rename in the examples below.

The following example replaces spaces in directory names with underscores. Before applying it, use the -n flag to preview the changes:

Terminal
find . -mindepth 1 -prune -type d -exec rename -n 'y/ /_/' {} +
output
rename(./Photos 2024, ./Photos_2024)
rename(./My Dir, ./My_Dir)

Each line shows the current name and the name rename would give it. The -exec option passes directory names directly as arguments, so names containing newlines are handled correctly.

To apply the changes, run the command without -n:

Terminal
find . -mindepth 1 -prune -type d -exec rename 'y/ /_/' {} +

Here is another example that converts directory names to lowercase:

Terminal
find . -mindepth 1 -prune -type d -exec rename 'y/A-Z/a-z/' {} +

Conclusion

Use mv -nT when a rename must skip existing destinations, and preview pattern-based changes with rename -n. For more on moving and renaming files, see the mv command guide .

Linuxize Weekly Newsletter

A quick weekly roundup of new tutorials, news, and tips.

About the authors

Dejan Panovski

Dejan Panovski

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

View author page