Welcome to Linux Bash!

Thank you for visiting our website, here you will find the best content for managing your servers operating system, Linux.

Bash (Bourne Again Shell) is the free and enhanced version of the Bourne shell distributed with Linux and GNU operating systems. Linux comes in a few flavours but all run the same Linux Bash format. Bash is a command processor that typically runs in a text window where the user types commands that cause actions. Bash can also read and execute commands from a file, called a shell script. In order to take advantage of all Linux' features, a good understanding of Bash is advised.

At first sight Bash appears to be a simple command/reply system, where users enter commands and Bash returns the results after those commands are run. However, Linux Bash is more than that, it is a programming platform whereby users are able to write programs that accept input and produce output using commands in scripts. A shell then runs these commands from the executable files (a.k.a. scripts). Scripts are often given the sh file type, which is where we got inspiration for our project domain, linuxbash.sh

hello world Welcome! Here you will find ways to get started or pickup tips with Linux Bash. linuxbash.sh For more information see our categories.

Recent posts

  • Posted on

    In Bash scripting, functions are used to group a set of commands that perform a specific task. Functions can be called multiple times within a script, making your code cleaner, reusable, and easier to maintain.

    1. Defining a Function in Bash

    A function in Bash can be defined using two main syntax formats:

    Syntax 1: Using function keyword

    function function_name {
      # Commands to be executed
    }
    

    Syntax 2: Without the function keyword (more common)

    function_name() {
      # Commands to be executed
    }
    

    The second format is the more common one and is often preferred for simplicity.

    Example of Function Definition

    greet() {
      echo "Hello, $1!"  # $1 is the first argument passed to the function
    }
    

    2. Calling a Function

    Once a function is defined, you can call it by simply using its name, followed by any arguments if needed.

    Example of Function Call

    greet "Alice"
    

    Output:

    Hello, Alice!
    

    3. Passing Arguments to a Function

    Functions in Bash can accept arguments (parameters) when called. These arguments are accessed using $1, $2, etc., where $1 is the first argument, $2 is the second, and so on. $0 refers to the script's name.

    Example: Function with Arguments

    add_numbers() {
      sum=$(( $1 + $2 ))
      echo "The sum of $1 and $2 is $sum"
    }
    
    add_numbers 5 10
    

    Output:

    The sum of 5 and 10 is 15
    

    4. Returning Values from a Function

    In Bash, functions do not have a built-in return type like other programming languages (e.g., int, string). Instead, a function can return a value in two ways:

    1. Using echo or printf: You can print a value from the function, and the calling code can capture this output.
    2. Using return: This returns an exit status (0-255), which is typically used for success or failure indicators.

    Example 1: Using echo to return a value

    multiply() {
      result=$(( $1 * $2 ))
      echo $result  # Output the result
    }
    
    result=$(multiply 4 3)  # Capture the output of the function
    echo "The result is $result"
    

    Output:

    The result is 12
    

    Example 2: Using return for status (exit code)

    check_even() {
      if (( $1 % 2 == 0 )); then
        return 0  # Return 0 (success) for even numbers
      else
        return 1  # Return 1 (failure) for odd numbers
      fi
    }
    
    check_even 4
    if [ $? -eq 0 ]; then
      echo "4 is even."
    else
      echo "4 is odd."
    fi
    

    Output:

    4 is even.
    

    The special variable $? stores the exit status of the last executed command. A return value of 0 typically indicates success, while non-zero values indicate failure.

    5. Local Variables in Functions

    By default, variables inside a function are global in Bash, which means they can be accessed from anywhere in the script. To make a variable local to a function (i.e., it only exists inside that function), use the local keyword.

    Example: Local Variables

    my_function() {
      local var=10  # This variable is local to the function
      echo "Inside function: $var"
    }
    
    my_function
    echo "Outside function: $var"  # $var is not defined outside the function
    

    Output:

    Inside function: 10
    Outside function:
    

    6. Function with No Arguments

    A function can be defined and called without any arguments. The function can still perform useful tasks based on hardcoded values or other data from the script.

    Example: Function with No Arguments

    say_hello() {
      echo "Hello, World!"
    }
    
    say_hello
    

    Output:

    Hello, World!
    

    7. Returning Multiple Values from a Function

    Since Bash functions can only return one value via return (an exit status), if you need to return multiple values, the usual approach is to print the values and capture them using command substitution or use arrays.

    Example: Returning Multiple Values

    calculate() {
      sum=$(( $1 + $2 ))
      diff=$(( $1 - $2 ))
      echo "$sum $diff"  # Output both values separated by a space
    }
    
    result=$(calculate 10 5)
    sum=$(echo $result | awk '{print $1}')
    diff=$(echo $result | awk '{print $2}')
    
    echo "Sum: $sum, Difference: $diff"
    

    Output:

    Sum: 15, Difference: 5
    

    8. Default Arguments and Error Handling in Functions

    You can provide default values for arguments using Bash's conditional constructs (${1:-default_value}), and you can handle errors within functions using return or exit.

    Example: Function with Default Argument

    greet_user() {
      local name=${1:-"Guest"}  # If no argument is passed, use "Guest" as default
      echo "Hello, $name!"
    }
    
    greet_user "Alice"  # Outputs: Hello, Alice!
    greet_user          # Outputs: Hello, Guest!
    

    Example: Error Handling in Functions

    divide() {
      if [ $2 -eq 0 ]; then
        echo "Error: Division by zero!"
        return 1  # Exit with error code
      fi
      echo "Result: $(( $1 / $2 ))"
    }
    
    divide 10 2
    divide 10 0  # Error: Division by zero!
    

    Output:

    Result: 5
    Error: Division by zero!
    

    9. Function Scope

    In Bash, by default, variables are global, but functions can also define local variables using the local keyword. This ensures that variables do not conflict with those defined outside the function.

    10. Example: Putting It All Together

    Here’s an example script demonstrating all the concepts above:

    #!/bin/bash
    
    # Function to add two numbers
    add_numbers() {
      local sum=$(( $1 + $2 ))
      echo $sum
    }
    
    # Function to greet a user
    greet_user() {
      local name=${1:-"Guest"}  # Default name is Guest
      echo "Hello, $name!"
    }
    
    # Function to calculate and return multiple values
    calculate() {
      local sum=$(( $1 + $2 ))
      local diff=$(( $1 - $2 ))
      echo "$sum $diff"
    }
    
    # Calling functions
    greet_user "Alice"
    result=$(add_numbers 10 5)
    echo "The sum of 10 and 5 is: $result"
    
    # Getting multiple values from a function
    result=$(calculate 20 4)
    sum=$(echo $result | awk '{print $1}')
    diff=$(echo $result | awk '{print $2}')
    echo "Sum: $sum, Difference: $diff"
    

    Summary of Key Concepts:

    • Defining a function: function_name() { commands; }
    • Calling a function: function_name
    • Arguments: $1, $2, etc.
    • Return values: Use echo for multiple values or output, and return for exit codes (0 for success, non-zero for errors).
    • Local variables: Use the local keyword to restrict a variable to the function scope.
    • Default values: Provide default argument values using ${1:-default_value}.
    • Error handling: Use return to indicate errors within functions.

    Using functions in Bash allows you to modularize your code, improving readability and maintainability while also making it more reusable.

  • Posted on

    Process management is a key concept when working with Bash and Linux/Unix-like systems. It involves handling the execution of programs or commands, tracking their status, and controlling their execution flow. In Bash, you can manage processes in several ways: running background processes, managing jobs, and using tools like ps to monitor processes. Below is an explanation of background processes, jobs, and how to use ps for process management.

    1. Background Processes

    A background process in Bash runs independently of the terminal session, allowing you to continue using the terminal while the process executes. This is useful for long-running tasks or when you need to run multiple tasks simultaneously.

    Running a Command in the Background

    To run a command in the background, append an & at the end of the command.

    sleep 60 &  # Run the sleep command in the background
    
    • The process starts running in the background, and Bash returns the prompt to you immediately.
    • The process will continue running even if you close the terminal, unless it's explicitly tied to the terminal session.

    Example:

    $ sleep 60 &
    [1] 12345
    

    Here, [1] is the job number, and 12345 is the process ID (PID) of the background process.

    2. Job Control in Bash

    Bash supports job control, which allows you to manage multiple processes that you have started in the background or in the foreground. You can suspend jobs, bring them to the foreground, or kill them.

    Listing Jobs

    To list the current jobs running in the background, use the jobs command:

    jobs
    

    Output example:

    [1]+ 12345 Running                 sleep 60 &
    [2]- 12346 Running                 sleep 100 &
    

    Each job has a job number (e.g., [1], [2]) and a process ID (PID). The + and - symbols represent the most recent job and the previous job, respectively.

    Bringing a Background Job to the Foreground

    To bring a background job to the foreground, use the fg command followed by the job number:

    fg %1
    

    This will bring job 1 (the one with job number [1]) to the foreground.

    Sending a Job to the Background

    If you've stopped a foreground job (e.g., by pressing Ctrl+Z), you can send it back to the background with the bg command:

    bg %1
    

    This resumes job 1 in the background.

    Stopping a Job

    If you want to stop a running job, you can suspend it by pressing Ctrl+Z. This sends a SIGTSTP signal to the process, which halts its execution temporarily.

    You can also use the kill command to send a termination signal (SIGTERM):

    kill %1  # Kill job 1
    

    To forcefully terminate a process, use the -9 option:

    kill -9 %1  # Force kill job 1
    

    Example: Job Control in Action

    $ sleep 100 &
    [1] 12345
    $ jobs
    [1]+ 12345 Running                 sleep 100 &
    $ fg %1
    sleep 100
    # Press Ctrl+Z to stop the job
    $ jobs
    [1]+ 12345 Stopped                 sleep 100
    $ bg %1
    [1]+ 12345 Running                 sleep 100 &
    

    3. Using ps to Monitor Processes

    The ps (process status) command is used to display information about running processes. It’s a versatile tool for monitoring system activity.

    Basic ps Command

    By default, ps shows processes running in the current terminal session:

    ps
    

    Output example:

    PID TTY          TIME CMD
    12345 pts/1    00:00:00 bash
    12346 pts/1    00:00:00 ps
    
    • PID: Process ID
    • TTY: Terminal associated with the process
    • TIME: CPU time the process has consumed
    • CMD: The command running

    Viewing All Processes

    To see all processes running on the system, use the -e or -A option:

    ps -e
    

    Or:

    ps -A
    

    This lists every process on the system, not just those tied to the current session.

    Viewing Detailed Information

    For more detailed information, use the -f (full-format listing) option:

    ps -ef
    

    This displays additional columns such as the parent process ID (PPID), user, and more.

    Output example:

    UID        PID  PPID  C STIME TTY      TIME     CMD
    1000      12345  1234  0 10:00 pts/1  00:00:00 bash
    1000      12346  12345  0 10:00 pts/1  00:00:00 ps
    
    • UID: User ID
    • PID: Process ID
    • PPID: Parent process ID
    • C: CPU utilization
    • STIME: Start time
    • TTY: Terminal type
    • TIME: Total CPU time used
    • CMD: Command name

    Viewing Process Tree

    You can view processes in a hierarchical tree-like format using the --forest option with ps:

    ps -ef --forest
    

    This shows the parent-child relationships between processes, which is useful for understanding how processes are spawned.

    Filtering with ps

    You can filter processes based on certain criteria using options like -p (for a specific PID) or -u (for a specific user).

    Example: View process for a specific PID:

    ps -p 12345
    

    Example: View processes for a specific user:

    ps -u username
    

    4. Other Useful Process Management Commands

    • top: Displays an interactive, real-time view of system processes, including resource usage (CPU, memory).

      top
      
    • htop: A more user-friendly, interactive version of top with additional features.

      htop
      
    • kill: Used to send signals to processes (e.g., terminate them).

      kill PID
      kill -9 PID  # Force kill
      
    • nice: Used to set process priority (CPU scheduling). A process with a lower priority will get less CPU time.

      nice -n 10 command
      
    • renice: Adjust the priority of a running process.

      renice -n 10 -p PID
      

    Summary of Key Commands:

    • Background process: Run with &.
    • Jobs: Use jobs, fg, bg to manage jobs.
    • Process status: Use ps, top, and htop to monitor processes.
    • Kill process: Use kill or kill -9 to terminate processes.
    • Managing priorities: Use nice and renice to manage process priorities.

    Mastering these process management tools will help you efficiently manage multiple tasks and optimize your system's performance in Bash.

  • Posted on

    Loops in Bash are essential for automating repetitive tasks, iterating through lists, or executing commands multiple times. Bash provides three primary types of loops: for, while, and until. Each has its own use cases and syntax.

    1. for Loop

    The for loop in Bash is used to iterate over a list of items (such as numbers, files, or strings) and execute a block of code for each item.

    Syntax:

    for variable in list
    do
      # Commands to execute
    done
    

    Example 1: Iterating Over a List of Items

    for fruit in apple banana cherry
    do
      echo "I love $fruit"
    done
    

    Output:

    I love apple
    I love banana
    I love cherry
    

    Example 2: Iterating Over a Range of Numbers (using {})

    for i in {1..5}
    do
      echo "Number $i"
    done
    

    Output:

    Number 1
    Number 2
    Number 3
    Number 4
    Number 5
    

    Example 3: Iterating with Step Size

    You can specify a step size when iterating over a range using the seq command or a specific step in the {} range.

    for i in {1..10..2}
    do
      echo "Odd number: $i"
    done
    

    Output:

    Odd number: 1
    Odd number: 3
    Odd number: 5
    Odd number: 7
    Odd number: 9
    

    Alternatively, using seq:

    for i in $(seq 1 2 10)
    do
      echo "Odd number: $i"
    done
    

    2. while Loop

    The while loop runs as long as a given condition is true. It is useful when you don't know how many times you need to iterate, but you have a condition to check before continuing the loop.

    Syntax:

    while condition
    do
      # Commands to execute
    done
    

    Example 1: Basic while Loop

    count=1
    while [ $count -le 5 ]
    do
      echo "Count is $count"
      ((count++))  # Increment count by 1
    done
    

    Output:

    Count is 1
    Count is 2
    Count is 3
    Count is 4
    Count is 5
    

    Example 2: Looping Until a Condition is Met

    You can use a while loop to keep iterating as long as a condition is true (or until it's false).

    count=5
    while [ $count -gt 0 ]
    do
      echo "Count is $count"
      ((count--))  # Decrement count by 1
    done
    

    Output:

    Count is 5
    Count is 4
    Count is 3
    Count is 2
    Count is 1
    

    3. until Loop

    The until loop works similarly to the while loop, but it continues as long as the condition is false. It’s used when you want to execute commands until a certain condition becomes true.

    Syntax:

    until condition
    do
      # Commands to execute
    done
    

    Example 1: Basic until Loop

    count=1
    until [ $count -gt 5 ]
    do
      echo "Count is $count"
      ((count++))  # Increment count by 1
    done
    

    Output:

    Count is 1
    Count is 2
    Count is 3
    Count is 4
    Count is 5
    

    Example 2: Infinite until Loop (with a break)

    You can also create an infinite until loop. This is often used with a break statement to stop the loop when a certain condition is met.

    count=1
    until [ $count -gt 5 ]
    do
      echo "Count is $count"
      ((count++))
      if [ $count -eq 3 ]; then
        echo "Stopping at count 3"
        break
      fi
    done
    

    Output:

    Count is 1
    Count is 2
    Count is 3
    Stopping at count 3
    

    4. Loop Control Statements

    • break: Exits the loop prematurely.
    • continue: Skips the rest of the current iteration and moves to the next one.

    Example with break:

    for i in {1..5}
    do
      if [ $i -eq 3 ]; then
        echo "Breaking at $i"
        break
      fi
      echo "Number $i"
    done
    

    Output:

    Number 1
    Number 2
    Breaking at 3
    

    Example with continue:

    for i in {1..5}
    do
      if [ $i -eq 3 ]; then
        continue  # Skip the rest of the loop for i=3
      fi
      echo "Number $i"
    done
    

    Output:

    Number 1
    Number 2
    Number 4
    Number 5
    

    5. Nested Loops

    You can nest loops within each other to perform more complex tasks.

    Example: Nested for Loops

    for i in {1..3}
    do
      for j in {1..2}
      do
        echo "i=$i, j=$j"
      done
    done
    

    Output:

    i=1, j=1
    i=1, j=2
    i=2, j=1
    i=2, j=2
    i=3, j=1
    i=3, j=2
    

    Example: Nested while Loop

    i=1
    while [ $i -le 3 ]
    do
      j=1
      while [ $j -le 2 ]
      do
        echo "i=$i, j=$j"
        ((j++))
      done
      ((i++))
    done
    

    Output:

    i=1, j=1
    i=1, j=2
    i=2, j=1
    i=2, j=2
    i=3, j=1
    i=3, j=2
    

    Summary of Loops in Bash:

    1. for loop: Iterates over a list of items (or range) and executes commands for each item.

      • Best for known iterations or ranges.
    2. while loop: Executes commands as long as the condition is true.

      • Useful when you want to repeat something until a condition changes.
    3. until loop: Executes commands until the condition becomes true.

      • Opposite of the while loop; it stops when the condition is true.
    4. Loop control: Use break to exit early or continue to skip the current iteration.

    By mastering these loops and their variations, you'll be able to automate a wide range of tasks in Bash effectively!

  • Posted on

    Securing Bash scripts is essential to prevent unauthorized access, accidental errors, or malicious activity. Here are best practices to secure your Bash scripts:

    1. Use Absolute Paths

    Always use absolute paths for commands and files to avoid ambiguity and to prevent the execution of unintended commands.

    Example:

    # Incorrect
    rm -rf /tmp/*
    
    # Correct
    /bin/rm -rf /tmp/*
    

    This ensures that the correct program is used, regardless of the user's environment or $PATH settings.

    2. Avoid Using sudo or root Privileges in Scripts

    If possible, avoid running scripts with sudo or root privileges. If root access is necessary, be explicit about which commands need it, and ensure they are used sparingly.

    • Run only the necessary commands with sudo or root privileges.
    • Consider using sudo with limited privileges (using sudoers file) to allow only certain actions.

    Example (to limit permissions in sudoers file):

    user ALL=(ALL) NOPASSWD: /path/to/safe/command
    

    3. Sanitize User Input

    Validate and sanitize all user input, especially when it's passed to commands, to prevent malicious injection, such as code injection or command substitution attacks.

    Example:

    # Avoid running commands directly with user input
    read user_input
    # Vulnerable to command injection
    
    # Better approach: sanitize input
    if [[ "$user_input" =~ ^[a-zA-Z0-9_]+$ ]]; then
      # Safe to proceed with the input
      echo "Valid input: $user_input"
    else
      echo "Invalid input"
      exit 1
    fi
    

    4. Use Shellcheck for Script Linting

    Use tools like ShellCheck to lint your scripts. It helps to catch errors, warnings, and potential security issues in your code.

    shellcheck script.sh
    

    5. Set Proper File Permissions

    Set appropriate permissions for your script files to ensure they can only be executed by authorized users. You can use chmod to set permissions:

    chmod 700 /path/to/script.sh  # Only the owner can read, write, or execute
    

    6. Use set -e to Exit on Errors

    Use set -e (also known as set -o errexit) to ensure that your script exits as soon as any command fails. This can help avoid unintended behavior.

    #!/bin/bash
    set -e  # Exit on error
    

    You can also use set -u (also set -o nounset) to make your script fail if it tries to use undefined variables:

    set -u  # Treat unset variables as an error
    

    7. Quote Variables Properly

    Always quote variables to prevent word splitting or globbing issues, which can be a security risk.

    Example:

    # Vulnerable to word splitting or globbing
    file="/path/to/directory/*"
    rm $file  # This can delete unintended files
    
    # Safe way
    rm "$file"
    

    8. Log Sensitive Information Carefully

    Avoid logging sensitive information such as passwords, keys, or tokens in clear text. If necessary, ensure logs are stored securely.

    Example:

    # Don't log passwords directly
    echo "Password is: $password"  # Not secure
    
    # Instead, log securely (e.g., obfuscated or masked)
    echo "Password update successful"  # Better approach
    

    9. Limit Access to Sensitive Files

    If your script needs to access sensitive files (e.g., configuration files, private keys), make sure those files are protected with the right permissions and ownership.

    # Set permissions to restrict access to sensitive files
    chmod 600 /path/to/sensitive/file
    

    10. Avoid Hardcoding Credentials

    Never hardcode sensitive credentials such as passwords, API keys, or tokens directly in your script. Instead, use environment variables, configuration files with restricted access, or secret management systems.

    Example:

    # Avoid hardcoding secrets in the script
    api_key="your-api-key"
    
    # Better approach: Use environment variables
    export API_KEY="your-api-key"
    

    11. Use Secure Communication (TLS/SSL)

    If your script communicates over a network, always use secure protocols like HTTPS instead of HTTP. Ensure that communication is encrypted, especially when transmitting sensitive data.

    Example:

    # Vulnerable (non-secure communication)
    curl http://example.com
    
    # Secure (encrypted communication)
    curl https://example.com
    

    12. Regularly Update and Patch Dependencies

    Ensure that the tools and libraries your script depends on are kept up-to-date with the latest security patches. Regularly review the security of the script and its dependencies.

    13. Use Proper Exit Statuses

    Return appropriate exit statuses (0 for success, non-zero for failure) to indicate the result of the script’s execution. This allows better error handling and debugging.

    Example:

    #!/bin/bash
    if some_command; then
      echo "Command succeeded"
      exit 0
    else
      echo "Command failed"
      exit 1
    fi
    

    14. Use Restricted Shell (rbash) or AppArmor/SELinux

    If the script is running on a multi-user system, consider restricting the environment with tools like rbash (restricted Bash shell) or enforce security policies with AppArmor or SELinux. These tools help limit what users can do, even if they gain access to the script.

    15. Testing in a Safe Environment

    Before running a script in a production environment, test it in a controlled, isolated environment. This helps to ensure that the script works as expected without causing unintended harm.


    By following these best practices, you can significantly improve the security of your Bash scripts, minimizing the risks associated with running or sharing scripts in multi-user or production environments.

  • Posted on

    The tar command in Bash is commonly used to create archives of files and directories. It can compress or just archive the data, and it supports several formats such as .tar, .tar.gz, .tar.bz2, .tar.xz, etc.

    Here's a breakdown of how you can use tar for various purposes:

    1. Creating an Archive (without compression)

    To create a .tar archive from files or directories:

    tar -cvf archive_name.tar /path/to/directory_or_file
    
    • -c: Create a new archive
    • -v: Verbose mode (optional, shows the progress)
    • -f: Specify the name of the archive

    Example:

    tar -cvf backup.tar /home/user/documents
    

    This will create an archive backup.tar containing the contents of the /home/user/documents directory.

    2. Creating a Compressed Archive

    You can compress the archive using different compression algorithms:

    a. With gzip (creates a .tar.gz or .tgz file):

    tar -czvf archive_name.tar.gz /path/to/directory_or_file
    
    • -z: Compress with gzip

    Example:

    tar -czvf backup.tar.gz /home/user/documents
    

    b. With bzip2 (creates a .tar.bz2 file):

    tar -cjvf archive_name.tar.bz2 /path/to/directory_or_file
    
    • -j: Compress with bzip2

    Example:

    tar -cjvf backup.tar.bz2 /home/user/documents
    

    c. With xz (creates a .tar.xz file):

    tar -cJvf archive_name.tar.xz /path/to/directory_or_file
    
    • -J: Compress with xz

    Example:

    tar -cJvf backup.tar.xz /home/user/documents
    

    3. Extracting an Archive

    To extract files from a .tar archive:

    tar -xvf archive_name.tar
    

    For compressed archives, replace .tar with the appropriate extension (e.g., .tar.gz, .tar.bz2).

    Extracting .tar.gz:

    tar -xzvf archive_name.tar.gz
    

    Extracting .tar.bz2:

    tar -xjvf archive_name.tar.bz2
    

    Extracting .tar.xz:

    tar -xJvf archive_name.tar.xz
    

    4. Listing the Contents of an Archive

    To see the contents of a .tar file without extracting it:

    tar -tvf archive_name.tar
    

    For compressed files, you can use the same command but replace the extension appropriately.

    5. Extracting to a Specific Directory

    If you want to extract files to a specific directory, use the -C option:

    tar -xvf archive_name.tar -C /path/to/extract/directory
    

    6. Adding Files to an Existing Archive

    To add files or directories to an existing archive:

    tar -rvf archive_name.tar /path/to/new_file_or_directory
    
    • -r: Append files to an archive

    7. Excluding Files from an Archive

    To exclude specific files or directories while archiving:

    tar -cvf archive_name.tar --exclude='*.log' /path/to/directory
    

    This command excludes all .log files from the archive.

    8. Extracting Specific Files from an Archive

    To extract a specific file from an archive:

    tar -xvf archive_name.tar path/to/file_within_archive
    

    This will extract only the specified file from the archive.

    Summary of Useful tar Options:

    • -c: Create an archive
    • -x: Extract an archive
    • -v: Verbose output
    • -f: Specify the archive file name
    • -z: Compress using gzip
    • -j: Compress using bzip2
    • -J: Compress using xz
    • -C: Extract to a specific directory
    • --exclude: Exclude specific files or directories
    • -r: Append files to an existing archive
    • -t: List contents of an archive

    These are some of the common usages of tar to archive and compress files in Bash.