Web development and Tech news Blog site. WEBISFREE.com

HOME > linux

Learn about the Linux command rename

Last Modified : 05 Mar, 2023 / Created : 05 Mar, 2023
619
View Count

We will look into the Linux command 'rename'. We will learn together the syntax, examples and usage of this command.



# Linux command, rename

In Linux, you can use the command 'rename' to change a file name. Note that there are two types of rename in Linux. Here, we are referring to the Perl version of rename, and if it is not available, you may need to install it.
apt-get install rename

So, let's take a look at the syntax for using rename, briefly.

rename [옵션] Perl expression target file.
rename s/ Find the pattern / Changing pattern / Target file


The method requires three values as shown above. It may seem a little complex, but the grammar above is for reference only when making simple changes. In fact, the 'mv' command is used more often than 'rename' when actually renaming files. The reason for this is because it is simpler. You only need to use the original file name and the new file name to change it.

However!! The biggest advantage of 'rename' is that you can apply patterns (Perl expressions), making it very convenient when dealing with multiple files or rules.

Advantage - It is possible to apply patterns to multiple files and convert them at once.

Anyway, simple changes are more convenient with 'mv'. And 'mv' can also make multiple file changes using a loop. Let's take a look at an example of renaming below.


! Linux rename examples

Let's assume that there are files as shown below.
afile.txt
bfile.txt
cfile.txt

If you want to add today’s date, 0414, to the end of the names of the files above, how can you do it? You can use the following method.
$ rename 's/file/file0414/' *.txt

First, the *.txt at the end searches for all files with a txt extension and finds all files with the name 'file' and renames them all to 'file0414'.

So far, we have learned about using the rename command for renaming Linux file names.
Perhaps you're looking for the following text as well?

    Previous

    How to connect to ssh using Linux commands

    Previous

    [Linux] Exploring How to Find and Replace File Text