In general, renaming files or managing them is one of the most basic tasks an OS needs to excel at. In Linux, you can do this either via the terminal or the GUI file manager.
In this article, we’re talking about three different ways you could rename files in Linux — two follow terminal command while the third one uses the GUI.
Renaming files using the terminal
When using the terminal, you can use either of the following two commands to rename your files.
The mv command was built to move files around, but as a side effect, you can also use it to rename files. For heavy lifting, however, the rename command comes in handy as its sole purpose is, you guessed it, renaming files.
Also read: How to install PHP in Ubuntu?
How to rename files in Linux using the mv command?
The usual syntax for the mv command is as follows.
mv [options] source_file destination/path
While the source_file can be one or many, the destination path has to be a single file or directory. Therefore in order to rename a file, all you have to do is mention the file name as the source and then simply type in a new name in the destination path.
Check out this example.
mv file1.txt file_new.txt
The above command will rename file1 to file_new
The mv command can only rename one file at a time, but you can use the command inside a for or while loop in a Bash script to rename multiple files at once.
for f in *.html; do
mv -- "$f" "${f%.html}.php"
done
In the above example, the for loop iterates through all the files that end with a .html extension. The second line then renames each file and changes the extension from .html to .php. The done statement simply marks the end of the loop.
You can also use the mv command in conjunction with the find command to achieve the same result.
find . -depth -name "*.html" -exec sh -c 'f="{}"; mv -- "$f" "${f%.html}.php"' \;
Here the find command is passing all files ending with .html in the active directory to the mv command which in turn is renaming them to .php files.
Also read: How to install OpenCV in Ubuntu?
How to rename files in Linux using the rename command?
The rename command can rename multiple files at once, which is its biggest advantage over the mv command. However, you do require a little bit of regular expressions knowhow to be able to use this command to its fullest.
Another thing to note before we start using this command is that it isn’t part of the standard Linux distrubiton. To install the command, enter the following command.
- Ubuntu/Debian – sudo apt install rename
- CentOS/Fedora – sudo yum install prename
- Arch Linux – yay perl-rename ## or yaourt -S perl-rename
- Manjaro – sudo pacman -Syu perl-rename
Now that we have the command installed, here’s the basic syntax for the same.
rename [Options] 's/oldName/newname/' files
Going back to our previous example, if you were to rename multiple .html files to .php, here’s the command you’d use.
rename 's/.html/.php/' \*.html
You can also use the -n flag to print out the file names before they’re renamed.
rename -n 's/.html/.php/' \*.html
By default, rename doesn’t overwrite existing files. If you’re renaming a lot of files at once, this can cause a lot of junk in your directories. Use the -f flag to allow existing files to be overwritten by the command.
rename -f 's/.html/.php/' \*.html
In another example, if you were to replace spaces in a file name with underscores, here’s what you’d have to do.
rename 'y/ /\_/' \*
There’s also a -v flag. This prints the list of renamed files along with their new names as the command goes along.
You can even delete parts of a filename by omitting the search term from the regular expression.
rename 's/ext_//' *.c
The above command will remove ‘ext_’ from all the files present with that name in the current working directory.
In order to make changes to specific parts of the name, you just need to mention the part that you want to change followed by the new bit.
rename 's/conf/parameter/' *.c
The above command will replace the word conf present in any filename with parameter. Note that since we’re mentioning *.cfiles, this command will only work for files ending in a .c extension.
Also read: How to remove a Directory in Linux?
How to change filenames in Linux using the GUI?
Using the Linux GUI file manager, changing names is exactly how you’d expect. Just right-click on the file or folder, click Rename and type out the new name. You can also use the F2 keyboard shortcut.
You can, however, use batch renaming tools such as Metamorphose2 or pyRenamer to rename multiple files at once and make your life easier.
Also read: How to use SCP command to transfer files in Linux?