Anyone who’s worked with Ubuntu systems 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.
Contrary to popular belief, there are multiple ways to rename a directory in Ubuntu 20.04. In this post, we’ll explore multiple ways, you can use to rename directories in Ubuntu. Let’s begin:
Through 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.
Renaming multiple folders through 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”.
Type 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.
Renaming 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.
Type: $ mv Temp Directory
Once you press enter, you won’t get any prompt.
Now type “ls” again to view the changes.
Renaming using the “rename” command
As discussed, 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:
Using the above syntax, you can rename 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.
Let’s explore the main options specified in the syntax.
Options
- -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 ‘;’.
Example (changing file extension)
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.
Renaming using a bash script
If you require updating multiple files, you can write a bash script to achieve your desired result.
Continuing from our old 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.
Creating a bash script may look like a tedious undertaking at the start, but once you have these scripts prepared per your requirements, running them can save you a lot of time in the long term.