Search⌘ K
AI Features

Using Bash Script

Explore how to create a Bash script to automate backup tasks and understand the importance of file permissions and the shebang line. Learn how to run scripts directly, modify permissions with chmod, and ensure the correct interpreter executes your script for consistent behavior on different systems.

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 the Bash script file. We call ...