Using Bash Script

Create your first Bash script and run it.

Bash script

Let’s create a Bash script that does our backup command. Here are the steps for doing that:

  1. Run the following command:
    vim ~/photo-backup.sh
    
Bash script
  1. Copy the following backup command:

    #!/bin/bash
    (tar -cjf ~/photo.tar.bz2 ~/photo &&
      echo "tar - OK" > results.txt ||
      ! echo "tar - FAILS" > results.txt) &&
    (cp -f ~/photo.tar.bz2 ~/backup &&
      echo "cp - OK" >> results.txt ||
      ! echo "cp - FAILS" >> results.txt)
    
  2. Paste the command in the terminal.

  3. Press the esc key on the keyboard and then type :x! to save the file.

To quit without saving we can press the ecs key on the keyboard and then type :q!.

  1. View the file created in the home directory with the photo-backup.sh name by:
    cat ~/photo-backup.sh
    

Now, we have ...