Bird
Raised Fist0
Bash Scriptingscripting~5 mins

File test operators (-f, -d, -e, -r, -w, -x) in Bash Scripting

Choose your learning style10 modes available
Introduction

File test operators help you check different properties of files or directories in scripts. This lets your script make decisions based on file types or permissions.

Check if a file exists before reading it.
Verify if a directory exists before saving files there.
Confirm if a file is writable before trying to write data.
Ensure a script file is executable before running it.
Test if a file is a regular file or a directory.
Syntax
Bash Scripting
[ -f filename ]  # True if filename is a regular file
[ -d filename ]  # True if filename is a directory
[ -e filename ]  # True if filename exists
[ -r filename ]  # True if filename is readable
[ -w filename ]  # True if filename is writable
[ -x filename ]  # True if filename is executable

Use these inside square brackets with spaces around them.

They return true or false and are often used in if statements.

Examples
This checks if 'myfile.txt' is a regular file and prints a message if true.
Bash Scripting
if [ -f myfile.txt ]; then
  echo "myfile.txt is a regular file"
fi
This checks if 'myfolder' exists and is a directory.
Bash Scripting
if [ -d myfolder ]; then
  echo "myfolder is a directory"
fi
This checks if 'myfile.txt' has read permission.
Bash Scripting
if [ -r myfile.txt ]; then
  echo "You can read myfile.txt"
fi
This checks if 'script.sh' can be run as a program.
Bash Scripting
if [ -x script.sh ]; then
  echo "script.sh is executable"
fi
Sample Program

This script checks if 'example.txt' exists. If yes, it tells what type it is and what permissions it has.

Bash Scripting
#!/bin/bash
filename="example.txt"

if [ -e "$filename" ]; then
  echo "$filename exists."
  if [ -f "$filename" ]; then
    echo "$filename is a regular file."
  elif [ -d "$filename" ]; then
    echo "$filename is a directory."
  fi
  if [ -r "$filename" ]; then
    echo "$filename is readable."
  else
    echo "$filename is not readable."
  fi
  if [ -w "$filename" ]; then
    echo "$filename is writable."
  else
    echo "$filename is not writable."
  fi
  if [ -x "$filename" ]; then
    echo "$filename is executable."
  else
    echo "$filename is not executable."
  fi
else
  echo "$filename does not exist."
fi
OutputSuccess
Important Notes

Always quote filenames to handle spaces or special characters.

These tests are case-sensitive and depend on the file system.

Use these operators to avoid errors when accessing files in scripts.

Summary

File test operators check file existence, type, and permissions.

They are used inside if statements to control script flow.

Common operators: -f (file), -d (directory), -e (exists), -r (readable), -w (writable), -x (executable).

Practice

(1/5)
1. Which file test operator checks if a path is a regular file in a bash script?
easy
A. -e
B. -f
C. -d
D. -x

Solution

  1. Step 1: Understand the meaning of each operator

    -f checks if the path is a regular file, -d checks for directory, -e checks if the path exists, and -x checks if the file is executable.
  2. Step 2: Match the operator to the question

    The question asks for the operator that checks if a path is a regular file, which is -f.
  3. Final Answer:

    -f -> Option B
  4. Quick Check:

    Regular file check = -f [OK]
Hint: Remember: -f means file, -d means directory [OK]
Common Mistakes:
  • Confusing -f with -d
  • Using -e which only checks existence
  • Mixing -x (executable) with file type
2. Which of the following is the correct syntax to check if a directory exists in bash?
easy
A. if [ -f /path/to/dir ]; then echo "Exists"; fi
B. if [ -x /path/to/dir ]; then echo "Exists"; fi
C. if [ -e /path/to/dir ]; then echo "Exists"; fi
D. if [ -d /path/to/dir ]; then echo "Exists"; fi

Solution

  1. Step 1: Identify the operator for directory check

    -d is used to check if a path is a directory.
  2. Step 2: Verify the syntax correctness

    The syntax if [ -d /path/to/dir ]; then ... fi is correct for checking directory existence.
  3. Final Answer:

    if [ -d /path/to/dir ]; then echo "Exists"; fi -> Option D
  4. Quick Check:

    Directory check uses -d [OK]
