Last July, I wrote about two great uses for the cp command: making a backup of a file, and synchronizing a secondary copy of a folder.
Having found these nice utilities, I discover that they’re extra verbose than vital, so I created shortcuts to them in my Bash shell startup script. I believed I’d share these shortcuts in case they’re helpful to others or may supply inspiration to Bash customers who haven’t fairly taken on aliases or shell capabilities.
Updating a second copy of a folder – Bash alias
The common sample for updating a second copy of a folder with cp
is:
cp -r -u -v SOURCE-FOLDER DESTINATION-DIRECTORY
the place the -r
stands for “recursively descend through the folder visiting all files”, -u
stands for “update the target” and -v
stands for “verbose mode”, SOURCE-FOLDER
is the identify of the folder that accommodates probably the most up-to-date info, and DESTINATION-DIRECTORY
is the listing containing copy of the SOURCE-FOLDER
that have to be synchronized.
I can simply bear in mind the -r
possibility as a result of I exploit it usually when copying folders round. I can in all probability, with some extra effort, bear in mind -v
, and with much more effort, -u
(is it “update” or “synchronize” or…).
Or I can simply use the alias capability in Bash to transform the cp
command and choices to one thing extra memorable, like this:
alias sync='cp -r -u -v'
If I save this in my .bash_aliases
file in my house listing after which begin a brand new terminal session, I can use the alias, for instance:
sync Pictures /media/me/4388-E5FE
to synchronize my Pictures folder in my house listing with the model of the identical in my USB drive.
Not positive if you have already got a sync
alias outlined? You can record all of your presently outlined aliases by typing the phrase alias
on the command immediate in your terminal window.
Like this a lot you simply wish to begin utilizing it instantly? Open a terminal window and sort:
echo "alias sync='cp -r -u -v'" >> ~/.bash_aliases
Then begin up a brand new terminal window and sort the phrase alias
on the command immediate. You ought to see one thing like this:
me@mymachine~$ aliasalias alert='notify-send --urgency=low -i "$([ $? = 0 ] && echo terminal || echo error)" "$(historical past|tail -n1|sed -e '''s/^s*[0-9]+s*//;s/[;&|]s*alert$//''')"'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias gvm='sdk'
alias l='ls -CF'
alias la='ls -A'
alias ll='ls -alF'
alias ls='ls --color=auto'
alias sync='cp -r -u -v'
me@mymachine:~$
There you’ll be able to see the sync
alias outlined.
Making versioned backups – Bash operate
The common sample for making a backup of a file with cp
is:
cp --force --backup=numbered WORKING-FILE BACKED-UP-FILE
the place the -- pressure
stands for “make the copy no matter what”, the -- backup=numbered
stands for “use a number to indicate the generation of backup”, WORKING-FILE
is the present file we want to protect, and BACKED-UP-FILE
is similar identify because the WORKING-FILE
and could have the era info appended.
Besides remembering the choices to the cp
command, we additionally want to recollect to repeat the WORKING-FILE
identify a second time. But why repeat ourselves when a Bash function can handle that overhead for us, like this:
Again, it can save you this to your .bash_aliases
file in your house listing.
operate backupif [ $# -ne 1 ]; then
echo "Usage: $0 filename"
elif [ -f $1 ] ; then
echo "cp --force --backup=numbered $1 $1"
cp --force --backup=numbered $1 $1
else
echo "$0: $1 is not a file"
fi
I referred to as this operate “backup” as a result of I don’t have every other instructions referred to as “backup” on my system, however you’ll be able to select no matter identify fits.
The first if
assertion checks to ensure that just one argument is offered to the operate, in any other case printing the proper utilization with the echo
command.
The elif
assertion checks to verify the argument offered is a file, and if that’s the case, it (verbosely) makes use of the second echo
to print the cp
command for use after which executes it.
If the one argument will not be a file, the third echo
prints an error message to that impact.
In my house listing, if I execute the backup
command so outlined on the file checkCounts.sql
, I see that backup
creates a file referred to as checkCounts.sql.~1~
. If I execute it as soon as extra, I see a brand new file checkCounts.sql.~2~
.
Success! As deliberate, I can go on modifying checkCounts.sql
, but when I take a snapshot of it occasionally with backup, I can return to the latest snapshot ought to I run into hassle.
At some level, it’s higher to begin utilizing git
for model management, however backup
as outlined above is a pleasant low-cost device when it’s good to create snapshots however you’re not prepared for git
.
Conclusion
In my final article, I promised you that repetitive duties can usually be simply streamlined via the usage of shell scripts, shell capabilities, and shell aliases.
Here I’ve proven concrete examples of the usage of shell aliases and shell capabilities to streamline the synchronize and backup performance of the cp
command. If you’d prefer to be taught extra about this, take a look at the 2 articles cited above: How to save keystrokes at the command line with alias and Shell scripting: An introduction to the shift method and custom functions, written by my colleagues Greg and Seth, respectively.