### Here's how to streamline the backup and synchronize functions of the cp command.
![Two great uses for the cp command: Bash shortcuts ](https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/yearbook-haff-rx-linux-file-lead_0.png?itok=-i0NNfDC)
Last July, I wrote about[two great uses for the cp command][7]: making a backup of a file, and synchronizing a secondary copy of a folder.
Having discovered these great utilities, I find that they are more verbose than necessary, so I created shortcuts to them in my Bash shell startup script. I thought I’d share these shortcuts in case they are useful to others or could offer inspiration to Bash users who haven’t quite taken on aliases or shell functions.
### Updating a second copy of a folder – Bash alias
The general pattern for updating a second copy of a folder withcpis:
```
cp-r-u-vSOURCE-FOLDER DESTINATION-DIRECTORY
```
I can easily remember the-roption because I use it often when copying folders around. I can probably, with some more effort, remember-v, and with even more effort,-u(is it “update” or “synchronize” or…).
Or I can just use the[alias capability in Bash][8]to convert thecpcommand and options to something more memorable, like this:
```
aliassync='cp -r -u -v'
```
```
syncPictures/media/me/4388-E5FE
```
Not sure if you already have asyncalias defined? You can list all your currently defined aliases by typing the wordaliasat the command prompt in your terminal window.
Like this so much you just want to start using it right away? Open a terminal window and type:
Besides remembering the options to thecpcommand, we also need to remember to repeat theWORKING-FILEname a second time. But why repeat ourselves when[a Bash function][9]can take care of that overhead for us, like this:
Again, you can save this to your.bash_aliasesfile in your home directory.
```
functionbackup{
if[$#-ne1];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
}
```
The firstifstatement checks to make sure that only one argument is provided to the function, otherwise printing the correct usage with theechocommand.
Theelifstatement checks to make sure the argument provided is a file, and if so, it (verbosely) uses the secondechoto print thecpcommand to be used and then executes it.
If the single argument is not a file, the thirdechoprints an error message to that effect.
In my home directory, if I execute thebackupcommand so defined on the filecheckCounts.sql, I see thatbackupcreates a file calledcheckCounts.sql.~1~. If I execute it once more, I see a new filecheckCounts.sql.~2~.
Success! As planned, I can go on editingcheckCounts.sql, but if I take a snapshot of it every so often with backup, I can return to the most recent snapshot should I run into trouble.
At some point, it’s better to start usinggitfor version control, butbackupas defined above is a nice cheap tool when you need to create snapshots but you’re not ready forgit.
### Conclusion
In my last article, I promised you that repetitive tasks can often be easily streamlined through the use of shell scripts, shell functions, and shell aliases.
Here I’ve shown concrete examples of the use of shell aliases and shell functions to streamline the synchronize and backup functionality of thecpcommand. If you’d like to learn more about this, check out the two articles cited above:[How to save keystrokes at the command line with alias][10]and[Shell scripting: An introduction to the shift method and custom functions][11], written by my colleagues Greg and Seth, respectively.
### About the author
[![](https://opensource.com/sites/default/files/styles/profile_pictures/public/clh_portrait2.jpg?itok=V1V-YAtY)][13] Chris Hermansen
Engaged in computing since graduating from the University of British Columbia in 1978, I have been a full-time Linux user since 2005 and a full-time Solaris, SunOS and UNIX System V user before that. On the technical side of things, I have spent a great deal of my career doing data analysis; especially spatial data analysis. I have a substantial amount of programming experience in relation to data analysis, using awk, Python, PostgreSQL, PostGIS and lately Groovy. I have also built a few...[more about Chris Hermansen][14]