File System Operations in Shell

LinuxLinuxBeginner
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 useful for checking the properties of files and directories in the file system. You will familiarize yourself with common file test commands and their usage.


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/BasicSyntaxandStructureGroup(["`Basic Syntax and Structure`"]) shell(("`Shell`")) -.-> shell/VariableHandlingGroup(["`Variable Handling`"]) shell(("`Shell`")) -.-> shell/AdvancedScriptingConceptsGroup(["`Advanced Scripting Concepts`"]) shell(("`Shell`")) -.-> shell/SystemInteractionandConfigurationGroup(["`System Interaction and Configuration`"]) shell/ControlFlowGroup -.-> shell/if_else("`If-Else Statements`") linux/BasicSystemCommandsGroup -.-> linux/echo("`Text Display`") linux/BasicSystemCommandsGroup -.-> linux/read("`Input Reading`") linux/FileandDirectoryManagementGroup -.-> linux/cd("`Directory Changing`") linux/FileandDirectoryManagementGroup -.-> linux/mkdir("`Directory Creating`") linux/BasicFileOperationsGroup -.-> linux/touch("`File Creating/Updating`") linux/BasicFileOperationsGroup -.-> linux/chmod("`Permission Modifying`") shell/BasicSyntaxandStructureGroup -.-> shell/shebang("`Shebang`") shell/BasicSyntaxandStructureGroup -.-> shell/comments("`Comments`") shell/BasicSyntaxandStructureGroup -.-> shell/quoting("`Quoting Mechanisms`") shell/VariableHandlingGroup -.-> shell/variables_decl("`Variable Declaration`") shell/VariableHandlingGroup -.-> shell/variables_usage("`Variable Usage`") shell/ControlFlowGroup -.-> shell/cond_expr("`Conditional Expressions`") shell/AdvancedScriptingConceptsGroup -.-> shell/read_input("`Reading Input`") shell/SystemInteractionandConfigurationGroup -.-> shell/globbing_expansion("`Globbing and Pathname Expansion`") subgraph Lab Skills shell/if_else -.-> lab-153905{{"`File System Operations in Shell`"}} linux/echo -.-> lab-153905{{"`File System Operations in Shell`"}} linux/read -.-> lab-153905{{"`File System Operations in Shell`"}} linux/cd -.-> lab-153905{{"`File System Operations in Shell`"}} linux/mkdir -.-> lab-153905{{"`File System Operations in Shell`"}} linux/touch -.-> lab-153905{{"`File System Operations in Shell`"}} linux/chmod -.-> lab-153905{{"`File System Operations in Shell`"}} shell/shebang -.-> lab-153905{{"`File System Operations in Shell`"}} shell/comments -.-> lab-153905{{"`File System Operations in Shell`"}} shell/quoting -.-> lab-153905{{"`File System Operations in Shell`"}} shell/variables_decl -.-> lab-153905{{"`File System Operations in Shell`"}} shell/variables_usage -.-> lab-153905{{"`File System Operations in Shell`"}} shell/cond_expr -.-> lab-153905{{"`File System Operations in Shell`"}} shell/read_input -.-> lab-153905{{"`File System Operations in Shell`"}} shell/globbing_expansion -.-> lab-153905{{"`File System Operations in Shell`"}} end

Test if a file exists

To test if a file exists, you can use the -e command. This command checks if a file exists as a regular file or a directory.

If the file exists, it will return true; otherwise, it will return false.

#!/bin/bash
filename="sample.md"
if [ -e "$filename" ]; then
  echo "$filename exists as a file"
fi

Cearate a file called ~/project/file.sh.

cd ~/project
chmod +x file.sh
./file.sh

Then, create a file called ~/project/sample.md.

cd ~/project
touch sample.md

Re-run the script.

./file.sh
sample.md exists as a file

Test if a directory exists

To test if a directory exists, you can use the -d command. This command checks if a directory exists. If the directory exists, it will return true; otherwise, it will return false.

#!/bin/bash
directory_name="test_directory"
if [ -d "$directory_name" ]; then
  echo "$directory_name exists as a directory"
fi

Cearate a file called ~/project/directory.sh.

cd ~/project
chmod +x directory.sh
./directory.sh

Then, create a directory called ~/project/test_directory.

cd ~/project
mkdir test_directory

Re-run the script.

./directory.sh
test_directory exists as a directory

Test if a file is readable

To test if a file has read permission for the user running the script, you can use the -r command. This command checks if a file is readable. If the file is readable, it will return true; otherwise, it will return false. If the file doesn't exist, you can create it using the touch command.

#!/bin/bash
filename="sample.md"
if [ ! -f "$filename" ]; then
  touch "$filename" ## Create the file if it doesn't exist
fi
if [ -r "$filename" ]; then
  echo "You are allowed to read $filename"
else
  echo "You are not allowed to read $filename"
fi

Cearate a file called ~/project/readable.sh.

cd ~/project
chmod +x readable.sh
./readable.sh
You are allowed to read sample.md

Then, change the file permissions to remove the read permission.

cd ~/project
chmod -r sample.md

Re-run the script.

./readable.sh
You are not allowed to read sample.md

Summary

In this lab, you learned how to perform file tests in the shell. You learned how to test if a file exists, if a directory exists, and if a file is readable. File tests are essential for checking the properties of files and directories in the file system.

Other Linux Tutorials you may like