PDF download Download Article
Easily rename directories with terminal commands or a file manager
PDF download Download Article

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. Read on to learn how to rename a directory in any Linux version, including Ubuntu, Fedora, and more.

Renaming a Directory in Linux

To rename a directory in the terminal, use the mv command. The syntax is "mv <old_directory_name> <new_directory_name>". You can also right-click a directory in your file browser and select Rename , or use the rename command.

Method 1
Method 1 of 5:

Using the "mv" Command

PDF download Download Article
  1. On most Linux distributions, the Terminal has an icon that resembles a black screen with a white cursor. You can click the Terminal icon in your Apps menu or use the keyboard shortcut "Ctrl + Alt + T" to open the Terminal.
  2. This command is easier if you are in the parent directory of the one you want to rename.
    • To change directories, type cd followed by the path, then press Enter .
    • For example, if you want to rename a directory called "Important" in your Documents directory, you'd enter cd /home/yourusername/Documents and press Enter .
    Advertisement
  3. In addition to using mv to move files and directories , you can use this command to rename directories.
    • Do not press Enter just yet. There is still more you need to type to complete the command.
  4. This is not required, but you can add any options you want to the "mv" command. [1] To add an option, add a space followed by one of the following options:
    • --backup : This will create a backup of all the files being moved.
    • -f : This option will force an overwrite of files or folders without a prompt.
    • -i : This option will prompt you before overwriting any files or folders.
    • -v : This option will explain everything that is being done by the command.
  5. If you are currently in the directory that contains the directory you want to change, you only need to enter the name of the directory.
    • To view all directories and folders in your current directory, type ls -la and press Enter . This will show all folders and hidden folders as well as which user has permission to access these folders.
  6. To do so, add a space after the directory name you want to change. Then press Enter to change the directory name.
    • If you are not in the directory that contains the directory that you want to change, you will need to add the path to where you want to save the new directory name. For example, /home/user/new_directory . You can also do this to change the location of the directory.
    • The entire command should look something like the following: mv -v /home/username/temp_dir /home/username/new_dir .
  7. Advertisement
Method 2
Method 2 of 5:

Using the File Browser

PDF download Download Article
  1. This will be different with each Linux distribution. On Ubuntu and Fedora, there is an app called "Files," with an icon resembling a file cabinet drawer. Click the Files app to open Files.
  2. You can rename folders using the graphical user interface the same way you would using Windows or macOS. Right-click the folder you want to rename.
  3. It should be in the menu that appears when you right-click the file.
  4. A box will appear with a field you can use to enter the new name for your folder. Enter the new name and click Rename . This will instantly rename the folder.
  5. Advertisement
Method 3
Method 3 of 5:

Using the "Rename" Command

PDF download Download Article
  1. The Rename command is not available on all Linux distributions, but you can install the package easily. Rename is a good option if you want to rename multiple directories at once, as you can use regular expressions or patterns without writing a script . Open a terminal window, then run the following command for your distribution:
    • Debian/Ubuntu : sudo apt install rename
    • Fedora & RedHat : sudo dnf install prename (note the 'p' at the beginning, which stands for "perl")
    • Arch Linux & Manjaro : sudo pacman -Syu perl-rename
  2. To change directories, type cd followed by the path, then press Enter .
    • For example, if you want to rename a directory called "Important" in your Documents directory, you'd enter cd /home/yourusername/Documents and press Enter .
    • Don't press Enter just yet. There is still more of the command that needs to be entered.
  3. The rename syntax will be slightly different depending on whether you want to rename a single directory or multiple directories. If you're using Fedora or RedHat, use prename . On Arch or Manjaro, use perl-rename .
    • Syntax: rename 's/[pattern]/[replacement]/' [current_name]
      • Replace "[pattern]" with the name or (or string of characters from) of the directory or directories you want to rename.
      • Replace "[replacement]" with the text you want to rename it with. To remove the string of characters from the directory/directories, leave this blank (e.g., rename 's/[pattern]//' [current_name] ).
      • Replace "[current_name]" with the name of the directory you want to rename. You can use wildcards here.
      • Keep in mind that rename also works on individual files. If there are files in the directory that match the string you're searching for, they will also be renamed.
    • Example: If you have a bunch of folders that end with "_backup" and want to remove the "_backup" from all of their names, you can use rename 's/_backup//' *_backup .
    • Example: Let's say you have a bunch of directories that end with "_backup," but you only want to remove "_backup" from the directories that begin with the word "files." In this case, you'd use rename 's/_backup//' files*_backup
    • Example: In this example, we'll change all directory names that contain "2023" with "2024": rename 's/2023/2024/' *2023*
  4. This is not required, but you can add any options you want to the "rename" command. For example:
    • rename -v : Adds the "verbose" option, which prints successfully renamed file and directory names to the screen.
    • rename -n . Use this if you don't want to rename the directory just yet, but instead want to see which files and directories would be renamed with the command.
    • rename -f : This will force-overwrite existing files and directories without prompting you.
  5. Advertisement
