Home » Linux Rename File and Directory

Linux Rename File and Directory

by Online Tutorials Library

Linux Rename File and Directory

To rename a file there are other commands also like ‘mv’. But ‘rename’ command is slightly advanced then others. This command will be rarely used and it works differently on different distros of linux. We’ll work on Debian/Ubuntu examples.

Generally, renaming is not a big task, but when you want to rename a large group of files at once then it will be difficult to rename it with ‘mv’ command. In these cases, it is adviced to use ‘rename’ command. It can convert upper case files to lower case files and vice versa and cn overwrite files using perl expressions. This command is a part of perl script.

Basic syntax:

This (‘s/old-name/new-name/’) is the PCRE (perl compatible regular expression) which denotes files to rename and how.

Let’s see an example of basic rename command:

In the example below we have converted all the files ending with ‘.txt’ into files ending with ‘.pdf’.

linux-file-rename-command

In another example, we have converted file into document for all files ending with .pdf

linux-file-rename-command


rename option:

rename has some optional arguments but a mandatory perl expression that comes with every opption and guides it how to work.

Option Function
rename -n Check changes before running the command.
rename -v Print the output.
rename (a-z)(A-Z)/ (A-Z)(a-z) Convert into upper case/lower case.
rename -f Forcefully over write existing files.

You may also like