How to Rename a Directory in Linux: Ubuntu, Fedora, + More

Download Article
Rename a directory using the terminal or file browser in Linux
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 How.com.vn 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

Download Article
  1. How.com.vn English: Step 1 Press Ctrl+Alt+T to open the Terminal icon.
    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 press the keyboard shortcut, "Ctrl + Alt + T" to open the Terminal.
  2. How.com.vn English: Step 2 Navigate to the directory that contains the directory you want to change.
    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. How.com.vn English: Step 3 Type mv in the Terminal.
    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. How.com.vn English: Step 4 Add any optional commands.
    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.
  5. How.com.vn English: Step 5 Add the name of the directory you want to change.
    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. How.com.vn English: Step 6 Add the new name of the directory and press ↵ Enter.
    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

Download Article
  1. How.com.vn English: Step 1 Open your File Browser app.
    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. How.com.vn English: Step 2 Right-click the folder you want to rename.
    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. How.com.vn English: Step 3 Click Rename.
    It should be in the menu that appears when you right-click the file.
  4. How.com.vn English: Step 4 Enter a new name and click Rename.
    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

Download Article
  1. How.com.vn English: Step 1 Press Ctrl+Alt+T to open the Terminal icon.
    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 press the keyboard shortcut "Ctrl + Alt + T" to open the Terminal.
  2. Step 2 Install the "Rename" command.
    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
  3. How.com.vn English: Step 3 Navigate to the directory that contains the directory you want to change.
    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.
  4. How.com.vn English: Step 4 Type rename in the Terminal.
    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.
  5. How.com.vn English: Step 5 Add any optional commands.
    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.
  6. How.com.vn English: Step 6 Type the name of the directory you want to change.
    Enter the name of the old directory next in the command line.
  7. How.com.vn English: Step 7 Type the new name for the directory.
    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>/'.
  8. How.com.vn English: Step 8 Add the name of the folder you want to change and press ↵ Enter.
    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.
  9. Advertisement
Method 4
Method 4 of 5:

Renaming Multiple Directories

Download Article
  1. How.com.vn English: Step 1 Press Ctrl+Alt+T to open the Terminal icon.
    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 press the keyboard shortcut "Ctrl + Alt + T" to open the Terminal.
  2. How.com.vn English: Step 2 Create a new shell file.
    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".
  3. How.com.vn English: Step 3 Type for d in *; do on the first line.
    This creates a loop in which the script will check all files within the directory the shell file is in.
  4. Step 4 Type if [ -d "$d" ]; then on the second line.
    This line of code checks if a file is a directory.
  5. Step 5 Type mv -- "$d" "<new_directory_names>" on the third line.
    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.
  6. How.com.vn English: Step 6 Type fi on the fourth line.
    This closes the loop and returns to the beginning.
  7. How.com.vn English: Step 7 Type done on the fifth line.
    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)"  fidone
  8. How.com.vn English: Step 8 Save the shell file and exit VIM.
    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.
  9. How.com.vn English: Step 9 Make the shell file executable.
    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.
  10. How.com.vn English: Step 10 Execute the script.
    To do so, simply type the name of the shell file (i.e, ./change_directories.sh and press Enter.
  11. Advertisement


Method 5
Method 5 of 5:

Finding and Changing a Directory Name

Download Article
  1. How.com.vn English: Step 1 Press Ctrl+Alt+T to open the Terminal icon.
    If you're not sure where the directory is that you want to rename, you can use the find command to find it. Start by opening a Terminal window.
  2. How.com.vn English: Step 2 Type find .
    -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.
  3. How.com.vn English: Step 3 Add -execdir mv {} <new_directory_name> \; and press Enter.
    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 \;
  4. Advertisement

Expert Q&A

Search
Add New Question
  • Question
    How do I count files in a Linux directory?
    How.com.vn English: Blain Gunter
    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.
    How.com.vn English: Blain Gunter
    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
200 characters left
Include your email address to get a message when this question is answered.
Submit
      Advertisement

      Tips


      Submit a Tip
      All tip submissions are carefully reviewed before being published
      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

      How.com.vn English: Blain Gunter
      Written by:
      Computer Repair Specialist
      This article was written by Blain Gunter and by How.com.vn staff writer, Travis Boylls. 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. This article has been viewed 7,927 times.
      How helpful is this?
      Co-authors: 5
      Updated: February 1, 2024
      Views: 7,927
      Categories: Linux
      Article SummaryX

      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 7,927 times.

      Is this article up to date?

      ⚠️ Disclaimer:

      Content from Wiki How English language website. Text is available under the Creative Commons Attribution-Share Alike License; additional terms may apply.
      Wiki How does not encourage the violation of any laws, and cannot be responsible for any violations of such laws, should you link to this domain, or use, reproduce, or republish the information contained herein.

      Notices:
      • - A few of these subjects are frequently censored by educational, governmental, corporate, parental and other filtering schemes.
      • - Some articles may contain names, images, artworks or descriptions of events that some cultures restrict access to
      • - Please note: Wiki How does not give you opinion about the law, or advice about medical. If you need specific advice (for example, medical, legal, financial or risk management), please seek a professional who is licensed or knowledgeable in that area.
      • - Readers should not judge the importance of topics based on their coverage on Wiki How, nor think a topic is important just because it is the subject of a Wiki article.

      Advertisement