How to Rename Files in Linux Using the “mv” Command

New users would take time to deal with things in Linux, which are relatively simple in GUI. For example, in Linux without GUI, the only way to deal with files, like renaming them, is to use the command terminal, and if you don’t know how to do these simple-looking tasks without GUI, they can become a headache. Nevertheless, here we are with a tutorial that lists how to rename any file and its extension, whether txt, doc, jpg, etc., available on Linux using the “mv” command.

Let’s see multiple methods of using the mv (move) command to rename files in Linux.

Basic Syntax of the mv Command

The basic syntax of the mv command is here. Although the “mv” command moves a file from one location to another on Linux, it can also rename files. In the given command, the source is the file’s current name, and the target is the new name you want to give it.

mv [options] source target

Renaming a Single File

To rename a single file, specify the current and new names. For example, to rename a file named original.txt to renamed.txt, you would use:

mv original.txt renamed.txt

Renaming Files with Spaces in Their Names

If the file has a space in its name or you want to add a space in its new name, use quotes around them. For example:

mv "old file.txt" "new file.txt"

Renaming Files Using Wildcards

We can use the asterisk sign, also known as wildcards, to rename multiple files that match a specific pattern. However, using wildcards directly with mv doesn’t work as intended for renaming various files; we can use a loop to solve this.

Here’s how you can rename files with a specific pattern: Let’s say there are multiple “.txt” files on your system, and you want to rename them with a “.bak” extension from “.txt“. Then, we can follow the given script.

In the given command syntax, replace “*.txt” with your current file extension and the “.bak” with which you want to replace it.

for file in *.txt; do
  mv "$file" "${file%.txt}.bak"
done

Furthermore, not only the extensions even, we can use a loop to rename multiple files systematically. For instance, if you have files named file1.txt, file2.txt, etc., and you want to rename them to document1.txt, document2.txt, etc., you can use a for loop:

for file in file*.txt; do
    mv "$file" "document${file#file}"
done

Using mv to Rename Files with a Suffix

We can combine the mv command and basename to rename files by adding a suffix. For example, if we want to rename file.txt to file_backup.txt, you can use:

mv file.txt $(basename file.txt .txt)_backup.txt

What if we want to add suffixes to multiple files at once? In that case, we can use the FOR loop; here is how… In a given example, it will rename all “.txt” files in a directory by adding a “_backup" Suffix:

This loop will rename file1.txt to file1_backup.txt, file2.txt to file2_backup.txt, and so on.

for file in *.txt; do
  mv "$file" "$(basename "$file" .txt)_backup.txt"
done

Renaming Files with Case Conversion

You can also use mv if you need to rename files to change their case (e.g., from uppercase to lowercase). For example, to rename FILE.TXT to file.txt:

From upper to lower case

mv FILE.TXT $(echo FILE.TXT | tr '[:upper:]' '[:lower:]')

For lower to upper case:

mv FILE.TXT $(echo FILE.TXT | tr '[:upper:]' '[:lower:]')

To convert all the files available in a directory from upper to lower or vice versa, we can use LOOP; here is the syntax:

To convert from upper to lower case for all available in the current directory:

for file in *; do
  mv "$file" "$(echo "$file" | tr '[:upper:]' '[:lower:]')"
done

Similarly, to convert lowercase to uppercase, we can use:

for file in *; do
  mv "$file" "$(echo "$file" | tr '[:lower:]' '[:upper:]')"
done

Renaming Files with Date and Time

What happens if you want to move your files or rename them simultaneously but with the current date and time to the file name; either to remember when you have changed or edited them easily? So, for that, we can use the given command syntax, in which we are renaming a file called ‘my.txt‘ to include the current date and time:

mv my.txt file_$(date +"%Y%m%d_%H%M%S").txt

Using find with mv for Advanced Renaming

Well, those with good command over Linux and looking for more advanced renaming tasks can combine the “find” command with mv. For instance, to find and rename all “.txt” extension files in a directory by adding a “prefix,” you can use:

find . -name "*.txt" -exec bash -c 'mv "$0" "${0%/*}/prefix_${0##*/}"' {} \;

For example, Suppose you have three files with linux1.txt, linux2.txt, and linux3.txt, all with similar extensions, that is, “.txt.” Now we want the “mv” command to find them and add “ubuntu” in front of all of them, then the command will be like this:

find . -name "*.txt" -exec bash -c 'mv "$0" "${0%/*}/prefix_${0##*/}"' {} \;

Conclusion

We have seen the basic commands of “mv (move)” to rename files on Linux. Users can easily follow them to deal with a single file or multiple ones with or without space along with wildcards, adding suffixes, converting cases, adding dates to file names, or performing advanced renaming with find; all this can be done with the mv command.

Other Articles:

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.