Make a Quick Backup of a File from the Command Line
It’s always a good idea to backup a file if you’re going to be making some changes to it and you’re uncertain of the outcome. In the Finder this is as simple as just selecting the file and hitting Command+D to create a duplicate of the file in question, but in the command line you typically have to use the cp command, point it at the file, and then specify the copied version. Here’s a nice variation of that to make fast backups of any file through the Terminal.
The simple syntax to create an instant backup of a file at the same location (directory) would be as follows:
For example, to backup the file named ‘file.txt’ as ‘file.txt.backup’ in the same directory, the command string would be like so:
Notice there are no spaces between the end of the first file and the brackets. This creates copy of “file.txt” with the added extension “.backup”, resulting in “file.txt.backup”.
Whether or not this is faster for you than typing “cp file1 file2” is going to depend on your keyboarding abilities and how much you use tab completion, but regardless of which approach you take it’s good to be in the habit of making backups so you can quickly restore things to how they were before being edited, be it htaccess, /etc/hosts, or just a plist file.
Thanks to James for the tip from commandlinefu