rename Command in Linux
Build an AI-First Career, Master the Complete Skillset
Choose from our industry-leading programs designed for career success
Modern Software and AI Engineering Program
Master full-stack development with AI integration
+1000 moreModern Data Science and ML with specialisation in AI
Advanced data science techniques with AI specialization
+1000 moreAdvanced AIML with Specialisation in Agentic AI
Deep dive into AIML with focus on Agentic systems
+1000 moreDevOps, Cloud & AI Platform Engineering
Build and manage AI-powered cloud infrastructure
+1000 moreAI Engineering Advanced Certification by IIT-Roorkee
Premier AI engineering certification from IIT-Roorkee
AI Forward Deployed Engineer Program
Full-stack engineering, production AI and client-facing consulting
+1000 moreOverview
In Linux, the 'rename' command is used to change the name of one or more files. It is a powerful tool that can help you rename multiple files at once with ease. In this article, we will discuss the syntax, usages, and tips related to the 'rename' command in Linux.
Linux rename Command Syntax
The syntax for the rename command is as follows:
Where:
- expression: A Perl regular expression that matches the part of the filename you want to replace.
- replacement: The new string you want to use in place of the matched expression.
- file: The file or group of files that you want to rename.
rename Command Options:
- -v, --verbose: Display the names of the files as they are being renamed.
- -n, --no-act: Do not actually rename any files, but show what would be done.
- -f, --force: Force the rename operation, even if it would overwrite an existing file.
- -i, --interactive: Prompt the user before overwriting an existing file.
Example Usages
-
Rename a single file.:
Explanation: This command renames the file 'file.txt' to 'new_name.txt'. The expression 's/old_name/new_name/' tells 'rename' to look for the string 'old_name' in the file name and replace it with 'new_name'.
-
Rename multiple files with a common pattern.:
Explanation: This command renames all files with the '.txt' extension in the current directory that contain the string 'old_name' in their name. The 'rename' command replaces 'old_name' with 'new_name' in each file name.
How Scaler Transformed Careers in Different Fields
Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.
Tips
-
Be careful when using the 'force' option (-f), as it can overwrite existing files without prompting.
-
Use the 'no-act' option (-n) to test your rename commands before actually renaming any files.
Advanced Use Cases of rename Command in Linux
-
Rename files in subdirectories recursively.:
Explanation: This command recursively renames all files with the '.txt' extension in the current directory and its subdirectories that contain the string 'old_name' in their name
-
Rename files using a specific numbering scheme.:
Explanation: This command renames all files with the '.txt' extension in the current directory using a numbering scheme that starts at 001. The 'sprintf()' function pads the number with leading zeros so that the filenames have a consistent length.
-
Rename files based on their modification time.:
Explanation: This command renames the 10 most recently modified files in the current directory by appending '-1.txt', '-2.txt', and so on to their original name. The 'ls' command lists the files in the directory in reverse chronological order, 'head' selects the first 10 files, and 'xargs' passes them as arguments to the 'rename' command.
Turn Learning into Career Growth
Conclusion
-
The 'rename' command is a powerful tool for renaming files in Linux.
-
The command uses Perl regular expressions to match and replace parts of file names.
-
Use the '-n' option to test your rename commands before actually renaming any files.
-
Be careful when using the '-f' option, as it can overwrite existing files without prompting.