Renaming a file in Linux is a common task that can be done in various ways. In this article, we will explore some of the most common methods and commands to rename a file in Linux.
Using the mv Command
The mv command is used to move or rename files and directories in Linux. The syntax of the mv command is:
mv [options] source destination
To rename a file, we can use the mv command with the source file name and the new file name as the destination. For example, to rename a file called old.txt to new.txt, we can use the following command:
mv old.txt new.txt
The mv command will overwrite the destination file if it already exists, unless we use the -i option to prompt for confirmation. For example, to rename a file called old.txt to new.txt and ask for confirmation if new.txt already exists, we can use the following command:
mv -i old.txt new.txt
The mv command can also rename multiple files at once, by using the * wildcard to match any file name. For example, to rename all the files with the .txt extension to .md extension, we can use the following command:
mv *.txt *.md
Using the rename Command
The rename command is used to rename files based on a pattern matching and substitution. The syntax of the rename command is:
rename [options] expression file(s)
To rename a file, we can use the rename command with a Perl regular expression that matches the old file name and replaces it with the new file name. For example, to rename a file called old.txt to new.txt, we can use the following command:
rename ‘s/old/new/’ old.txt
The rename command can also rename multiple files at once, by using the * wildcard to match any file name. For example, to rename all the files with the .txt extension to .md extension, we can use the following command:
rename ‘s/\.txt$/.md/’ *.txt
The rename command has some useful options, such as -n to show what files would be renamed without actually renaming them, and -v to show the renaming process verbosely. For example, to see what files would be renamed from .txt to .md without actually renaming them, we can use the following command:
rename -n ‘s/\.txt$/.md/’ *.txt
Using the mmv Command
The mmv command is used to move, copy, append, or link multiple files in one command. The syntax of the mmv command is:
mmv [options] source destination
To rename a file, we can use the mmv command with the source file name and the new file name as the destination. For example, to rename a file called old.txt to new.txt, we can use the following command:
mmv old.txt new.txt
The mmv command can also rename multiple files at once, by using the * wildcard to match any file name. For example, to rename all the files with the .txt extension to .md extension, we can use the following command:
mmv ‘*.txt’ ‘#1.md’
The #1 in the destination file name refers to the first wildcard in the source file name. We can use #2, #3, etc. to refer to the second, third, etc. wildcards in the source file name. For example, to rename all the files with the format name-date.txt to date-name.txt, we can use the following command:
mmv ‘*-*.txt’ ‘#2-#1.txt’
The mmv command has some useful options, such as -n to show what files would be renamed without actually renaming them, and -v to show the renaming process verbosely. For example, to see what files would be renamed from.txt to.md without actually renaming them, we can use the following command:
mmv -n ‘*.txt’ ‘#1.md’
Conclusion
In this article, we have learned how to rename a file in Linux using three different commands: mv, rename, and mmv. Each command has its own advantages and disadvantages, and we can choose the one that suits our needs best. We hope this article was helpful and informative for you. Happy renaming!