Test your skills on our all Hosting services and get 15% off!

Use code at checkout:

Skills
30.10.2024

How to rename files with mv

Renaming files in Linux is a common task, but the approach to renaming can vary depending on your needs and the tools you want to use. This article will discuss various methods for renaming files in Linux, ranging from simple file renaming with commands such as mv to more complex batch renaming with utilities such as rename and find.

1. Renaming files with mv

The most basic and commonly used command for renaming files in Linux is the mv (move) command. The mv command is mainly used to move files and directories, but also serves as a renaming tool.

Basic syntax

The syntax for renaming a file is simple:

mv old_filename new_filename

Example

To rename a file named file1.txt to file2.txt, run:

mv file1.txt file2.txt

This command renames file1.txt to file2.txt in the same directory.

Renaming multiple files

If you need to rename multiple files one at a time, you can do so with a few mv commands, but this can get tedious. For batch renaming, it is better to use more advanced tools like rename.

2. Renaming files with rename

The rename command is a powerful utility for renaming multiple files at once. It allows you to use Perl-based regular expressions to batch rename files, which can save a lot of time on complex renaming tasks.

Installing rename

In some Linux distributions, you may need to install the rename tool. You can install it using your package manager:

  • Debian/Ubuntu:
    the rename service is installed at: sudo apt install rename
  • CentOS/RHEL:
    sudo yum install prename

Basic syntax

The syntax of the rename command is:

rename 's/old_pattern/new_pattern/' files

Example

Suppose you have a set of files like file1.txt, file2.txt and file3.txt and you want to change all .txt files to .md. You can use:

rename 's/\.txt$/\.md/' *.html

This command uses a regular expression to replace the .txt at the end of each filename with .md.

Batch file renaming

You can also perform more complex batch renaming, such as prefixing or suffixing file names.

  • To add a prefix (e.g. “new_”) to all .txt files:
    rename 's/^/new_/' *.html
  • To add a suffix (e.g. “_backup”) before the file extension:
    rename 's/^.txt$/_backup.html/' *.html

3. Renaming files using find and mv

Another way to rename files, especially when dealing with files in multiple directories, is to combine the find command with mv. This approach gives you more flexibility in renaming files based on specific criteria.

Example

Let’s say you want to find all the files with a .log extension in the current directory and rename them to .txt:

find . -name "*.log" -exec bash -c 'mv "$1" "${1%.log}.txt"' -- {} \;
  • find .-name“*.log”: Finds all files with .log extension in the current directory.
  • exec bash -c ‘mv “$1” “${1%.log}.txt”‘ — {} \;: Executes the mv command to rename the files, removing the .log extension and adding .txt.

This method is powerful when you need to perform renaming in a large directory tree.

4. Renaming files with GUI

If you prefer to use a graphical interface to rename files, most Linux environments provide file managers that allow simple renaming. Common file managers include:

  • Nautilus (for GNOME)
  • Dolphin (for KDE)
  • Thunar (for XFCE)

Rename steps in file manager

  1. Open your file manager.
  2. Navigate to the file you want to rename.
  3. Right-click the file and select Rename.
  4. Type the new name and press Enter.

This is useful for users who prefer the point-and-click method of file management.

5. Renaming files using a Bash script

For more advanced users, you can create a custom Bash script to automate file renaming tasks. This is especially useful for repetitive renaming tasks or specific file patterns.

Example of a Bash script

1. Creating a shell script file

You will save the script to a file so you can run it later. To create the file, use a text editor such as nano.

nano rename_script.sh

2. Add your script to the file

#!/bin/bash
for file in *.txt; do
new_name="${file%.txt}.md"
mv "$file" "$new_name"
done

3. Make the script executable

Now you need to make the script executable so you can run it. Use the following command to give the script execute permissions:

chmod x rename_script.sh
4. Run the script

Now you can run the script in the terminal. It will rename all .txt files in the current directory to .md.

./rename_script.sh

5. Check changes

After running the script, you can verify the renaming of the files by making a list of the directory contents:

ls

Conclusion

Renaming files in Linux can be done in different ways depending on the complexity of the task. For simple renaming, the mv command is sufficient, but for batch renaming you can use the rename command or combine find with mv for more flexibility. In addition, graphical file managers and custom Bash scripts provide other ways to rename efficiently.

Choose the method that best suits your needs, and you can easily manage file renaming on your Linux system.

Let me know if you need more details or help!

Test your skills on our all Hosting services and get 15% off!

Use code at checkout:

Skills