🌐
ServerMania
servermania.com › home › how to › software setup › how to rename a directory in linux: command guide with examples
How to Rename a Directory in Linux: Command Guide & Examples
February 6, 2026 - So, to rename a file, use mv old_name new_name. Here is how the command must look without the brackets: “mv banana apple“. We just changed a file’s name from “banana” to “apple” with a single command.
Top answer
1 of 3
2

Assuming all subdirectories in /path/to/directory follow that naming convention:

cd /path/to/directory
prename -n 's~/\d{4}[a-z]? - ~/~i' */*

prename is the Perl rename (any of the variants will do).

2 of 3
2

This prints:

mv ...

Which is exactly the command I want to execute. But I can't execute it.

I tried assigning the output to a variable and executing it by calling the variable. That didn't work. I tried a few variations on that idea, and I got errors.

It feels like I'm missing some tool here.

The easy way you are looking for is to append | bash to your command. That's how you can go from "a command which prints a command" to "actually running the command that was printed."

However, even though you've included double quotes in the command to be printed, this is a bad idea to include in a script.

  • Why does my shell script choke on whitespace or other special characters?

You may have handled the whitespace aspect, but what if the filenames include double quote characters directly? Or newlines? Or variable names (which will be expanded within double quotes)?

The only characters that are illegal in filenames are a slash (/) and a null byte.

In writing scripts you should hold yourself to a much higher standard of robustness than you might at an interactive command line.

At a command line, you can see the command you are about to run, you can confirm it's correct, and you can then run it. In a script, there is no oversight and no confirmation. The script will do what you told it to do, no matter how destructive that may turn out to be.

So the correct approach is actually to use find, as detailed in my other answer. This will handle any filenames correctly and doesn't contain arbitrary code execution vulnerabilities.

🌐
Masteringunixshell
masteringunixshell.net › qa29 › bash-how-to-rename-directory.html
bash how to rename directory - Mastering UNIX Shell
It will rename old-name-dir to new-name-dir. If old-name-dir contains any files, it is good advice add option -R after mv. Was this information helpful to you? You have the power to keep it alive. Each donated € will be spent on running and expanding this page about UNIX Shell. ... We prepared for you video course Marian's BASH Video Training: Mastering Unix Shell, if you would like to get much more information.
🌐
GeeksforGeeks
geeksforgeeks.org › linux-unix › how-to-rename-folder-in-linux
How to Rename a Folder in Linux (All Methods Explained) - GeeksforGeeks
July 23, 2025 - There are various commands to rename a folder or the directory in Linux, such as the commonly used mv, the advanced rename command for batch renaming, and mv with sudo for folders requiring elevated permissions. mv (move) command : It is the ...
🌐
nixCraft
cyberciti.biz › nixcraft › howto › centos › linux rename folder command
Linux Rename Folder Command - nixCraft
January 21, 2020 - I am a new Linux system user. How do I rename a folder on Linux operating system using the command line? How can I rename a directory via the bash command line option? You need to use the mv command to rename and move files and directories/folders.
🌐
Super User
superuser.com › questions › 1617254 › rename-folder-recursively-with-specific-name
linux - Rename folder recursively with specific name - Super User
January 13, 2021 - find / -depth -type d -name VT_GH -print | awk '{ print "mv -n -T \"" $0 "\" \"`dirname '"'"'" $0 "'"'"'`/VT GH\"" }' | bash · Let's break it into pieces. The first command: ... outputs a list of pathnames of all folders called VT_GH on your system. The -depth parameter is important here, as it causes the paths to be listed in order from deepest ones. This will be important later when we rename them, because if we have a path like /some/dir/VT_GH/VT_GH, then the lower-level VT_GH must be renamed before the upper-level one, otherwise the path to the lower-level one will cease to exist.
🌐
Hivelocity
hivelocity.net › home › knowledge base articles › how to move, copy, and rename a directory in linux
How to Move, Copy, and Rename a Directory in Linux
January 5, 2024 - So, to rename /directory1 to /directory_new, you would use the command: mv /directory1 /directory_new *Note: If “/directory_new” already exists, then this command will move the contents of /directory1 to /directory_new.
Find elsewhere
Top answer
1 of 3
1

This is an entirely trivial one-liner; for example:

rename_pieces smali/com/FOLDER/SUB-FOLDER smali/com/NEW/SUBNEW

...assuming, of course, that you run an appropriate function definition first. If you only want to rename SUB-FOLDER, after creating NEW-NAME should it not exist, that would look like:

rename_pieces() {
  local old=2
  [[ $new = */* ]] && mkdir -p -- "${new%/*}"
  mv -T -- "$old" "$new"
}

...and this is much more likely to be the behavior you really do want than any other interpretation, insofar as it leaves contents of FOLDER other than SUB-FOLDER alone with its original name.


By contrast, if you really want to rename both directories, that gets a lot more interesting. If we have a guarantee that both source and destination are at the same depth, this might look something like:

