How to Rename Files and Directories in Linux

Renaming information is without doubt one of the most elementary duties you typically must carry out on a Linux system. You possibly can rename information utilizing a GUI file supervisor or by way of the command-line terminal.

Renaming a single file is straightforward, however renaming a number of information without delay could be a problem, particularly for customers who’re new to Linux.

On this tutorial, we are going to present you tips on how to use the mv and rename instructions to rename information and directories.

Renaming Recordsdata with the mv Command

The mv command (wanting transfer) is used to rename or transfer information from one location to a different. The syntax for the mv command is as follows:

mv [OPTIONS] supply vacation spot

The supply might be a number of information, or directories and vacation spot could be a single file or listing.

  • When you specify a number of information as supply, the vacation spot should be a listing. On this case, the supply information are moved to the goal listing.
  • When you specify a single file as supply, and the vacation spot goal is an current listing, then the file is moved to the required listing.
  • To rename a file, it’s essential to specify a single file as a supply and a single file as a vacation spot goal.

For instance, to rename the file file1.txt as file2.txt you’d run:

$ mv file1.txt file2.txt

Renaming a number of information with the mv Command

The mv command can rename just one file at a time, however it may be used along with different instructions resembling find or inside bash for or while loops to rename a number of information.

The next instance exhibits tips on how to use the Bash for loop to rename all .html information within the present listing by altering the .html extension to .php.

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

Let’s analyze the code line by line:

  • The primary line creates a for loop and iterates by way of a listing of all information edging with .html.
  • The second line applies to every merchandise of the checklist and strikes the file to a brand new one changing .html with .php. The half ${file%.html} is utilizing the shell parameter expansion to take away the .html half from the filename.
  • completed signifies the top of the loop phase.

Right here is an instance utilizing mv together with discover to attain the identical as above:

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

The discover command is passing all information ending with .html within the present listing to mv one after the other utilizing the -exec possibility. The string {} is the title of the file at present being processed.

As you may see from the examples above, renaming a number of information utilizing the mv command isn’t a straightforward process because it requires a great data of Bash scripting.

Renaming Recordsdata with the rename Command

The rename command is used to rename a number of information. This command is extra superior than mv because it requires some fundamental data of standard expressions.

There are two variations of the rename command with completely different syntax. On this tutorial, we can be utilizing the Perl model of the rename command. When you don’t have this model put in in your system, you may simply set up it utilizing the package deal supervisor of your distribution.

  • Set up rename on Ubuntu and Debian
$ sudo apt set up rename
  • Set up rename on CentOS and Fedora
$ sudo yum set up prename
  • Set up rename on Arch Linux
$ yay perl-rename ## or yaourt -S perl-rename

The syntax for the rename command is as follows:

rename [OPTIONS] perlexpr information

The rename command will rename the information in response to the required perlexpr common expression. You possibly can learn extra about perl common expressions here .

The next instance will change all information with the extension .html to .php:

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

You should use the -n choice to print names of information to be renamed, with out renaming them.

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

The output will look one thing like this:

Output:
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)

By default, the rename command doesn’t overwrite current information. Cross the -f possibility to permit current information to be over-written:

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

Beneath are just a few extra frequent examples of tips on how to use the rename command:

  • Substitute areas in filenames with underscores

$ rename 'y/ /\_/' \*
  • Convert filenames to lowercase

$ rename 'y/A-Z/a-z/' \*
  • Convert filenames to uppercase

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

Conclusion

We’ve proven you tips on how to use the mv and rename instructions to rename information.

There are additionally different instructions to rename information in Linux, resembling mmv . New Linux customers who’re intimidated by the command line can use GUI batch rename instruments such because the Métamorphose .

You probably have any questions or suggestions, be at liberty to go away a remark.

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

four × 2 =

Related Articles