Check File Existence

LinuxLinuxBeginner
Practice Now

Introduction

In Bash, you can check the existence of a file using various options. This challenge focuses on using the '-f' option to test the file existence.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL shell(("`Shell`")) -.-> shell/ControlFlowGroup(["`Control Flow`"]) linux(("`Linux`")) -.-> linux/BasicSystemCommandsGroup(["`Basic System Commands`"]) linux(("`Linux`")) -.-> linux/BasicFileOperationsGroup(["`Basic File Operations`"]) shell(("`Shell`")) -.-> shell/BasicSyntaxandStructureGroup(["`Basic Syntax and Structure`"]) shell(("`Shell`")) -.-> shell/VariableHandlingGroup(["`Variable Handling`"]) shell(("`Shell`")) -.-> shell/SystemInteractionandConfigurationGroup(["`System Interaction and Configuration`"]) shell/ControlFlowGroup -.-> shell/if_else("`If-Else Statements`") linux/BasicSystemCommandsGroup -.-> linux/echo("`Text Display`") linux/BasicFileOperationsGroup -.-> linux/ls("`Content Listing`") 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/SystemInteractionandConfigurationGroup -.-> shell/globbing_expansion("`Globbing and Pathname Expansion`") subgraph Lab Skills shell/if_else -.-> lab-387376{{"`Check File Existence`"}} linux/echo -.-> lab-387376{{"`Check File Existence`"}} linux/ls -.-> lab-387376{{"`Check File Existence`"}} shell/shebang -.-> lab-387376{{"`Check File Existence`"}} shell/comments -.-> lab-387376{{"`Check File Existence`"}} shell/quoting -.-> lab-387376{{"`Check File Existence`"}} shell/variables_decl -.-> lab-387376{{"`Check File Existence`"}} shell/variables_usage -.-> lab-387376{{"`Check File Existence`"}} shell/cond_expr -.-> lab-387376{{"`Check File Existence`"}} shell/globbing_expansion -.-> lab-387376{{"`Check File Existence`"}} end

Check File Existence

Create a Bash script named file_exist.sh that takes a filename as an argument and checks if the file exists in the current location.

If the file exists, the script should print "File exists" and if the file does not exist, the script should print "File does not exist".

Requirements

  • The script should be named ~/project/file_exist.sh.
  • The script should take a filename as an argument.
  • The script should use the -f option to check the file existence.
  • If the file exists, the script should print "File exists".
  • If the file does not exist, the script should print "File does not exist".

Example

After you finish this challenge, you can verify the script works as expected.

Check if the file filename.txt exists:

touch filename.txt
bash file_exist.sh filename.txt

Output:

File exists

Check if a non-existent file non_existent_file.txt exists:

bash file_exist.sh non_existent_file.txt

Output:

File does not exist

Summary

In this challenge, we learned how to check the existence of a file in Bash using the '-f' option. We created a Bash script that takes a filename as an argument and checks if the file exists in the current location. If the file exists, the script prints "File exists" and if the file does not exist, the script prints "File does not exist".

Other Linux Tutorials you may like