How to Rename Files in Linux and also Directories in Linux

mv /home/user/Files/filename1.ext /home/user/Files/filename2.ext

Notice that for the folder containing files, the mv command needs write authorization. For device files and folders, the user should be able to rename the files by prepending mv with sudo or su to get root permission. The-i (interactive) alternative offers a further security layer that allows the consumer to validate the rename of the file before it is actively used.

There is also the alternative -v (verbose) listing all the changes made by mv. Options are written before filenames after mv not before.

Using the “rename” command

This order is slightly more advanced than mv because it requires regular expression awareness or at least basic familiarity. This may sound scary, but don’t give up rename – it can be used just by following tutorials like this one for batch renaming.

The rename the syntax looks like this:

rename (option) 's/oldname/newname/' file1.ext file24.ext

The letter “s” stands for “substitute,” and it’s the main part of the regular expression. Single quotes around it are obligatory. Available options are:

  • -v (verbose: prints the list of renamed files along with their new names)
  • -n (“no action:” a test mode or simulation which only shows the files that will be changed without touching them)
  • -f (a forced overwrite of the original files)

The rename the command also accepts wildcards to rename multiple files of the same type, and it works on file extensions as well. For example, this would change all files with the extension .jpeg to .jpg:

 

rename 's/.jpeg/.jpg/' *

The wildcard symbol (*) means that all files in the folder will be affected.

The regular expression also has its own options (modifiers): “g” (global; affects all occurrences of the expression), and “i” (performs case-insensitive substitution). They are written at the end of the expression just before the closing single quote and can be combined:

rename -n 's/DSC/photo/gi' *.jpg

The command would also modify the portion of the filename to “photo.” But, because of the option “-n, “the command would not rename the file but just display them into the console window. This will be applicable in the files” DSC,” “dSC “and” dsc “This will occur.

The only thing that this common phrase can do is not a replacement. There’s also a conversion–labeled with a “y” dot–that can render the filenames more complicated. The case file name is most commonly changed:

rename 'y/a-z/A-Z/' *.jpg

That will shift the name to uppercase for all.jpg files. To do so vice versa, just alter the standard expression’s “old name” and “new name.”

To achieve the desired result, the rename command combines a few simple patterns. The “-n” option allows users to never jeopardize their data (or their nerves) since it gives a secure and usable overview of how they’re called.

Métamorphose2

A cross-platform script and mass-replacer archive is Métamorphose. Métamorphose is a very powerful tool to use for those who want an Interface application for renaming transactions. It’s both Windows and Linux accessible.

Go to its website to launch and download your Distro Installer Set. It includes a deb file for Fedora, Mandriva, and SUSE for Debian-based distro and RPM kit. Only in Arch Linux, it is usable in AUR.

It is the first “picker” tab when you launch the device. You pick the directory with the files that you want to delete. Notice that only one directory per rename process is allowed, but child directories are accessible in the chosen directory.

 

How to Rename Files in Linux and also Directories in
How to Rename Files in Linux

 

Click on the “Renamer” tab when you pick the page. You can choose the renaming rules from here. The operation you want to execute on the left panel can be picked. For instance, “insert” incorporates words into name while “length” allows a number of characters to trim the files’ names. You can also pick the’ jump file,” turn’ and’ shift.’ As you can see, you can rename your files with comprehensive choices.

 
1668688931 957 How to Rename Files in Linux and also Directories in
How to Rename Files in Linux

Lastly, just click the “Go” button at the top of the bottom pane to run the renaming action. If you find any error after renaming, there is an Undo option to revert all the changes.

pyRenamer – the easy way out

And lastly, the answer waited for by all anti-console users: a desktop application that can do these tasks with a single mouse button. PyRenamer is a crazy, efficient Python-written device to rename directories. Right now, Ubuntu users and its descendants are obviously still free to install py renamed from repositories using the command:

sudo apt-get install pyrenamer

How to Rename Files in Linux and also Directories in

The interface consists of four parts:

  1. a tree-view file browser for selecting files and folders
  2. a central preview pane showing the filenames before and after renaming
  3. a tabbed control area for choosing the renaming criteria
  4. the Options sidebar

PyRenamer may delete accents and symbols from filenames, change any text string to another, modify filename to uppercase, lowercase or sentence case and insert or erase spaces and underscores automatically. Beginners love it as it can all be selected simply by pressing Rename in the tab-box section, previewed in the main area. PyRenamer can provide you with a cheat sheet to make it easier to play with patterns.

 

1668688932 210 How to Rename Files in Linux and also Directories in

Advanced users can enjoy the ability of pyRenamer to rename multimedia files by reading metadata. You can also rename a single file manually if it is impractical to rename a series.

Basically, py renamed folds the mv code into a user-friendly Interface and eliminates orders. For people who do not believe in their CLI abilities, that is a great choice.

There are, of course, many ways of changing Linux files–for example, by writing a script or using other py renamed-similar tools.

How to Rename Files and Directories in Linux

One of the most critical activities you often require in a Linux environment is renaming the files and directories. Using a GUI file manager or via the command-line console, you can rename data.

Renaming a single file is simple, but it can be a difficult challenge to rename multiple files at once, especially for users new to Linux.

This guide teaches you the way to delete files and folders using the mv and rename commands.

Renaming Files with the mv Command

The mv command (short of move) is used to rename or move files from one location to another. The syntax for the mv the command is as follows:

mv [OPTIONS] source destination

Copy

The source can be one or more files, or directories and destination can be a single file or directory.

  • If you specify multiple files as the must be a directory. In this case, the files are moved to the target directory.
  • If you specify a single file as source, and the destination target is an existing directory, then the file is moved to the specified directory.
  • To rename a file, you need to specify a single file as a source and single file as a destination target.

For example, to rename the file file1.txt as file2.txt you would run:

mv file1.txt file2.txt

Renaming multiple files with the mv Command

The mv a command can rename only one file at a time, but it can be used in conjunction with other commands such as find or inside bash for or while loops to rename multiple files.

The following example shows how to use the Bash for loop to rename all .html files in the current directory by changing the .html extension to .php.

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

Copy

Let’s analyze the code line by line:

  • The first line creates a for loop and iterates through a list of all files edging with .html.
  • The second line applies to each item of the list and moves the file to a new one replacing .html with .php. The part ${file%.html} is using the shell parameter expansion to remove the .html part from the filename.
  • done indicates the end of the loop segment.

Here is an example using mv in combination with find to achieve the same as above:

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

Copy

The find the command is passing all files ending with .html in the current directory to mv one by one using the -exec option. The string {} is the name of the file currently being processed.

As you can see from the examples above, renaming multiple files using the mv a command is not an easy task as it requires a good knowledge of Bash scripting.

Renaming Files with the rename Command

The rename a command is used to rename multiple files. This command is more advanced than mv as it requires some basic knowledge of regular expressions.

There are two versions of the rename command with different syntax. In this tutorial, we will be using the Perl version of the rename command. If you don’t have this version installed on your system, you can easily install it using the package manager of your distribution.

The syntax for the rename the command is as follows:

rename [OPTIONS] perlexpr files

Copy

The rename the command will rename the files according to the specified perlexpr regular expression. You can read more about Perl’s regular expressions here.

The following example will change all files with the extension .html to .php:

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

You can use the -n option to print names of files to be renamed, without renaming them.

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

The output 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)

By default, the rename the command doesn’t overwrite existing files. Pass the -f option to allow existing files to be overwritten:

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

Below are a few more common examples of how to use the rename command:

  • Replace spaces 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 shown you how to use the mv and rename commands to rename files.

 

Leave a Comment