How to Move and Rename a File Simultaneously in Ubuntu
In Ubuntu and other Linux distributions, moving and renaming a file are technically the same operation. This article provides a quick overview of the command-line tool used for this task and demonstrates how to execute both actions at once without needing separate steps.
The MV Command
To move and rename a file simultaneously in Ubuntu, you use the
mv command in the terminal. This command stands for “move.”
In Linux, renaming a file is simply moving it to the same directory with
a different name. Therefore, specifying a new path and a new filename in
the destination argument accomplishes both tasks instantly.
Basic Syntax
The standard syntax for the command is:
mv [options] source_file destination_path/new_filename- source_file: The current name and location of the file.
- destination_path: The folder where you want the file to end up.
- new_filename: The new name you want to give the file.
Practical Examples
Rename and Move to a Different Directory
If you have a file named report.txt in your current
directory and want to move it to the Documents folder while
changing its name to final_report.txt, run:
mv report.txt ~/Documents/final_report.txtRename and Move Using Absolute Paths
You can also use full paths to ensure accuracy. To move a file from
/tmp/data.log to /var/logs/archive.log,
use:
sudo mv /tmp/data.log /var/logs/archive.logNote that using sudo may be required if you are moving
files into system directories that require administrator privileges.
Overwriting Existing Files
Be cautious when moving and renaming. If a file with the same name
already exists at the destination, the mv command will
overwrite it without warning by default. To prevent accidental data
loss, you can use the -i (interactive) flag:
mv -i source.txt /destination/new_source.txtThis will prompt you to confirm before overwriting an existing file.