×

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

ls Command in Linux/Unix with Examples

The ls command in Linux is a command-line utility that is used to list the files and directories in a directory. It is one of the most basic and commonly...

4 minutes read.

Linux Commands with Examples

Linux Commands with Examples All the Linux commands are executed on the terminal provided by the Linux system. This terminal is a command-line interface to implement and display command outputs. Linux...

10 minutes read.

Linux pwd

The pwd command is used to print or display the name of the working or current directory. Some shells have their own version of the pwd with several features, in...

1 minute read.

cp Command in Linux/Unix with Examples

The cp (copy) command in Linux is a command-line utility that is used to copy files and directories. It is one of the most basic and commonly used commands in...

5 minutes read.

Linux Absolute and Relative Path

The concept of the absolute and relative path is very simple. The names “relative path” and “absolute path” give us little understanding of the concept we are learning here. The...

1 minute 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.

cat Command in Linux/Unix with Examples

The cat command in Linux is a command-line utility that is used to display the contents of a text file. It stands for "concatenate" and it is used to view...

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.

Linux Bash

Linux Bash In this tutorial, we are going to learn what the shell is? And how many various shells we have, we have also covered most of the bash shell features....

4 minutes read.

Linux SCP Command

SCP stands for secure copy protocol. It is a command-line application for copying files and directories safely between two places. Command-line utilities are programs that operate on a computer's command...

3 minutes 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.

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.

curl Command in Linux/Unix with Examples

The 'curl' command in Linux is a command-line tool that allows you to transfer data from or to a server using various protocols, including HTTP, HTTPS, FTP, and many more....

3 minutes read.

touch Command in Linux/Unix with Examples

The "touch" command in Linux (and other Unix-like operating systems) is used to create new empty files or update the timestamps of existing files. The touch command is a simple...

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.

gzip Command in Linux/Unix with Examples

The gzip command in Linux is used for compressing and decompressing files. It uses the Lempel-Ziv coding (LZ77) algorithm to compress files, which is known for its high compression ratio...

6 minutes read.

ssh Command in Linux/Unix with Examples

The ssh (Secure Shell) command in Linux is used to remotely access and control another computer or device over a network. It provides a secure and encrypted connection between the...

6 minutes read.

Linux Commands List

Linux Commands List The Linux systems are filled with commands for several use cases. We have divided these commands into some of the most common implementations for a better understanding of...

2 minutes read.

sudo Command in Linux/Unix with Examples

The sudo command in Linux is used to run a command or set of commands with the privileges of another user, typically the root user. This allows users to perform...

3 minutes read.

telnet Command in Linux/Unix with Examples

The telnet command in Linux is a command-line utility that allows you to connect to a remote host using the Telnet protocol. Telnet is a network protocol that provides a...

3 minutes read.