How to Backup Files When Moving in Ubuntu Linux
When managing files in Ubuntu, accidentally overwriting important data during a move operation can be disastrous. This article explains how to safely use the move command by automatically creating backups of existing destination files. You will learn the specific flag required to preserve previous versions before replacing them with new ones.
The option that allows the mv command to create a backup
of the destination file before moving is --backup. You can
also use the shorthand version -b. When this option is
enabled, if the destination file already exists, mv will
rename the existing file with a backup suffix instead of deleting it
immediately.
To use this feature, open your terminal and structure your command as follows:
mv --backup=existing source_file destination_file
Or simply:
mv -b source_file destination_file
By default, the backup file is created with a tilde (~)
suffix. For example, if you move data.txt to a location
where data.txt already exists, the original destination
file becomes data.txt~.
You can customize the backup suffix using the --suffix
option. This is helpful for identifying when the backup was created or
distinguishing it from other backup files. The command structure looks
like this:
mv -b --suffix=.bak source_file destination_file
In this scenario, the existing destination file will be saved as
destination_file.bak. This ensures you retain access to the
original content even after the move operation completes successfully.
Using these options provides a safety net when managing critical files
in the Ubuntu environment.