How To Rename Files & Directories in Linux
In this tutorial, you will learn how to rename your files and directories in Linux using the terminal with the mv and rename commands.
If you want to change the name of a single file, the best option is graphically, but sometimes we can only do it from the terminal, or we want to change many files, and this last one is not very effective to do so.
In this tutorial we will see how to rename files or directories in Linux using the terminal, we will see how to do it using the mv command and the rename command.
Following these steps, you will see that it is not complex at all.
The practice is the important thing!
MV command to rename file or Linux directory
To rename a simple file or directory we use the following:
mv current_file_name new_file_name
If we want to change the extension of several files at the same time, because they have one that we do not want, instead of doing it one by one, we can execute:
mv *.CurrentExtension *.extensionNew
Maybe with some examples, it looks better.
Examples
We want to change the name of the Linux folder to Linuxcode:
mv Linux Linuxcode
We want to change the extensions of all the files in the directory we are in (the change will be from png to jpg):
mv *.png *.jpg
This way of renaming is very easy to use, but the next option that we will see is more powerful.
Command RENAME to rename Linux files
To see the use of the rename command, we will do it directly with examples, because it will be better understood.
If we want to change from extension .txt to .bak:
rename 's/\.txt$/\.bak/' *.txt
If we’re going to remove the 2016 text from some photos with jpg extension:
rename 's/ 2016//g' *.jpg
If we’re going to convert all uppercase letters to all names:
rename y/A-Z/a-z/ *.extension_files
If you want it the other way around, it is to pass lower case to uppercase merely change y/A-Z/a-z/ to y/a-z/A-Z/.
The way to do it in these cases (in Ubuntu):
sudo rename 's/\.db$/\.bak/' *.db