0
0
Bash Scriptingscripting~15 mins

File existence checks in Bash Scripting - Mini Project: Build & Apply

Choose your learning style9 modes available
File existence checks
📖 Scenario: You are writing a simple script to check if certain files exist on your computer. This is useful when you want to make sure important files are present before running other commands.
🎯 Goal: Create a bash script that checks if a file named data.txt exists in the current folder and prints a message based on the result.
📋 What You'll Learn
Create a variable called filename with the value data.txt
Create a variable called filepath that stores the full path to the file using $(pwd) and filename
Use an if statement with the -e test to check if the file exists at filepath
Print File exists. if the file is found
Print File does not exist. if the file is not found
💡 Why This Matters
🌍 Real World
Checking if files exist is a common task in automation scripts to avoid errors when processing files.
💼 Career
Many IT and DevOps roles require writing scripts that verify file presence before running backups, deployments, or data processing.
Progress0 / 4 steps
1
Create the filename variable
Create a variable called filename and set it to the string data.txt.
Bash Scripting
Need a hint?

Use filename="data.txt" to create the variable.

2
Create the filepath variable
Create a variable called filepath that combines the current directory path from $(pwd) and the filename variable, separated by a slash /.
Bash Scripting
Need a hint?

Use filepath="$(pwd)/$filename" to combine the current directory and filename.

3
Check if the file exists
Write an if statement that uses [ -e "$filepath" ] to check if the file exists at filepath. Inside the if, print File exists.. Use else to print File does not exist. if the file is missing.
Bash Scripting
Need a hint?

Use if [ -e "$filepath" ]; then to check file existence.

4
Print the result
Run the script and ensure it prints File exists. if data.txt is in the current folder, or File does not exist. if it is not.
Bash Scripting
Need a hint?

Make sure the file data.txt is in your current folder or not, then run the script.