rename Command in Linux

Learn via video courses
Topics Covered

Transform Your Career

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program

Overview

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.
Free Courses by top Scaler instructors
Python Course for Beginners With Certification: Mastering the Essentials
Java Course - Mastering the Fundamentals
DBMS Course - Master the Fundamentals and Advanced Concepts
JavaScript Course With Certification: Unlocking the Power of JavaScript
C++ Course: Learn the Essentials
Python and SQL for Data Science Course
Python Course for Beginners With Certification: Mastering the Essentials
Java Course - Mastering the Fundamentals
DBMS Course - Master the Fundamentals and Advanced Concepts
JavaScript Course With Certification: Unlocking the Power of JavaScript
C++ Course: Learn the Essentials
Python and SQL for Data Science Course

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.

Scaler Placement Report and Statistics

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data
Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more

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

1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary
1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary

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.