:!cp % %-

Create a backup of file being edited while using vi

At the start of a vi session and *before* saving any changes use ":!cp % %-" to make a backup of the current file being edited. example: vi /data/some/long/path/file :!cp% %- creates /data/some/long/path/file-

By: mpb
2009-03-17 00:34:24

These Might Interest You

  • This will create an exact duplicate image of your hard drive that you can then restore by simply reversing the "if" & "of" locations. sudo dd if=/media/disk/backup/sda.backup of=/dev/sda Alternatively, you can use an SSH connection to do your backups: dd if=/dev/sda | ssh user@ssh.server.com dd of=~/backup/sda.backup

    sudo dd if=/dev/sda of=/media/disk/backup/sda.backup
    bandit36 · 2009-02-27 20:23:37 2
  • It will create a backup of the filename. The advantage is that if you list the folder the backups will be sorted by date. The command works on any unix in bash. Show Sample Output

    cp path/filename{,-$(date +%Y-%m-%d)}
    vutcovici · 2009-08-06 13:50:00 1
  • this command can be added to crontab so as to execute a nightly backup of directories and store only the 10 last backup files.

    for file in $(find /var/backup -name "backup*" -type f |sort -r | tail -n +10); do rm -f $file; done ; tar czf /var/backup/backup-system-$(date "+\%Y\%m\%d\%H\%M-\%N").tgz --exclude /home/dummy /etc /home /opt 2>&- && echo "system backup ok"
    akiuni · 2014-09-24 14:04:11 0
  • If you have ever edited a locally checked out version of a file to tweak it for testing purposes, and came back to it over a weekend, you might have forgotten what you exactly changed. This command helps you see the differences between the the checked in SVN version, and the one you tweaked. Show Sample Output

    svn diff <FILE>
    bbbco · 2012-01-30 16:47:48 5
  • Create backup (.tar.gz) for all first-level directory from current dir. Show Sample Output

    find -maxdepth 1 -type d -print0 | xargs -0 -I {} tar -cvzf {}.tar.gz {}
    piemme · 2009-03-17 11:12:53 3
  • Create a cronjob that runs weekly to backup your database to a file.

    0 0 * * 0 /usr/bin/mysqldump -uroot -p'<password>' data_base_name > /home/bob/XYZ_DB_BACKUP/$(date +\%Y-\%m-\%d_\%Hh\%M).sql
    dtolj · 2010-09-02 20:07:07 0

What Others Think

You do realize that vim can be set to backup files, right? Aside from that, you should check out the -b option for cp.
jmcantrell · 479 weeks and 3 days ago
@jmcantrell command-line fu is about commands not config files. In any case, I have been using "!cp % %-" since 1990 (when vim did not exist) and I prefer to name the backup file to my own convention. For me, "file" and "file-" is sufficient. However, this technique could also be used to make snapshots of the file as the edit progresses: "!cp % %-1! "!cp % %-2! "!cp % $-with-function-y" etc etc
mpb · 479 weeks and 3 days ago
:w! >> file
voyeg3r · 479 weeks and 2 days ago
This may actually be a good way to create live patches of ascii files. Thanks for the idea.
denzuko · 472 weeks and 2 days ago
super handy
soupmagnet · 24 weeks ago

What do you think?

Any thoughts on this command? Does it work on your machine? Can you do the same thing with only 14 characters?

You must be signed in to comment.

What's this?

commandlinefu.com is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands

Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.

» http://twitter.com/commandlinefu
» http://twitter.com/commandlinefu3
» http://twitter.com/commandlinefu10

Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: