YouTube Icon

Code Playground.

How to Rename Files and Directories in Linux


How to Rename Files and Directories in Linux

Renaming records is one of the most fundamental undertakings you regularly need to perform on a Linux framework. You can rename records utilizing a GUI document director or by means of the order line terminal. 

Renaming a solitary record is simple, however renaming various documents on the double can be a test, particularly for clients who are new to Linux. 

In this instructional exercise, we will tell you the best way to utilize the mv and rename orders to rename records and catalogs. 

Renaming Files with the mv Command 

The mv order (shy of move) is utilized to rename or move records starting with one area then onto the next. The sentence structure for the mv order is as per the following: 

mv [OPTIONS] source destination

The source can be at least one documents, or indexes and objective can be a solitary record or catalog. 

In the event that you determine various documents as source, the objective must be a catalog. For this situation, the source documents are moved to the objective registry. 

In the event that you determine a solitary record as source, and the objective is a current registry, at that point the document is moved to the predetermined catalog. 

To rename a document, you need to determine a solitary record as a source and a solitary record as an objective. 

For instance, to rename the document file1.txt as file2.txt you would run: 

mv file1.txt file2.txt

Renaming various records with the mv Command 

The mv order can rename just each document in turn, however it tends to be utilized related to different orders, for example, find or inside slam for or while circles to rename various records. 

The accompanying model tells the best way to utilize the Bash for circle to rename all .html documents in the current index by evolving the .html augmentation to .php. 

for f in *.html; do
    mv -- "$f" "${f%.html}.php"

We should investigate the code line by line: 

The main line makes a for circle and emphasizes through top notch of all documents edging with .html. 

The subsequent line applies to every thing of the rundown and moves the record to another one supplanting .html with .php. The part ${file%.html} is utilizing the shell boundary extension to eliminate the .html part from the filename. 

done demonstrates the finish of the circle section. 

Here is a model utilizing mv in mix with find to accomplish equivalent to above: 

find . -depth -name "*.html" -exec sh -c 'f="{}"; mv -- "$f" "${f%.html}.php"' \;

The discover order is passing all documents finishing with .html in the current index to mv individually utilizing the - executive alternative. The string {} is the name of the document presently being prepared. 

As should be obvious from the models above, renaming different records utilizing the mv order is definitely not a simple assignment as it requires a decent information on Bash scripting. 

Renaming Files with the rename Command

The rename order is utilized to rename different documents. This order is further developed than mv as it requires some fundamental information on customary articulations. 

There are two variants of the rename order with various grammar. In this instructional exercise, we will utilize the Perl rendition of the rename order. On the off chance that you don't have this variant introduced on your framework, you can undoubtedly introduce it utilizing the bundle supervisor of your appropriation. 

Introduce rename on Ubuntu and Debian 

sudo apt install rename

Introduce rename on CentOS and Fedora 

sudo yum install prename

Introduce rename on Arch Linux 

yay perl-rename ## or yaourt -S perl-rename

The sentence structure for the rename order is as per the following: 

rename [OPTIONS] perlexpr files

The rename order will rename the records as indicated by the predetermined perlexpr ordinary articulation. You can peruse more about perl customary articulations here . 

The accompanying model will change all documents with the augmentation .html to .php: 

rename 's/.html/.php/' \*.html

You can utilize the - n alternative to print names of records to be renamed, without renaming them. 

rename -n 's/.html/.php/' \*.html

The yield will look something like this: 

rename(file-90.html, file-90.php)
rename(file-91.html, file-91.php)
rename(file-92.html, file-92.php)
rename(file-93.html, file-93.php)
rename(file-94.html, file-94.php)

As a matter of course, the rename order doesn't overwrite existing records. Pass the - f choice to permit existing records to be over-composed: 

rename -f 's/.html/.php/' \*.html

The following are a couple of more normal instances of how to utilize the rename order: 

Supplant spaces in filenames with underscores 

rename 'y/ /\_/' \*

Convert filenames to lowercase 

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

Convert filenames to capitalized 

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


We've told you the best way to utilize the mv and rename orders to rename documents. 

There are additionally different orders to rename documents in Linux, for example, mmv . New Linux clients who are threatened by the order line can utilize GUI bunch rename instruments, for example, the Métamorphose . 

On the off chance that you have any inquiries or input, don't hesitate to leave a remark.