Easy ways to rename files and directories in Linux Terminal

Renaming files and directories is one of the most basic tasks that you will often need to perform on Linux.

Renaming a single file is easy, but renaming multiple files at once can be a challenge, especially for users new to Linux. You can rename files using the file manager on GUI-enabled systems. Then what if you are working on a system that only supports command line?

It is recommended to open the Linux distribution’s console to follow the guide on this page so that you have a better understanding of how to rename files or directories through the Linux Terminal.

We also recommend that you test any online tutorials or guides on a virtual machine (vmware or virtualbox) before deploying to a production server, so you don’t mess with an active system when something goes wrong.

You can learn how to install VMware on Ubuntu, CentOS, VirtualBox on Ubuntu, Fedora and CentOS

In this tutorial, we’ll show you how to use the command mv and commands rename To rename files and directories.

Rename the file using the mv . command

The mv command (stands for MaFifthe) To rename or move files from one location to another.

The syntax of the mv command is as follows:

mv [OPTIONS] sumber tujuan

sumber It can be one or more files or folders and tujuan It can be a single file or a directory.

  • If you select multiple files as sumber, the destination must be a guide. In this case, the file sumber Go to the target directory.
  • If you select one file as a file sumberAnd the goal tujuan is an existing directory, then the file will be moved to the specified directory.
  • To rename a file, you need to specify a file as the source and a file as the target tujuan.

For example, to change the file name from file1.txt Become file2.txt You can run the following command:

mv file1.txt file2.txt

How to rename multiple files using the mv . command

ranking mv Only one file can be renamed at a time, but it can be used with other commands, such as the find command or in a bash or while loop program to rename multiple files.

See also  How to install and use Curl on CentOS 8

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

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

Let’s break down the code line by line:

  • The first line makes loop to get data for to find all lists of files ending in .short message.
  • The second line applies to each item in the list and moves the file to the new format, replacing .txt with .php. part ${file%.txt} Use expand shell parameter to remove part .txt from the file name.
  • Done, indicates the end of the episode segment.

We can also use the . command mv in association with find To achieve the same result mentioned above.

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

The find command goes through all files ending in .txt In the current directory of the command mv One by one using the switch -exec. series {} It is the name of the file to be processed.

As you can see from the example above, rename multiple files with the . command mv Not an easy task as it requires a good knowledge of Bash scripts.

Rename a file using the rename . command

ranking rename Used to rename multiple files. This is more advanced than this mv because it requires basic knowledge of regular expressions.

There are two versions of the order rename in a different form. In this tutorial, we will use the version perl from driving rename. If you have not installed rename On your system, you can easily install it using your distribution’s package manager.

  • Install rename on Ubuntu and Debian
    sudo apt install rename
  • Install Rename on CentOS and Fedora
    sudo yum install prename
  • Install Rename on Arch Linux
    yay perl-rename ## atau yaourt -S perl-rename

command syntax rename as follows:

rename [OPTIONS] perlexpr files

The rename command will rename all files by regular expression perlexpr which is specified. You can read more about Perl regular expressions here.

See also  A tutorial on using the last command in Linux Terminal

For example, the following command will change all files with the extension .txt to me .py:

rename 's/.txt/.py/' *.txt

You can use arguments -n Prints the names of the files to be renamed without renaming them.

rename -n 's/.txt/.py/' *.html

Output the above command:

rename(file-10.txt, file-10.py)
rename(file-11.txt, file-11.py)
rename(file-12.txt, file-12.py)
rename(file-13.txt, file-13.py)
rename(file-14.txt, file-14.py)

By default, the rename command will not overwrite existing files. use arguments -f Allows existing files to be overwritten.

rename -f 's/.txt/.py/' *.html

Here are some common examples of how to use the command rename :

  • Change the spaces in the file name to underscores (For example this_is_file_name)
    rename 'y/ /_/' *
  • Rename the file to lowercase (all lowercase)
    rename 'y/A-Z/a-z/' *
  • Rename the file in uppercase (all caps)
    rename 'y/a-z/A-Z/' *

conclusion

Now you have a good understanding of how to use commands mv and the rename command to rename the file. Of course there are other commands to rename files in Linux like mmv. New Linux users who are afraid of the command line can use a GUI batch renaming tool like Métamorphose.

Source link