Rename Files and Directories in Ubuntu 20.04 - How to do it ?

Any Ubuntu Linux systems user knows how important it is to keep your directories clean and structured, for effective and efficient access to them. Sometimes, you may need to create temp directories that might need renaming later, or you might want to rename a directory once you are done with a project.

Here at LinuxAPT, as part of our Server Management Services, we regularly help our Customers to perform related Linux files and directories queries.

In this context, you will see that there are multiple ways to rename a directory in Ubuntu 20.04.


1. Renaming a directory in Ubuntu via Nautilus File Explorer

Similar to its mainstream cousin, Ubuntu now features a very user-friendly GUI, which has made it relatively easier to use compared to its earlier iteration. Like Windows, you get a file explorer in Ubuntu as well. Using it you can navigate to your desired directory, right-click on it, click on "Rename" and enter your desired name for the directory.


2. Renaming multiple folders via file explorer

If you require to rename multiple folders into a sequence, the file explorer in Ubuntu can help you greatly.

Start by selecting all of the folders. Right-click and click on "Rename".

Then, Enter the consistent part of the name in the textbox.

Click on the "Add" button in front of the textbox, to select your desired sequence.

Once you apply the sequence, you can sort the name by ascending and descending order.

Click on rename in the upper right corner of the dialog box to apply the changes.


3. Renaming a directory using the "mv" command

There is no built-in rename command in Ubuntu.

You can also use the "mv" command in the terminal to rename a file or directory. Open up the terminal by using the shortcut "Alt + Ctrl + T". Once you have the terminal open, type the following command:

$ mv <original_name> <new_name>

For instance, if you want to rename a directory named "Temp", located in your home directory, to "Directory" in the same location, you can use the following command:

Start by using the "ls" command to view the current contents of your current location in the terminal:

$ ls

Next, enter the command:

$ mv Temp Directory

Once you press enter, you won't get any prompt.

Now type "ls" again to view the changes.


4. Renaming using the "rename" command

As earlier mentioned, the rename command isn't built into Ubuntu. Just a heads up, it's more advanced than the "mv" command as it requires you to have at least a basic understanding of regular expressions.

You can install the command by typing the following command into your terminal:

$ sudo apt install rename

To use the command, you need to understand the syntax below:

$ rename

This command can rename a single or multiple files to the result specified as the first argument.

The Perl argument "perlexpr" is expected to modify the $_string part using Perl for at least some of the names specified. If a name is not updated using this command, running this command again will not update it. If you don’t specify any file or directory names in this command, it will ask you for the names through regular input on the terminal.


Main Options specified in the Rename syntax:

  • -V: used to show the version number.
  • -h: print OPTIONS and SYNOPSIS.
  • -f: forcing existing files to be overwritten.
  • -n: (no action) prints the names of the files to be renamed but doesn't rename.
  • -e: code for acting on file names. It can be repeated to build up code like in perl, but if there is no -e, the first argument in the command will be used as code.
  • -E: similar to -e but it is terminated using ';'.


An Example (Modifying file extension)

When working with code or text files, you might need to change the file extension of your code file frequently. Let's consider a "file.txt" containing our code, and we need to convert it to a C/C++ file to compile it. Open the terminal where your file is located and type the following:

$ rename 's/\.txt/\.c/' file.txt

You can run "ls" to see the results.


5. Renaming a directory or file using a bash script

If you require updating multiple files, you can write a bash script to achieve your desired result.

As with the previous example, if we have multiple "*.txt" files that need to be converted to C/C++ ".c" files. You can write the following bash script to convert the whole series.

You can create the bash script by opening up the text editor and typing in the following code:

for file in *.txt; do
mv -- "${file}" "${file%.txt}.c"
done

Now save the file in the same directory as your text files and name it anything e.g., "fileRename.sh". Following that go into the terminal and run "ls" to confirm the availability of all the files.

Run your bash script using the command below:

$ bash fileRename.sh

Now running "ls" will show you that all the file extensions have changed.


[Need assistance in fixing Linux System issues? We can help you. ]

This article covers different methods to Rename Files and Directories Using Linux Terminal. We can rename files and directories with rename and mv commands in the Linux Terminal. The mv command can only rename one file at a time, but the rename command can rename multiple files simultaneously.


How to Rename Files and Directories Using the mv Command ?

The mv command can rename files and directories. It is also used to move files and directories from one location to another.

Syntax of mv Command:

$ mv [OPTIONS] source destination

The source can be one or more files or directories, and the destination is always a single file or directory.

Related Posts