PDF download Download Article
Rename a directory using the terminal or file browser in Linux
PDF download Download Article

Do you want to change the name of a directory or folder in Linux ? Most Linux distributions have a graphical user interface with a file browser application that you can use to rename files and folders. However, you can change the name of a directory or folder in all versions of Linux using a few basic Terminal commands. This wikiHow article teaches you how to rename a directory in all versions of Linux.

Things You Should Know

  • The "mv" command can be used to rename directories and other files.
  • The syntax to change a directory name is "mv <options> <old_directory_name> <new_directory_name>."
  • You can also use the "rename" command or the file browser to change a directory name.
Method 1
Method 1 of 5:

Using the "mv" Command

PDF download Download Article
  1. 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
  2. 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.
  3. 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 any 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.
  4. 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.
  5. 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 .
  6. 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, it's the app called "Files" and it has an icon that resembles 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. Simply 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. Enter one of the following commands below and press Enter to install "Rename" on your Linux distribution:
    • Debian/Ubuntu : sudo apt install rename
    • Fedora : sudo yum install prename
    • Arch Linux : sudo pacman -S install rename
  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 .
  3. Now that you have "Rename" installed, you can use the "rename" command to change directory names. It works very similarly to the "mv" command. Type "rename" in the Terminal to get started.
    • Don't press Enter just yet. There is still more of the command that needs to be entered.
  4. This is not required but you can add any options you want to the "rename" command. To add an option, add a space followed by one of the following options:
    • -v : This command will add information about actions taking place.
    • -n . This command will not take any action. You can use it to test your command to see if it works before changing the command for real.
    • -f : This command will force overwrite any directories without prompting you.
  5. Enter the name of the old directory next in the command line.
  6. Add a space and type the name you want to change the directory to.
    • Alternatively, you can search for multiple folders by entering patterns instead of the name of a folder. To do so, you would type 's/<pattern_name>/<new_name>/' instead of the old and new names for the directory. For example, if you have a bunch of folders named "Untitled", you could type 's/Untitled/<new_name>/' to search for all folders with the name "Untitled". If you want to search for all folders that begin with a capital letter, you would type 's/A-Z/<new_name>/' .
  7. With this command, you have to tell Linux which folder you want to change the name for. Enter the name of the folder you want to change at the end and press Enter .
    • The entire command should look something like rename -v Untitled New_folder_Name Untitled and press Enter .
  8. 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 creating a shell script . To do so, type vim followed by the script name. Then add the .sh file extension at the end, and press Enter . This will create a new shell file and open it within the VIM text editor.
    • For example, you can type {{kbd|vim change_directories.sh} and press enter to create a new shell file called "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 . Then type :wq and press Enter . This will save the file and exit VIM. You will be returned to the standard Terminal interface.
  8. To do so, 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. -depth -type d -name <directory_name>. If you are not sure where the directory you want to rename is located, you can use this command 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,128 times.

      Is this article up to date?

      Advertisement