The cart is empty

In the Linux operating system, file manipulation is a fundamental task performed by users every day. Among common tasks is renaming files. In this article, we'll explore two basic commands for renaming files: mv and rename. Both of these tools are powerful and flexible, but each serves a slightly different purpose.

Using the mv Command

The mv command, short for move, is commonly used in Linux to move files from one location to another. However, few people know that mv can also be used to rename files. The usage is very straightforward and direct. To rename a file, simply provide the original filename followed by the new filename. The basic syntax looks like this:

mv [options] source_file target_file

For example, if you want to rename the file old_name.txt to new_name.txt, you would use the command:

mv old_name.txt new_name.txt

Advanced Usage of the mv Command

The mv command also supports working with multiple files at once and allows the use of wildcard characters (such as * to represent any number of characters). For example, if you wanted to move all .txt files to the documents directory, you could use:

mv *.txt documents/

However, this command does not rename multiple files to individually specified new names. For such tasks, it's more appropriate to use the rename command.

Using the rename Command

The rename command is specifically designed for batch renaming files in Linux. It allows the use of regular expressions, enabling advanced manipulation of file names. The syntax of the rename command may vary depending on the distribution, but the basic form looks like this:

rename 's/pattern/new_pattern/' files

For example, if you want to change the prefix old to new for all .txt files in the current directory, you can use the command:

rename 's/old/new/' *.txt

Beware of Different Versions of the rename Command

It's important to note that there are different versions of the rename command, which may vary between Linux distributions. The command shown above works for the perl version of rename, which is often available in Debian-based distributions. For precise usage and options, it's always best to refer to the manual page of the rename command in your distribution (man rename).

 

Renaming files in Linux can be a simple or complex task depending on your needs. While mv is great for quick and straightforward renaming of individual files, rename offers advanced options for batch renaming using regular expressions. Understanding these commands and their proper usage will enable you to efficiently manage files in your Linux system.