How To: Rename A File In Bash

To rename a file in Bash, you can use the mv command, which is used to move and rename files. Here’s the basic syntax:

mv old_filename new_filename

For example, to rename a file named “file.txt” to “new_file.txt”, you can use the following command:

mv file.txt new_file.txt

This will rename the file “file.txt” to “new_file.txt” in the current directory.

If you want to rename a file in a different directory, you can specify the full path to the file instead of just the filename. For example, to rename a file named “file.txt” in the directory “/home/user/documents/” to “new_file.txt”, you can use the following command:

mv /home/user/documents/file.txt /home/user/documents/new_file.txt

This will rename the file “file.txt” to “new_file.txt” in the directory “/home/user/documents/”.

Note that if the new filename already exists in the directory, the mv command will overwrite it without warning. So make sure to choose a unique name for the new file to avoid overwriting existing files.

Leave a Comment