log_or_run() {
  if [[ $log_only ]]; then   # just log what we would run
    printf '%q ' "$@" >&2    # use printf %q to generate a safely-escaped version
    printf '\n' >&2          # ...and terminate with a newline.
  else
    "$@"                     # actually run the command
  fi
}
rename_pieces() {
  local old=2 common
  while [[ ${old%%/*} = "${new%%/*}" ]]; do
    common+=/"${old%%/*}"
    old=${old#*/}; new=${new#*/}
  done
  while [[ $old && $new ]]; do
    log_or_run mv -T -- "${common#/}/${old%%/*}" "${common#/}/${new%%/*}"; echo
    common+=/"${new%%/*}"
    [[ $old = */* && $new = */* ]] || return
    old=${old#*/}; new=${new#*/}
  done
}

Whereafter:

log_only=1 rename_pieces smali/com/FOLDER/SUB-FOLDER smali/com/NEW/SUBNEW

...emits on output:

mv -T -- smali/com/FOLDER smali/com/NEW
mv -T -- smali/com/NEW/SUB-FOLDER smali/com/NEW/SUBNEW

...and doing likewise without log_only=1 actually runs those commands.

2 of 3
0

Can't think of anything more elegant, but the following should work: find . -path '.*com/FOLDER/SUBFOLDER' | awk '{new_dir=$1; sub("/FOLDER/SUBFOLDER","/NEW/SUBNEW", $new_dir); printf("mkdir -p %s\nmv %s %s\n", $new_dir, $1, $new_dir);}' > cmds

That gets all the required mv commands in the cmds file. Verify it is correct, and then execute using sh cmds. Although this isn't the most fun, I like this approach when dealing with important data because it is always better to verify what you're going to run.

Edit: Fixed the issue pointed out in comments about the script not creating the directory if it does not exist.

🌐
nixCraft
cyberciti.biz › nixcraft › howto › linux › linux/unix: rename directory with spaces in name
Linux/Unix: Rename Directory With Spaces In Name - nixCraft
February 26, 2025 - Open the Terminal and type the following command to rename “My Personal Files” to “Files”: $ mv My\ Personal\ Files Files $ ls -l · WARNING! These examples may result in unexpected outputs if executed. Make a backup before making bulk changes to the dir/folder structure.
🌐
ManageEngine
manageengine.com › home › knowledge base › linux › how to rename a directory in linux
How to rename a directory in Linux
May 26, 2025 - For simpler tasks, the mv command or GUI file managers are quick and efficient. Bash scripts and the mmv command are ideal for large-scale or automated renaming tasks, while the find command is powerful for conditional renaming based on specific ...
🌐
MonoVM
monovm.com › 🐧 linux 🐧 › linux rename directory command | rename folder in linux
Linux Rename Directory Command | Rename Folder in Linux
March 5, 2023 - You can do it effortlessly if you want to rename a single directory with linux rename folder command. But if you want to rename multiple directories simultaneously, you have to face some challenges if you are a new Linux user. The “mv” command will help move only one directory at a time. Still, you can use the “mv” command in conjunction with other commands that will solve the purpose of renaming multiple directories at the same time. You can consider the following example, where we have used the bash “for” loop for appending the current date to all the directory names that we are considering renaming and are present within the current working directory.
🌐
Linuxize
linuxize.com › home › linux commands › how to rename directories in linux
How to Rename Directories in Linux | Linuxize
3 weeks ago - Rename single or multiple directories from the Linux command line using the mv command, for loops, and the rename utility.
🌐
Quora
quora.com › How-do-you-change-the-name-of-folders-recursively-in-Bash-bash-rename-Linux
How to change the name of folders recursively in Bash (bash, rename, Linux) - Quora
Answer (1 of 2): That’s not something you would normally do, and I very much doubt that there’s a command to do it. If I actually needed to do it, I’d first generate a list of directory names and write them to a file: [code]find . -type d -print > /tmp/list [/code]Then I’d edit that file and ch...
🌐
Wikihow
wikihow.com › computers and electronics › operating systems › linux › how to rename a directory in linux: 5 ways (with examples)
How to Rename a Directory in Linux: 5 Ways (with Examples)
November 22, 2024 - Do you want to change the name of a directory or folder in Linux? To rename a directory at the command line, you can use the "mv" command. Most Linux distributions also come with file browsers you can use to rename directories.
🌐
PhoenixNAP
phoenixnap.com › home › kb › sysadmin › how to rename a directory in linux
How to Rename a Directory in Linux {6 Options}
May 7, 2024 - To edit multiple folders, select the folders and press F2 to perform batch renaming. 5. Enter a new name and press Enter or click Rename to confirm the name change for a single directory.
🌐
Medium
medium.com › @tpointtechblog › rename-a-folder-in-linux-commands-and-examples-03828a237b64
Rename a Folder in Linux: Commands and Examples | by Tpoint Tech Blog | Medium
September 28, 2025 - Backup Important Data: Before batch renaming, back up to avoid accidental data loss. Test on Sample Folders: Practice commands on test folders before applying them to critical directories.
🌐
How-To Geek
howtogeek.com › home › linux › how to rename a directory on linux
How to Rename a Directory on Linux
November 6, 2023 - The "mv" command is the simplest and most commonly used method for renaming directories in Linux.
🌐
devconnected
devconnected.com › home › linux system administration › how to rename a directory on linux
How To Rename a Directory on Linux – devconnected
December 15, 2019 - Now that you know where your directory is, you can rename it by using the “execdir” option and the “mv” command. $ find . -depth -type d -name temp -execdir mv {} directory \; As described in our previous tutorials, the Bash scripting ...