The rename Linux command is designed to modify the names of recordsdata and directories in step with specified regulations. It’s now not only a easy renaming software; it permits customers to accomplish bulk renaming operations the use of common expressions, making it an crucial command for gadget directors and builders alike.

Very similar to the mv command, which is used to transport or rename recordsdata, the rename command provides extra flexibility and regulate. It’s recurrently utilized by IT pros, equivalent to gadget directors, programmers, and knowledge analysts, to control huge units of recordsdata successfully.

The right way to Set up the rename Command

The provision of the rename command would possibly range relying at the Linux distribution. Underneath, I’ll define easy methods to set up and uninstall the rename command for some not unusual distributions:

Debian-based techniques (e.g., Ubuntu)

Set up:

sudo apt-get replace
sudo apt-get set up rename

Uninstall:

sudo apt-get take away rename

RedHat-based techniques (e.g., Fedora, CentOS)

Set up:

sudo dnf set up prename

Uninstall:

sudo dnf take away prename

Arch Linux

Set up:

sudo pacman -S perl-rename

Uninstall:

sudo pacman -R perl-rename

openSUSE

Set up:

sudo zypper set up rename

Uninstall:

sudo zypper take away rename

The right way to Use rename

1. Change a string in filenames

Syntax: rename 's/outdated/new/' *

Clarification: Replaces the string “outdated” with “new” in all filenames within the present listing.

Instance: rename 's/take a look at/demo/' *

Output:

testfile1.txt renamed as demofile1.txt
testfile2.txt renamed as demofile2.txt

All recordsdata within the present listing that had take a look at of their title now have that changed with demo.

2. Upload a prefix to filenames

Syntax: rename 's/^/prefix_/' *

Clarification: Provides “prefix_” to the start of all filenames within the present listing.

Instance: rename 's/^/sample_/' *

Output:

file1.txt renamed as sample_file1.txt
file2.txt renamed as sample_file2.txt

All recordsdata within the present listing now get started with the prefix sample_.

3. Take away a document extension

Syntax: rename 's/.ext$//' *

Clarification: Eliminates the “.ext” document extension from all matching filenames within the present listing.

Instance: rename 's/.txt$//' *

Output:

report.txt renamed as report
notes.txt renamed as notes

All .txt extensions from recordsdata within the present listing had been got rid of.

4. Convert filenames to lowercase

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

Clarification: Converts all uppercase letters in filenames to lowercase within the present listing.

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

Output:

FILE1.TXT renamed as file1.txt
FILE2.TXT renamed as file2.txt

All filenames within the present listing that had uppercase letters at the moment are in lowercase.

5. Trade document extension

Syntax: rename 's/.oldext$/.newext/' *

Clarification: Adjustments the document extension from “.oldext” to “.newext” for all matching filenames within the present listing.

Instance: rename 's/.jpg$/.png/' *

Output:

image1.jpg renamed as image1.png
image2.jpg renamed as image2.png

All .jpg extensions from recordsdata within the present listing had been modified to .png.

6. Take away a suffix from filenames

Syntax: rename 's/suffix$//' *

Clarification: Eliminates “suffix” from the top of all filenames within the present listing.

Instance: rename 's/_backup$//' *

Output:

file1_backup.txt renamed as file1.txt
file2_backup.txt renamed as file2.txt

All recordsdata within the present listing that ended with _backup now have that suffix got rid of.

7. Change areas with underscores in filenames

Syntax: rename 's/ /_/' *

Clarification: Replaces all areas with underscores in filenames within the present listing.

Instance: rename 's/ /_/' *

Output:

my document.txt renamed as my_file.txt
any other document.txt renamed as another_file.txt

All filenames within the present listing that had areas now have the ones areas changed with underscores.

8. Upload a suffix to filenames with out converting the extension

Syntax: rename 's/(.w+)$/_suffix$1/' *

Clarification: Provides “_suffix” sooner than the document extension for all filenames within the present listing.

Instance: rename 's/(.w+)$/_edited$1/' *

Output:

file1.txt renamed as file1_edited.txt
file2.jpg renamed as file2_edited.jpg

All recordsdata within the present listing now have _edited added sooner than the document extension.

Extra Linux instructions:
Listing Operations rmdir · cd · pwd
Document Operations cat · cp · dd · much less · ls · mkdir · mv · tail · tar · zip
Document Gadget Operations chown · mkfs
Networking ping · curl · wget · iptables
Seek and Textual content Processing to find · grep · sed · whatis
Gadget Data and Control env · historical past · best · who
Person and Consultation Control display · su · sudo

The put up The right way to Use the ‘rename’ Command in Linux seemed first on Hongkiat.

WordPress Website Development Source: https://www.hongkiat.com/blog/linux-command-rename/

[ continue ]