Introduction
Before we begin talking about how to rename files and directories on Linux let's briefly understand – What is Directory?
A directory is a digital storage space that organizes and categorizes information. It acts as a hierarchical structure and allows users to navigate and locate specific files or data quickly. Directories are commonly used to store files on computers or websites, providing easy access and efficient management.
By using directories, individuals and businesses can effortlessly locate and retrieve information, enhancing productivity and organization. With their intuitive design and efficient functionality, directories play a key role in effectively managing data and optimizing workflow efficiency.
In this tutorial, you will rename files and directories on Linux. We will also address a few FAQs on how to rename files and directories on Linux.
Advantages of Directory
- Easy Organization: Directories simplify file management by providing a hierarchical structure, making it convenient to categorize and store information.
- Quick Navigation: With directories, users can easily locate and access specific files, saving time and effort in searching through extensive data.
- Efficient File Retrieval: Directories enable swift file retrieval, allowing users to find and retrieve information promptly, enhancing productivity.
- Streamlined Collaboration: Directories facilitate seamless collaboration by allowing multiple users to access and share files, fostering teamwork and coordination.
- Enhanced Data Security: Directories offer enhanced data security measures, ensuring that files are protected from unauthorized access, reducing the risk of data breaches or loss.
Rename Files with mv Command
To rename or move files from one location to another, you can use mv
command. Following will be the syntax for the mv
command:
mv [OPTIONS] source destination
You can have both files and directories as the source and the destination can be a single file or directory.
- If there are multiple files in the
source
then the destination should be adirectory
. - If there is only a single file in the
source
and the target is an existing directory then the file will be moved to that particular directory. - Rename a file by specifying a single file as a
source
and a single file as adestination
target.
Run the following command to rename the file file1.txt
to file2.txt
:
mv file1.txt file2.txt
Renaming Multiple Files with mv Command
Only one file at a time can be renamed using mv
command. To be able to rename multiple files with mv
command, it can be used along with some other commands like find
or you can also use for
or while
loops inside the bash.
Below is an example showing the way in which you can rename all .html
files to the files with .php
extension.
for f in *.html; do
mv -- "$f" "${f%.html}.php"
done
The same result can be achieved using the find
command like below:
find . -depth -name "*.html" -exec sh -c 'f="{}"; mv -- "$f" "${f%.html}.php"' \;
The find
command is passing all the files with .html
extension in the current directory to mv
one by one using the -exec
option.
Renaming Files with rename
Command
rename
command can be used to rename multiple files. This command requires more knowledge of regular expressions hence it is more advanced.
rename
command has two versions with different syntaxes. We will be using the Perl version in this tutorial.
- If you want to install
rename
on Ubuntu and Debian, use the following command:
sudo apt install rename
- For CentOS and Fedora:
sudo yum install prename
- For Arch Linux
yay perl-rename ## or yaourt -S perl-rename
Check below the syntax of rename
command:
rename [OPTIONS] perlexpr files
With rename
command, files are renamed based on the perlexpr
regular expression. You can read more about Perl regular expressions here.
Use the following command to change files with .html
extension to .php
:
rename 's/.html/.php/' *.html
Use the -n
option to print names of files to be renamed, without renaming them.
rename -n 's/.html/.php/' *.html
You will get the output like below:
Output
rename(file-20.html, file-20.php)
rename(file-12.html, file-12.php)
rename(file-34.html, file-34.php)
rename(file-70.html, file-70.php)
rename(file-28.html, file-28.php)
If you want to overwrite the existing command, use -f
option:
rename -f 's/.html/.php/' *.html
Check the below examples to understand a few more common examples of the rename
command:
- Replace spaces in filenames with an underscore
rename 'y/ /\_/' *
- Convert the filename to lowercase
rename 'y/A-Z/a-z/' *
- Convert the filename to uppercase
rename 'y/a-z/A-Z/' *
FAQs to Rename Files and Directories on Linux
Is it possible to rename multiple files simultaneously?
Yes, you can use wildcard characters with mv
command to rename multiple files. For example, mv *.txt newdir
will rename all 'txt' files to 'newdir'.
Can I change the extension of a file in Linux?
Yes, you can change the file extension by simply renaming the file with the new extension using the mv
command.
How can I rename a directory in Linux?
Use the mv
command to rename directories as well. Just provide the current directory name and the desired new name, like this: mv olddir newdir
.
What happens if I try to rename a file to an existing filename?
If the new filename already exists in the same directory, the old file will be replaced by the new one with the same name.
How can I preserve file permissions while renaming?
By default, file permissions are preserved during renaming, so there's no need to worry about them being changed.
Can I rename files and directories in different locations?
Yes, you can specify the full path of the file/directory along with the new name to rename it in a different location on Linux.
Are there any limitations or restrictions on file/directory renaming?
Renaming is subject to permissions, so you must have proper read/write access to the file/directory and its parent directory in order to rename it.
Conclusion
We hope this detailed guide helped you understand the use-cases of rename
and mv
command.
Few other commands like mmv
can be used to rename the files. New Linux users can use GUI batch rename tools such as the Métamorphose .
If you have any queries or doubts, please leave them in the comment below. We'll be happy to address them.