case Command in Linux

Introduction

In this practical guide, you will discover how to leverage the Linux case command within shell scripts to effectively manage diverse file extensions. This tutorial covers the fundamental syntax and application of the case command. It also illustrates how to implement case statements specifically for handling file extensions, and demonstrates the automation of backup procedures using the case command. This lab aims to enhance your proficiency in utilizing various utilities within the Linux environment, crucial skills for any systemadmin.

We begin with a comprehensive explanation of the syntax and usage of the case command, a valuable asset for making decisions based on variable or expression values. You'll grasp how to structure the case statement and observe examples of its real-world usage in system administration tasks.

Subsequently, this guide walks you through implementing case statements for efficient file extension handling. You will craft a shell script that executes specific actions based on the identified file type, such as distinguishing between text documents, PDF files, and image files.

Finally, we will demonstrate automating backup routines with the case command. You'll learn to construct a script that selectively backs up files with particular extensions to designated locations, simplifying data management and organization for a systemadmin.

Understand the Syntax and Usage of the case Command

This section focuses on the syntax and usage of the case command within Linux. The case command is a potent decision-making tool based on the value of a variable or expression within your systemadmin scripts.

The fundamental syntax of the case command is presented below:

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

Here is an example that clarifies the practical 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 this illustration, the case command evaluates the file_ext variable's value and executes the corresponding commands according to the matched pattern. The * pattern serves as the default case, triggered when none of the preceding patterns match. This is a vital concept for systemadmin scripting.

The case command is especially valuable when different actions are required based on file extensions or other input values.

Implement case Statement for File Extensions

In this section, you'll master the use of the case statement to manage different file extensions within a shell script – a key skill for any aspiring systemadmin.

Imagine a directory containing various files, where you need to execute different actions based on the file extension. The case statement provides an elegant solution.

Start by creating a new directory and navigating into it:

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

Now, let's generate a set of sample files with varying extensions:

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

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

nano file_operations.sh

Populate the script with the following code:

#!/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.

Grant the script execute permissions:

chmod +x file_operations.sh

Now, execute 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

Within the script, the case statement assesses the file extension and performs the associated action. The patterns *.txt, *.pdf, *.jpg | *.png | *.gif, and *.doc match the respective file extensions, and the appropriate message is displayed for each file type. The * pattern acts as the default case, handling any unsupported file types. A valuable technique for any systemadmin.

This example illustrates how the case statement can automate file management tasks based on file extensions, a vital task for any systemadmin working with Linux systems.

Automate Backup Operations with case Command

In this section, we'll explore using the case command to automate backup operations for different file types - a crucial task for every systemadmin to understand.

Consider a scenario where you have a directory containing various files and you want to create backups tailored to each file type. The case statement simplifies this process.

First, create a new directory and navigate to it:

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

Now, let's create 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 examines the file extension and executes the corresponding backup action. For each identified file type, the script generates a backup file with the same name but appended with a .bak extension. The pattern *.jpg | *.png | *.gif matches all image files, creating backup files with the same base name but without the original extension. This is a simple, yet vital technique for any systemadmin.

This example demonstrates how you can employ the case statement to automate backup operations based on file extensions, a core function in Linux system administration.

Summary

In this tutorial, you first explored the syntax and practical application of the case command in Linux. The case command serves as a robust tool for making decisions based on variable or expression values. You learned the basic structure of the case command and examined an example showcasing its use in verifying file extensions and performing actions accordingly. In the following section, you utilized the case statement to efficiently handle a variety of file extensions within a shell script, thereby automating file operations according to file type. This is a core skill for a Linux systemadmin.

400+ Linux Commands