Is there any way to create backup copy of a file, without type its name twice?
Solution 1
You could use brace expansion like this:
cp example_file{,.bak}
Solution 2
1. What you asked for
You can create a small shellscript file bupper
:
I have a directory ~/bin
, where I keep such help files.
#!/bin/bash
if [ $# -eq 1 ]
then
cp -pvi "$1" "${1}.bak"
else
echo "Info: $0 copies to a backup file"
echo "Usage: $0 <file to be backed up with .bak extension>"
fi
Make it executable,
chmod ugo+x bupper
When in ~/bin
, it will be in PATH and you can run it like any executable program anywhere (where you have write permissions).
Example:
$ bupper hello.txt
'hello.txt' -> 'hello.txt.bak'
$ bupper hello.txt
cp: overwrite 'hello.txt.bak'? n
$ bupper hello.txt
cp: overwrite 'hello.txt.bak'? y
'hello.txt' -> 'hello.txt.bak'
2. Alternative - let the editor do the job automatically
Some editors have an option to create a backup copy of the file before you save a new version. This backup has often a tilde as the last character (tilde is the extension, but there is no dot before it).
Gedit, the standard editor in Ubuntu is one of them.
After setting gedit
to save such a backup copy:
gedit hello.txt
And check afterwards
$ ls hello.txt*
hello.txt hello.txt~ hello.txt.bak
Now hello.txt~
has been added to hello.txt
and the backup created by bupper
.
This works with nano
too, with the option -B
nano -B hello.txt
so you can do it with a command line editor for 'sudo' tasks :-)
Related videos on Youtube
![pa4080](https://i.stack.imgur.com/Lrlbx.jpg?s=256&g=1)
pa4080
I have a degree as a Mechanical Engineer and Doctor of Engineering Science as well. However, since I am self-educated at Computer Science and English language, please be tolerant at my mistakes and feel free to correct them when it is necessary. Actually my real name is Sраs Zdrаvkоv Sраsоv. In my birthplace Pacho is "short" for Spas. So the first two letters of my nickname - 'pa' - comes from there. Written in Bulgarian, the number '4' starts with the letter 'ч' that is pronounced as 'ch'. The number '0' looks like the letter 'o'. So we have 'pa40', and finally '80' is my birth year ;) This nickname originates from the time before ICQ and mIRC was modern. Create Digital Ocean account and get $100 in credit to use for 2 months just for signing up.
Updated on September 18, 2022Comments
-
pa4080 almost 2 years
Often when I'm editing some system file first I create a backup copy. For example:
sudo cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak
Is there any simple 'shortcut' such as:
sudo cp /etc/ssh/sshd_config %s.bak
?
A workaround that I found is to use
sed
in this way:sudo sed '' /etc/ssh/sshd_config -i.bak
-
George Udosen over 6 yearsOh, so nice! +1...
-
pa4080 over 6 yearsThank you for this answer. This was my first idea, but I must have script as this on every machine which I'm using.
-
sudodus over 6 yearsYou beat me, very elegant :-)
-
sudodus over 6 years@pa4080, 75andyd's solution is elegant and portable (when you have learned it by heart). But I edited my answer to add a workaround - to let the editor do the job automatically. You can make Gedit create a backup file 'hello.txt' -> 'hello.txt~' and there are other editors that have the same option. This alternative is very convenient.
-
shouldsee over 5 yearsto be safer
cp -n example_file{,.bak}
-
hakre over 3 yearslet (GNU) cp handle the backup:
cp -v -f -b -T file{,}
(also numbered backups are available) -
Victor S. about 3 yearsto be even more safer
cp -nv example_file{,.bak};
-
Cymatical almost 3 yearsnano -B only seems to have one backup version ie notes.txt~