Method 4
Method 4 of 5:

Renaming Multiple Directories

PDF download Download Article
  1. The "mv" command can be used to rename a single directory at a time. However, if you want to rename multiple directories, you can do so by writing a shell script . To create a new script, type vim script_name.sh at the prompt, where script_name.sh is the name you're giving your script. his will create a new file and open it in the Vim text editor.
    • For example, vim change_directories.sh .
  2. This creates a loop in which the script will check all files within the directory the shell file is in.
  3. This line of code checks if a file is a directory.
  4. This will change all directories in the current directory to the new name with a number at the end of it.
    • Alternatively, you can add a new name to the end of each directory instead of changing it completely. To do so, mv --"$d" "${d}_$(<new_directory_name>") instead. For example, if you want to add the date to the end of each directory, you could type mv -- "$d" "${d}_$(date +%Y%m%d)" on the third line.
  5. This closes the loop and returns to the beginning.
  6. This ends the script. The entire script should look something like the following:
     for 
    d in * ; 
     do 
     if 
     [ 
    -d " 
     $d 
     " 
     ] 
     ; 
     then 
    mv -- " 
     $d 
     " 
     " 
     ${ 
     d 
     } 
     _ 
     $( 
    date +%Y%m%d ) 
     " 
     fi 
     done 
    
  7. To do so, press Esc , type :wq , and press Enter . You will be returned to the prompt.
  8. To do so, type chmod +x <filename> and press Enter . For example, if you named your shell file "change_directories.sh", you would type chmod +x change_directories.sh and press Enter .
    • Make sure you are in the same directory as the shell file. To change directories, type cd followed by the path of the shell file (i.e., cd /home/user/ and press Enter .
  9. To do so, simply type the name of the shell file (i.e., ./change_directories.sh and press Enter .
  10. Advertisement


Method 5
Method 5 of 5:

Finding and Changing a Directory Name

PDF download Download Article
  1. In a terminal, type find . -depth -type d -name <directory_name> and press Enter if you are not sure where the directory you want to rename is located and need to find it . This command will search your entire file system for a directory with the name you replace "<directory_name>" with.
    • Don't press Enter just yet. You still need to add the part of the command that will change the directory name.
  2. This adds the "mv" command to change the directory name once it is located.
    • The entire command should look something like find . -depth -type d -name temp_directory -execdir mv {} new_directory_name \;
  3. Advertisement

Expert Q&A

Search
Add New Question
  • Question
    How do I count files in a Linux directory?
    Blain Gunter
    Computer Repair Specialist
    Blain Gunter is a Computer Repair Specialist and small business owner based in Bakersfield, California. He was first introduced to computers at the age of five and has over twenty years of experience in his field. He is both an IT consultant and computer repair technician and takes pride in his ability to troubleshoot anything. He works with hardware, software, Windows, macOS, GNU/Linux, and even vintage electronics.
    Computer Repair Specialist
    Expert Answer
    To precisely count files in a Linux directory, actively employ commands such as "ls" and "tree." These commands are valuable tools for not only listing but also tallying the files within a directory and its subdirectories. Ensuring familiarity with these commands is crucial for effective file management in the Linux environment.
Ask a Question
      Advertisement

      Tips


      Submit a Tip
      All tip submissions are carefully reviewed before being published
      Name
      Please provide your name and last initial
      Thanks for submitting a tip for review!
      Advertisement

      Expert Interview

      Thanks for reading our article! If you’d like to learn more about troubleshooting computers and printers, check out our in-depth interview with Blain Gunter .

      About This Article

      Article Summary X

      1. Open the Terminal.
      2. Navigate to folder that contains the directory you want to change.
      3. Type "mv " and press Enter .

      Did this summary help you?
      Thanks to all authors for creating a page that has been read 11,776 times.

      Is this article up to date?

      Advertisement