Hint: Use -d to check directories, not -f or -x [OK]
Common Mistakes:
  • Using -f to check directories
  • Confusing -e (exists) with directory check
  • Missing spaces inside [ ] causing syntax errors
3. What will be the output of this script if myfile.txt exists and is readable but not writable?
if [ -r myfile.txt ]; then echo "Readable"; else echo "Not readable"; fi
if [ -w myfile.txt ]; then echo "Writable"; else echo "Not writable"; fi
medium
A. Readable\nWritable
B. Not readable\nWritable
C. Readable\nNot writable
D. Not readable\nNot writable

Solution

  1. Step 1: Check readability condition

    The file exists and is readable, so [ -r myfile.txt ] is true, printing "Readable".
  2. Step 2: Check writability condition

    The file is not writable, so [ -w myfile.txt ] is false, printing "Not writable".
  3. Final Answer:

    Readable\nNot writable -> Option C
  4. Quick Check:

    Readable true, writable false = A [OK]
Hint: Check each permission separately with -r and -w [OK]
Common Mistakes:
  • Assuming writable if readable
  • Mixing up -r and -w operators
  • Ignoring else branch output
4. Identify the error in this bash snippet that checks if a file exists and is executable:
if [ -e myscript.sh -a -x ]; then echo "Executable file"; fi
medium
A. Missing filename after -x operator
B. Wrong operator -e should be -f
C. Missing spaces around -a operator
D. Should use double brackets [[ ]] instead of single [ ]

Solution

  1. Step 1: Analyze the -x operator usage

    The -x operator requires a filename argument, but none is provided after it.
  2. Step 2: Check other parts of the condition

    -e myscript.sh is valid to check existence; spaces around -a are correct; using single brackets is valid.
  3. Final Answer:

    Missing filename after -x operator -> Option A
  4. Quick Check:

    -x needs a file name [OK]
Hint: Always provide filename after each test operator [OK]
Common Mistakes:
  • Forgetting filename after -x
  • Confusing -e and -f operators
  • Missing spaces around operators causing syntax errors
5. You want to write a bash script that prints "Ready" only if config.cfg exists, is a regular file, and is readable and writable. Which condition correctly checks all these?
hard
A. if [ -f config.cfg ] && [ -r config.cfg ] && [ -w config.cfg ]; then echo "Ready"; fi
B. if [ -e config.cfg -a -r config.cfg -a -w config.cfg ]; then echo "Ready"; fi
C. if [ -d config.cfg ] && [ -r config.cfg ] && [ -w config.cfg ]; then echo "Ready"; fi
D. if [ -e config.cfg ] && [ -r config.cfg ] && [ -w config.cfg ]; then echo "Ready"; fi

Solution

  1. Step 1: Identify the operator for regular file check

    -f checks if the path is a regular file, which is required.
  2. Step 2: Combine all conditions correctly

    Using && between separate [ ] tests is correct and clear. The script checks existence as a file, readability, and writability.
  3. Step 3: Eliminate incorrect options

    if [ -e config.cfg ] && [ -r config.cfg ] && [ -w config.cfg ]; then echo "Ready"; fi uses -e which allows directories; if [ -e config.cfg -a -r config.cfg -a -w config.cfg ]; then echo "Ready"; fi uses -e which allows directories; if [ -d config.cfg ] && [ -r config.cfg ] && [ -w config.cfg ]; then echo "Ready"; fi checks for directory which is wrong.
  4. Final Answer:

    if [ -f config.cfg ] && [ -r config.cfg ] && [ -w config.cfg ]; then echo "Ready"; fi -> Option A
  5. Quick Check:

    Regular file with read/write uses -f, -r, -w combined [OK]
Hint: Use -f for file, combine tests with && for clarity [OK]
Common Mistakes:
  • Using -e instead of -f to check file type
  • Using -a inside single [ ] causing confusion
  • Checking directory instead of file
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard