Linux case Command with Practical Examples

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, you will learn how to use the Linux case command to handle different file extensions in a shell script. The lab covers the syntax and usage of the case command, demonstrates how to implement case statements for file extensions, and shows how to automate backup operations using the case command. This lab is designed to help you develop your skills in working with miscellaneous utilities in the Linux environment.

The lab starts by explaining the syntax and usage of the case command, which is a powerful tool for making decisions based on the value of a variable or expression. You will learn how to structure the case statement and see examples of its practical application.

Next, the lab guides you through the process of implementing case statements to handle different file extensions. You will create a shell script that performs various actions based on the file type, such as identifying text documents, PDF files, and images.

Finally, the lab demonstrates how to use the case command to automate backup operations. You will learn how to create a script that backs up files with specific extensions to different locations, making it easier to manage and organize your data.

Linux Commands Cheat Sheet


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/BasicSystemCommandsGroup(["`Basic System Commands`"]) linux(("`Linux`")) -.-> linux/BasicFileOperationsGroup(["`Basic File Operations`"]) linux/BasicSystemCommandsGroup -.-> linux/echo("`Text Display`") linux/BasicSystemCommandsGroup -.-> linux/read("`Input Reading`") linux/BasicFileOperationsGroup -.-> linux/cp("`File Copying`") linux/BasicFileOperationsGroup -.-> linux/touch("`File Creating/Updating`") subgraph Lab Skills linux/echo -.-> lab-422588{{"`Linux case Command with Practical Examples`"}} linux/read -.-> lab-422588{{"`Linux case Command with Practical Examples`"}} linux/cp -.-> lab-422588{{"`Linux case Command with Practical Examples`"}} linux/touch -.-> lab-422588{{"`Linux case Command with Practical Examples`"}} end

Understand the Syntax and Usage of the case Command

In this step, you will learn the syntax and usage of the case command in Linux. The case command is a powerful tool for making decisions based on the value of a variable or expression.

The basic syntax of the case command is as follows:

case expression in
  pattern1)
    commands
    ;;
  pattern2)
    commands
    ;;
  ...
  *)
    default commands
    ;;
esac

Here's an example that demonstrates the usage of the case command:

echo "Enter a file extension: "
read file_ext

case "$file_ext" in
  "txt")
    echo "The file is a text document."
    ;;
  "pdf")
    echo "The file is a PDF document."
    ;;
  "jpg" | "png" | "gif")
    echo "The file is an image."
    ;;
  *)
    echo "Unsupported file type."
    ;;
esac

Example output:

Enter a file extension:
pdf
The file is a PDF document.

In the example above, the case command checks the value of the file_ext variable and executes the corresponding commands based on the pattern match. The * pattern is used as the default case, which will be executed if none of the other patterns match.

The case command is particularly useful when you need to perform different actions based on the file extension or other input values.

Implement case Statement for File Extensions

In this step, you will learn how to use the case statement to handle different file extensions in a shell script.

Let's say you have a directory with various files, and you want to perform different actions based on the file extension. You can use the case statement to achieve this.

First, create a new directory and navigate to it:

mkdir ~/project/files
cd ~/project/files

Now, let's create some sample files with different extensions:

touch file1.txt file2.pdf file3.jpg file4.png file5.gif file6.doc

Next, create a new shell script called file_operations.sh in the ~/project/files directory:

nano file_operations.sh

Add the following code to the script:

#!/bin/bash

for file in *; do
  case "$file" in
    *.txt)
      echo "Text file: $file"
      ;;
    *.pdf)
      echo "PDF file: $file"
      ;;
    *.jpg | *.png | *.gif)
      echo "Image file: $file"
      ;;
    *.doc)
      echo "Document file: $file"
      ;;
    *)
      echo "Unsupported file type: $file"
      ;;
  esac
done

Save and exit the file.

Make the script executable:

chmod +x file_operations.sh

Now, run the script:

./file_operations.sh

Example output:

Text file: file1.txt
PDF file: file2.pdf
Image file: file3.jpg
Image file: file4.png
Image file: file5.gif
Document file: file6.doc

In the script, the case statement checks the file extension and performs the corresponding action. The *.txt, *.pdf, *.jpg | *.png | *.gif, and *.doc patterns match the file extensions, and the appropriate message is printed for each file type. The * pattern is the default case, which handles any unsupported file types.

This example demonstrates how you can use the case statement to automate file management tasks based on file extensions.

Automate Backup Operations with case Command

In this step, you will learn how to use the case command to automate backup operations for different file types.

Let's say you have a directory with various files, and you want to create backups for each file type. You can use the case statement to achieve this.

First, create a new directory and navigate to it:

mkdir ~/project/backup
cd ~/project/backup

Now, let's create some sample files with different extensions:

touch file1.txt file2.pdf file3.jpg file4.png file5.gif file6.doc

Next, create a new shell script called backup.sh in the ~/project/backup directory:

nano backup.sh

Add the following code to the script:

#!/bin/bash

for file in *; do
  case "$file" in
    *.txt)
      echo "Backing up text file: $file"
      cp "$file" "${file%.txt}.txt.bak"
      ;;
    *.pdf)
      echo "Backing up PDF file: $file"
      cp "$file" "${file%.pdf}.pdf.bak"
      ;;
    *.jpg | *.png | *.gif)
      echo "Backing up image file: $file"
      cp "$file" "${file%.*}.bak"
      ;;
    *.doc)
      echo "Backing up document file: $file"
      cp "$file" "${file%.doc}.doc.bak"
      ;;
    *)
      echo "Skipping unsupported file type: $file"
      ;;
  esac
done

Save and exit the file.

Make the script executable:

chmod +x backup.sh

Now, run the script:

./backup.sh

Example output:

Backing up text file: file1.txt
Backing up PDF file: file2.pdf
Backing up image file: file3.jpg
Backing up image file: file4.png
Backing up image file: file5.gif
Backing up document file: file6.doc

In the script, the case statement checks the file extension and performs the corresponding backup action. For each file type, the script creates a backup file with the same name but a .bak extension. The *.jpg | *.png | *.gif pattern matches all image files, and the backup file is created with the same base name but without the extension.

This example demonstrates how you can use the case statement to automate backup operations based on file extensions.

Summary

In this lab, you first learned the syntax and usage of the case command in Linux. The case command is a powerful tool for making decisions based on the value of a variable or expression. You explored the basic structure of the case command and saw an example demonstrating how to use it to check file extensions and perform different actions accordingly. In the second step, you implemented the case statement to handle various file extensions in a shell script, automating file operations based on the file type.

Linux Commands Cheat Sheet

Other Linux Tutorials you may like