quickly backup or copy a file with bash

cp filename{,.bak}

357
By: elofland
2009-02-05 17:12:03
cp

8 Alternatives + Submit Alt

What Others Think

this is using the bash shell I think... The same thing will work using mv e.g. mv filename{,.bak} nice command
finkployd · 488 weeks and 3 days ago
I cannot tell you how many types I've typed out this command the hard way. Of course one could write a bash/perl one-liner that would do the same thing "backify, or somesuch" with less typing :-D
QuantumFTL · 488 weeks and 3 days ago
Here is some info on the general case. http://www.linuxjournal.com/content/bash-brace-expansion
terrasect · 488 weeks and 3 days ago
Using tab-completion, I don't know how much time this would save, especially having to type out all the symbols
swegner · 486 weeks and 6 days ago
Lots of time. Typing symbols is not difficult. Think of a dir with files named test1_a2_tt.dat or something, but hundreds of them, tab completion will only help so much here.
jonathan · 486 weeks and 4 days ago
Or think of a case when you are not in the directory, but want to backup/edit the file from where you are.
ozymandias · 485 weeks and 6 days ago
I prefer tab completion too, i.e. start typing the filename again and then tab, adding .bak to the end.
Buzzcp · 479 weeks ago
Okay, I get it now. There can't be a space between filename and the opening brace. For anyone more newb than me... NO SPACES!
xizdaqrian · 476 weeks and 4 days ago
tab completion or middle-click can be faster, that depends. anyway the game is to find the best solution in each case :P
ioggstream · 467 weeks ago
Another option would be to do [CTRL-W] to cut the filename, then [CTRL-Y][space][CTRL-Y].bak. If there are non-keyword characters (ie [[:alnum:]_] in the , you'll have to hit CTRL-W a few times consecutively.
intuited · 442 weeks and 6 days ago
Very handy when tab-complete does not work, and no mouse working from console :)
alf · 403 weeks ago
It's very useful~
deargodzw · 369 weeks ago
you can use : mv, diff, ...
Koobiac · 362 weeks and 4 days 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: