This bash example creates an archive from /home directory to /backup/ directory as one tar.gz file. Let's create a file backup.sh. It will consist of two lines:
#!/bin/bash
tar -czf /var/home-backup.tar.gz /home/
First line is a hashpling. Basically, it says who execute script. In this example, we choose /bin/bash.
The second line is tar command. It tarballs and compress whole directory (/home) to one file.
I recommend you give also the third line (empty line). Why? If you execute this script in unusual UNIXes (ec. SCO UNIX), UNIX coudn't execute last line, becasuse last symbol of file - EOF (end of file) is different from symbol EOLN (end of line). Symbol EOLN (enter) or semicolon (;) executes command.
Would you to extend this script to some output? Here is an example.
#!/bin/bash
echo -n Creating backup of home directory to /backup...
tar -czf /var/home-backup.tar.gz /home/ >/dev/null 2>&1
echo done.
On the second line, echo with n parameter doesn't give a new line.
On the third line, output from tar command is redirected to trash (/dev/null).
Last line, just echoes done.
If you don't know what to put on the first line (hashpling) type:
echo $SHELL
Output: /bin/bash
You will get path to your shell, which can use in the hashspling.
Was this information helpful to you? You have the power to keep it alive.
Each donated € will be spent on running and expanding this page about UNIX Shell.
We prepared for you video course Marian's BASH Video Training: Mastering Unix Shell, if you would like to get much more information.
Thank you. Marian Knezek