How to Rename a Directory in Linux: Ubuntu, Fedora, + More
How to Rename a Directory in Linux: Ubuntu, Fedora, + More
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.

Using the "mv" Command

Press Ctrl+Alt+T to open the Terminal Mac Terminal. 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.

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.

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.

Add any optional commands. This is not required but you can add any options you want to the "mv" command. 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.

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.

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.

Using the File Browser

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.

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.

Click Rename. It should be in the menu that appears when you right-click the file.

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.

Using the "Rename" Command

Press Ctrl+Alt+T to open the Terminal Mac Terminal. 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.

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

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.

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.

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.

Type the name of the directory you want to change. Enter the name of the old directory next in the command line.

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///' 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//' 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//'.

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.

Renaming Multiple Directories

Press Ctrl+Alt+T to open the Terminal Mac Terminal. 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.

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".

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.

Type if [ -d "$d" ]; then on the second line. This line of code checks if a file is a directory.

Type mv -- "$d" "" 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}_$(") 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.

Type fi on the fourth line. This closes the loop and returns to the beginning.

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)" fi done

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.

Make the shell file executable. To do so, chmod +X 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.

Execute the script. To do so, simply type the name of the shell file (i.e, ./change_directories.sh and press Enter.

Finding and Changing a Directory Name

Press Ctrl+Alt+T to open the Terminal Mac Terminal. 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.

Type find . -depth -type d -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 "" with. Don't press Enter just yet. You still need to add the part of the command that will change the directory name.

Add -execdir mv {} \; 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 \;

What's your reaction?

Comments

https://ugara.net/assets/images/user-avatar-s.jpg

0 comment

Write the first comment for this!