
How to Change File Name in Linux: A Comprehensive Guide
Changing the name of a file in Linux can be a simple task, but it’s important to understand the different methods available to ensure you choose the one that best suits your needs. Whether you’re a beginner or an experienced user, this guide will walk you through the process step by step.
Using the mv Command
The most common and straightforward way to change a file name in Linux is by using the `mv` command. This command is used to move files and directories, but it can also be used to rename files. Here’s how to do it:
mv old_filename new_filename
Replace `old_filename` with the current name of your file and `new_filename` with the desired new name. For example, if you have a file named “document.txt” and you want to rename it to “report.txt”, you would use the following command:
mv document.txt report.txt
This command will rename the file in the same directory. If you want to move the file to a different directory while renaming it, you can specify the new directory path:
mv document.txt /path/to/new/directory/report.txt
Using the cp and rm Commands
Another method to rename a file in Linux is by using the `cp` and `rm` commands. This method involves creating a new file with the desired name and then deleting the old file. Here’s how to do it:
- Use the `cp` command to copy the old file to a new location with the desired name:
- Use the `rm` command to delete the old file:
cp old_filename new_filename
rm old_filename
This method is useful if you want to keep a backup of the original file before renaming it.
Using the touch Command
The `touch` command is primarily used to create new files, but it can also be used to rename files by creating a new file with the desired name and then deleting the old file. Here’s how to do it:
- Use the `touch` command to create a new file with the desired name:
- Use the `rm` command to delete the old file:
touch new_filename
rm old_filename
This method is similar to the `cp` and `rm` method, but it’s more straightforward since you don’t need to manually copy the file first.
Using the rename Command
The `rename` command is a more advanced method for renaming multiple files at once. It’s available in some Linux distributions, such as Ubuntu. Here’s how to use it:
rename 's/old_filename/new_filename/'
Replace `old_filename` with the current name of your file and `new_filename` with the desired new name. The asterisk () is a wildcard that matches all files in the current directory. For example, to rename all files ending with “.txt” to “.doc”, you would use the following command:
rename 's/.txt/.doc/'
Using the GUI File Manager
For users who prefer a graphical user interface (GUI), most Linux distributions come with a file manager that allows you to rename files easily. Here’s how to do it:
- Open the file manager and navigate to the directory containing the file you want to rename.
- Right-click on the file and select “Rename” or press F2.
- Enter the new name for the file and press Enter.
This method is the most user-friendly and is recommended for beginners.
Using the Bash Shell
For advanced users, you can use the Bash shell to rename files using various scripting techniques. Here’s an example of a simple Bash script that renames all files in a directory:
for file in ; do mv "$file" "${file%.}_new.${file.}"; done
This script uses a loop to iterate through all files in the current directory and renames them by appending “_new” to the file name before the extension. For example, “document.txt” would be renamed to “document_new.txt”.
Using the find Command
The `find` command is a powerful tool for searching