Bash script for beginners

June 25, 2012 in Bash script

Bash scripting for beginners Part 2

A simple backup script

Now we will create the first version of this backup script. So what this script will do? It will create a compressed file in the backup folder from the folder that we store our scripts.

Let’s create the Bash script file as before, but now we will name that script backup.sh:

gedit ~/Desktop/bash/code/backup.sh

write the following to the gedit :

#!/bin/bash  
# Bash with allaboutlinux.eu
tar -zcvf ~/Desktop/bash/backup/backup.tar.gz ~/Desktop/bash/code/
echo "backup saved to ~/Desktop/bash/backup/backup.tar.gz"

save and close. Now make the file executable

chmod +x backup.sh

execute that file with

./backup.sh

Just a quick explanation of the script:

The first line is comment. So no comment  😀

The second line is actually the main program it compresses a folder to a file and it works like that:

“tar -zcvf” is the tool that we use here to compress the folder followed by “~/Desktop/bash/backup/backup.tar.gz” which is the location to store the backup file with the file name, and last by “~/Desktop/bash/code/” which is the folder that we want to backup. Maybe that looks a bit complicated but if you take a good look at the script you will see that is easier than you think.

The third line is just a message which you know from the previous part.

From now on I will not explain parts of the script that already have been explained so at any time you want to check something just turn back and take a look on the previous parts of this tutorial.

Now you have a script that can run and make a backup of the folder ~/Desktop/bash/code. If you are ready let’s proceed to the next part and make our script a bit better.

Pages: 1 2 3 4 5 6 7 8