Linux rename directory command

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

This article was co-authored by wikiHow staff writer, Travis Boylls. Travis Boylls is a Technology Writer and Editor for wikiHow. Travis has experience writing technology-related articles, providing software customer service, and in graphic design. He specializes in Windows, macOS, Android, iOS, and Linux platforms. He studied graphic design at Pikes Peak Community College.

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.

  • The «mv» command can be used to rename directories and other files.
  • The syntax to change a directory name is «mv .»
  • You can also use the «rename» command or the file browser to change a directory name.

Using the «mv» Command

Image titled 13852336 1

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.

Image titled 13852336 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.

Image titled 13852336 3

  • Do not press Enter just yet. There is still more you need to type to complete the command.

Image titled 13852336 4

  • —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.

Image titled 13852336 5

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

Image titled 13852336 6

  • 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

Image titled 13852336 7

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.

Image titled 13852336 8

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.

Читайте также:  Как установить postgresql в linux

Image titled 13852336 9

Image titled 13852336 10

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

Image titled 13852336 11

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.

Image titled 13852336 12

  • Debian/Ubuntu: sudo apt install rename
  • Fedora: sudo yum install prename
  • Arch Linux: sudo pacman -S install rename

Image titled 13852336 13

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

Image titled 13852336 14

Image titled 13852336 15

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

Image titled 13852336 16

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

Image titled 13852336 17

  • 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//’ .

Image titled 13852336 18

  • The entire command should look something like rename -v Untitled New_folder_Name Untitled and press Enter.

Renaming Multiple Directories

Image titled 13852336 19

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.

Image titled 13852336 20

  • For example, you can type and press enter to create a new shell file called «change_directories.sh».

Image titled 13852336 21

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.

Image titled 13852336 22

Image titled 13852336 23

  • Alternatively, you can add a new name to the end of each directory instead of changing it completely. To do so, mv —«$d» «$_$(«) instead. For example, if you want to add the date to the end of each directory, you could type mv — «$d» «$_$(date +%Y%m%d)» on the third line.

Image titled 13852336 24

Image titled 13852336 25

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 

Image titled 13852336 26

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.

Image titled 13852336 27

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

Image titled 13852336 28

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

Image titled 13852336 29

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.

Image titled 13852336 30

  • Don’t press Enter just yet. You still need to add the part of the command that will change the directory name.

Image titled 13852336 31

  • The entire command should look something like find . -depth -type d -name temp_directory -execdir mv <> new_directory_name \;

Expert Q&A

You Might Also Like

Install Google Chrome Using Terminal on Linux

Can Linux Run Exe

Can Linux Run .exe Files? How to Run Windows Software on Linux

Add or Change the Default Gateway in Linux

Open Ports in Linux Server Firewall

How to Open Linux Firewall Ports: Ubuntu, Debian, & More

Take a Screenshot in Linux

Become Root in Linux

Execute INSTALL.sh Files in Linux Using Terminal

How to Run an INSTALL.sh Script on Linux in 4 Easy Steps

Ping in Linux

Use Ping in Linux: Tutorial, Examples, & Interpreting Results

Boot Linux from a USB on Windows 10

Delete Read Only Files in Linux

How to Delete Read-Only Files in Linux

Use Wine on Linux

Run Files in Linux

Install Linux

How to Install Linux on Your Computer

Install Puppy Linux

How to Install Puppy Linux

Источник

Linux Rename Directory Command | Rename Folder in Linux

Rename directory Linux is a simple task that can be accomplished using the “mv” command from the command line. To learn how to rename folder in Linux, read this post.

Linux Rename Directory Command | Rename Folder in Linux

List of content you will read in this article:

Linux Rename Directory Command is similar to renaming files in a Linux system. It is one of the simple and commonly performed operations on the Linux system. You can rename directory in linux from the command-line commands or use the GUI file manager.

In this article, we will focus on various commands that will help to Rename Folder in Linuxy, along with various options that will somehow affect the working of the rename command.

What are directories?

A directory is a location on a computer where files and other folders are stored. In Linux, directories are also called «folders.» You can think of a directory as a folder in Windows or macOS.

You are placed in your home directory when opening a terminal window. This is the default location for many users when they start using Linux. Your home directory will be filled with files and folders that store your settings and preferences.

To view the contents of your home directory, type ls at the command prompt. This will list all of the files and folders in your current location if you want to see what is inside a particular folder, type ls followed by the name of the folder. For example, type ls Desktop to see what is inside the Desktop folder.

To navigate to different directories, use the cd command. For example, to change to the Documents directory, type cd Documents. To go up one level in the directory structure (for example, from the Documents directory back to your home directory), type

Why would I want to rename directories?

There are several reasons you might want to rename directories. For example, you may want to change the name of a directory to reflect the contents of that directory. Alternatively, you may need to rename a directory to avoid confusion with another similarly named directory. Additionally, renaming directories can help keep your file system organized and tidy.

  • Renaming folders in Linux offers a simple command, “mv” that will solve the purpose of renaming the directories. The “mv” command can be used for renaming and moving the file from one source location to the destination location.

You can follow the below-mentioned syntax for the “mv” command.

mv [OPTIONS] source destination

If the dir_name2 already exists, then the dir_name1 is moved to the existing dir_name2 directory.

  • To rename the folder that is not present in your current working directory, you need to specify the path to that directory while renaming it. You can consider the following example.

mv /home/user/dir_name1 /home/user/dir_name2

Rename Multiple Directories using “mv” command

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.

Now we will see how the above bash code will process the directories one by one to rename them. The first line in the above code will iterate through all the files. The second line will check if the specified file is a directory. Then the third line will append the current date to the end of the directory.

  • You can use the “find” command if you want a less complex solution. Consider the following example.

find . -mindepth 1 -prune -type d -exec sh -c ‘d=»<>«; mv — «$d» «$_$(date +%Y%m%d)»‘ \;

The find command will pass all the directories to the “mv” command one by one with the help of the “-exec” option. The string<> will take the directory’s name currently being processed.

Renaming multiple directories using the rename command

Rename command is considered more powerful than the mv command as it will require the basic knowledge of the regular expressions and make your complex work easier.

The example below will show how you can replace the spaces in the name of the directories present in the current working directory with an underscore.

find . -mindepth 1 -prune -type d | rename ‘y/ /_/’

How can I find out the path of my home directory?

Assuming you are using the bash shell, the command to find out the path of your home directory is:

This will print out the path of your home directory to the terminal.

Conclusion

Well, we have learned how to rename a directory in Linux with simple tasks that can be accomplished using the “mv” command from the command line. But if you want to rename multiple directories simultaneously, you might find it difficult as you need prior knowledge of regular expressions and bash scripting.

This article focuses on two commands for renaming the directories- the “mv” command and the “rename” command.

People are also reading:

Источник

Оцените статью
Adblock
detector