×

rename Command in Linux/Unix with Examples

The rename command in Linux is used to rename multiple files at once, using a specified pattern. The command is also known as rename, ren, and mv. This command is particularly useful when you need to rename a large number of files in a specific format, such as changing the file extension or adding a prefix or suffix to the file name.

The basic syntax of the rename command is as follows:

rename [options] 'perlexpr' [files]

Here are some examples of how to use the rename command:

1) To rename all files in the current directory with a ".txt" extension to a ".md" extension, you can use the following command:

rename 's/.txt$/.md/' *

2) This command uses a regular expression to match files with a ".txt" extension and replace it with ".md". The * specifies that all files in the current directory should be renamed.

To add a prefix "old_" to all files in the current directory, you can use the following command:

rename 's/^/old_/' *

This command uses a regular expression to match the start of the file name and add the prefix "old_" to it.

3) To remove a prefix "old_" from all files in the current directory, you can use the following command:

rename 's/^old_//' *

This command uses a regular expression to match the prefix "old_" and remove it from the file name.

4) To change the case of all files in the current directory to uppercase, you can use the following command:

rename 'y/a-z/A-Z/' *

This command uses the y option to translate all lowercase letters to uppercase.

5) To move all files in the current directory to a subdirectory called "backup , you can use the following command:

rename 's/^/backup\//' *

This command uses a regular expression to match the start of the file name and add the "backup/" prefix to it. This essentially moves the files to a subdirectory called "backup".

6) To rename all files in the current directory with a specific pattern in the file name, you can use the following command:

rename 's/pattern/newname/' *

This command uses a regular expression to match the pattern in the file name and replace it with "newname".

7) To rename a specific file, you can use the following command:

rename 's/oldname/newname/' oldname

This command uses a regular expression to match the "oldname" and replace it with "newname".

8) To rename files recursively in all subdirectories, you can use the -R option

rename -R 's/oldname/newname/' /path/to/directory

It's important to note that the rename command can be dangerous if used improperly, as it can rename multiple files at once and overwrite existing files. It's always a good idea to test the command with a small set of files before running it on a large number of files. Additionally, it's recommended to use the -n option, which will print the names of the files to be renamed, but will not actually perform the rename operation. This allows you to review the changes and make sure they are what you expect before running the command without the -n option.

It's also worth noting that the rename command is not available on all distributions by default. On some distributions, you may need to install the perl-rename package, or use the mv command with a shell script to achieve similar functionality.


Related Topics

Linux apt-get command

Apt-get is a command-line that helps to handle the packages in Linux. Its major task is to assist in retrieving data and packages from trusted sources for purposes of installation,...

6 minutes read.

Linux Path Completion

Every operating system has some features which make them special. The Linux operating system primarily works on the command line interface, and we do have many features for this CLI....

1 minute read.

df Command in Linux/Unix with Examples

The 'df' command in Linux is used to display the amount of disk space available on the file system. The command reports the amount of disk space used and available...

3 minutes read.

man Command in Linux/Unix with Examples

The man command in Linux is used to display the manual pages for a given command or system call. These manual pages provide detailed information on the command's usage, options,...

3 minutes read.

chown Command in Linux/Unix with Examples

The 'chown' command in Linux is used to change the ownership of a file or directory. The command stands for "change owner" and it allows you to change the user...

3 minutes read.

sed Command in Linux/Unix with Examples

The 'sed' command in Linux is a powerful text processing tool that allows you to perform various operations on text files, such as searching, replacing, and manipulating text. The command...

3 minutes read.

Linux Distributions

Linux Distributions Linux OS is not like other operating systems. Linux can be modified according to the user’s requirements, and it is very flexible to any change. Linux distributions are often...

4 minutes read.

tar Command in Linux/Unix with Examples

The 'tar' command in Linux is used to create, maintain, and extract files from archive files called tarballs. The command stands for "tape archive" and it allows you to combine...

4 minutes read.

Linux Admin Tutorial

In publishing and graphic design, lorem ipsum is a placeholder text commonly used to demonstrate the visual form of a document without relying on meaningful content....

1 minute read.

ping Command in Linux/Unix with Examples

The "ping" command in Linux (and other Unix-like operating systems) is used to test the connectivity between two networked devices. The "ping" command sends a small packet of data, called...

4 minutes read.

ps Command in Linux/Unix with Examples

The 'ps' command in Linux is used to display information about the running processes on a system. The command stands for "process status" and it allows you to view details...

4 minutes read.

adduser Command in Linux with Examples

What is the adduser command? The adduser command creates a new user, and this command may be used to add apre-existing user to a group that already exists. How to use the...

4 minutes read.

Linux mkdir | Linux Create Directory

The Linux mkdir command is very much similar to Windows’s "new folder" feature. This command is used in Linux os to create new directories. The command mkdir stands for "make...

1 minute read.

top Command in Linux/Unix with Examples

The 'top' command in Linux is a real-time system monitoring tool that displays information about the running processes and system resource usage. The command provides a dynamic, scrolling view of...

4 minutes read.

Linux ls

The primary use case of the ls command is to list the content available in the directory. The default directory is the current directory of the user. The below screenshot...

5 minutes read.

Difference Between Unix and Linux

Unix vs Linux Unix and Linux both have been of great use in changing the world of operating systems. Unix was among the very few operating systems which were programmed for...

4 minutes read.

Linux rmdir command

The default use case of the rmdir command in the Linux system is to delete an empty directory. The below screenshot shows how to delete the empty directory. First, we...

1 minute read.

awk Command in Linux/Unix with Examples

The 'awk' command in Linux is a powerful text processing tool that allows you to perform various operations on text files, such as searching, replacing, and manipulating text. The command...

4 minutes read.

Top 20 Linux Commands

1) clear command clear is a standard operating system command which is used to clear the terminal screen. This command brings the command line on the top of the terminal. By pressing the...

10 minutes read.

cd Command in Linux/Unix with Examples

The 'cd' command in Linux is used to change the current working directory. The command stands for "change directory" and it is used to navigate the file system on a...

3 minutes read.