File System Operations in Shell

ShellShellBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

In this lab, you will learn how to perform various file tests in the shell. File tests are essential tools for checking the properties of files and directories in the file system. By the end of this lab, you will be familiar with common file test commands and their usage, which are fundamental skills for working with files in Linux environments.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL shell(("`Shell`")) -.-> shell/ControlFlowGroup(["`Control Flow`"]) linux(("`Linux`")) -.-> linux/BasicSystemCommandsGroup(["`Basic System Commands`"]) linux(("`Linux`")) -.-> linux/FileandDirectoryManagementGroup(["`File and Directory Management`"]) linux(("`Linux`")) -.-> linux/BasicFileOperationsGroup(["`Basic File Operations`"]) shell(("`Shell`")) -.-> shell/VariableHandlingGroup(["`Variable Handling`"]) shell/ControlFlowGroup -.-> shell/if_else("`If-Else Statements`") linux/BasicSystemCommandsGroup -.-> linux/echo("`Text Display`") linux/FileandDirectoryManagementGroup -.-> linux/mkdir("`Directory Creating`") linux/BasicFileOperationsGroup -.-> linux/touch("`File Creating/Updating`") linux/BasicFileOperationsGroup -.-> linux/chmod("`Permission Modifying`") shell/VariableHandlingGroup -.-> shell/variables_decl("`Variable Declaration`") shell/VariableHandlingGroup -.-> shell/variables_usage("`Variable Usage`") shell/ControlFlowGroup -.-> shell/cond_expr("`Conditional Expressions`") subgraph Lab Skills shell/if_else -.-> lab-385393{{"`File System Operations in Shell`"}} linux/echo -.-> lab-385393{{"`File System Operations in Shell`"}} linux/mkdir -.-> lab-385393{{"`File System Operations in Shell`"}} linux/touch -.-> lab-385393{{"`File System Operations in Shell`"}} linux/chmod -.-> lab-385393{{"`File System Operations in Shell`"}} shell/variables_decl -.-> lab-385393{{"`File System Operations in Shell`"}} shell/variables_usage -.-> lab-385393{{"`File System Operations in Shell`"}} shell/cond_expr -.-> lab-385393{{"`File System Operations in Shell`"}} end

Creating a Test File

Before we begin with file operations, it's important to understand our working environment. In Linux, you're always working within a specific directory, and it's crucial to know where you are in the file system.

  1. Open a terminal in the WebIDE. This is where you'll type your commands.

  2. Create a new file named test_file.txt:

    touch test_file.txt

    The touch command is used to create an empty file. If the file already exists, it updates the file's timestamp without changing its content.

  3. Add some content to the file:

    echo "This is a test file for our lab." > test_file.txt

    This command uses echo to output the text, and > to redirect that output into the file. Be careful with >, as it will overwrite any existing content in the file.

  4. Verify the file contents:

    cat test_file.txt

    cat is short for "concatenate", but it's often used to display the contents of a file. You should see the message "This is a test file for our lab."

If you make a mistake or want to start over, you can always remove the file with rm test_file.txt and start again from step 1.

Testing File Existence

Now that we have created a file, let's learn how to check if a file exists. This is a common task in shell scripts, especially when you need to perform operations on files.

  1. Create a new script file named file_exists.sh:

    touch file_exists.sh
  2. Add the following content to the file:

    #!/bin/bash
    
    filename="test_file.txt"
    if [ -e "$filename" ]; then
      echo "$filename exists"
    else
      echo "$filename does not exist"
    fi

    Let's break this down:

    • #!/bin/bash is called a shebang. It tells the system this is a bash script.
    • We set a variable filename to "test_file.txt".
    • The if statement checks if the file exists. -e is a test that returns true if the file exists.
    • We use echo to print a message based on whether the file exists or not.
  3. Save the file and exit the editor.

  4. Make the script executable:

    chmod +x file_exists.sh

    chmod changes the permissions of a file. +x adds executable permissions.

  5. Run the script:

    ./file_exists.sh

    You should see the output: "test_file.txt exists"

  6. Now, let's test with a non-existent file. Modify the script to check for a file named "non_existent.txt":

    Change the filename variable to "non_existent.txt".

  7. Run the script again:

    ./file_exists.sh

    You should see the output: "non_existent.txt does not exist"

This script demonstrates how to check for file existence, which is crucial when your script needs to work with files that may or may not be present.

Testing Directory Existence

Similar to testing file existence, we can also check if a directory exists. This is useful when your script needs to work with directories that may or may not be present.

  1. Create a new script file named dir_exists.sh:

    touch dir_exists.sh
  2. Add the following content to the file:

    #!/bin/bash
    
    dirname="test_directory"
    if [ -d "$dirname" ]; then
      echo "$dirname exists"
    else
      echo "$dirname does not exist"
    fi

    This script is very similar to our file existence script, but it uses -d instead of -e. The -d test checks specifically for directory existence.

  3. Save the file and exit the editor.

  4. Make the script executable:

    chmod +x dir_exists.sh
  5. Run the script:

    ./dir_exists.sh

    You should see the output: "test_directory does not exist"

  6. Now, let's create the directory and run the script again:

    mkdir test_directory
    ./dir_exists.sh

    You should now see the output: "test_directory exists"

    mkdir is the command to create a new directory.

This script demonstrates how to check for directory existence. This can be particularly useful in scripts that need to create, modify, or delete directories.

Testing File Permissions

In Linux, every file and directory has associated permissions that determine who can read, write, or execute them. In this step, we'll learn how to check file permissions, specifically if a file is readable.

  1. Create a new script file named file_readable.sh:

    touch file_readable.sh
  2. Add the following content to the file:

    #!/bin/bash
    
    filename="test_file.txt"
    if [ -r "$filename" ]; then
      echo "You have read permission for $filename"
    else
      echo "You do not have read permission for $filename"
    fi

    This script uses the -r test, which checks if the file is readable by the current user.

  3. Save the file and exit the editor.

  4. Make the script executable:

    chmod +x file_readable.sh
  5. Run the script:

    ./file_readable.sh

    You should see the output: "You have read permission for test_file.txt"

  6. Now, let's remove the read permission and run the script again:

    chmod -r test_file.txt
    ./file_readable.sh

    You should now see the output: "You do not have read permission for test_file.txt"

    chmod -r removes read permissions from the file.

  7. Restore the read permission:

    chmod +r test_file.txt

    It's important to restore the permissions so we don't accidentally leave our file unreadable.

This script demonstrates how to check file permissions. Understanding and managing file permissions is crucial for system security and proper functioning of scripts.

Summary

In this lab, you learned how to perform essential file system operations in the shell. You created scripts to test for file and directory existence, as well as file permissions. These skills are fundamental for working with files and directories in Linux environments and form the basis for more complex shell scripting tasks.

You practiced:

  1. Understanding your working environment
  2. Creating and manipulating files
  3. Writing and executing shell scripts
  4. Testing for file and directory existence
  5. Checking file permissions

These skills will be valuable as you continue to work with Linux systems and develop more advanced shell scripts. Remember to always check file and directory existence before performing operations on them to avoid errors in your scripts. Also, be mindful of file permissions when working with sensitive data or system files.

As you progress, you might want to explore more advanced file tests, learn about other shell scripting constructs like loops and functions, and practice combining these concepts to create more complex and powerful scripts.

Other Shell Tutorials you may like