0
0
Bash-scriptingHow-ToBeginner · 2 min read

Bash Script to Check if File Exists and Is Readable

Use if [ -r filename ] in Bash to check if a file exists and is readable; it returns true only if the file exists and you have read permission.
📋

Examples

Inputfile.txt exists and is readable
OutputFile 'file.txt' exists and is readable.
Inputfile.txt does not exist
OutputFile 'file.txt' does not exist or is not readable.
Inputfile.txt exists but is not readable
OutputFile 'file.txt' does not exist or is not readable.
🧠

How to Think About It

To check if a file exists and is readable, think about two conditions combined: the file must be present in the system, and you must have permission to read it. In Bash, the -r test operator checks both at once, so you only need to test that condition to confirm both existence and readability.
📐

Algorithm

1
Get the filename as input.
2
Check if the file exists and is readable using the <code>-r</code> test.
3
If true, print that the file exists and is readable.
4
Otherwise, print that the file does not exist or is not readable.
💻

Code

bash
#!/bin/bash
filename="$1"

if [ -r "$filename" ]; then
  echo "File '$filename' exists and is readable."
else
  echo "File '$filename' does not exist or is not readable."
fi
Output
File 'file.txt' exists and is readable.
🔍

Dry Run

Let's trace checking 'file.txt' that exists and is readable through the code

1

Set filename variable

filename='file.txt'

2

Check if file is readable

[ -r 'file.txt' ] returns true

3

Print success message

echo "File 'file.txt' exists and is readable."

StepConditionResult
Check -r 'file.txt'File exists and readabletrue
Print messageN/AFile 'file.txt' exists and is readable.
💡

Why This Works

Step 1: Using -r test operator

The -r operator checks if the file exists and if the current user has read permission on it.

Step 2: Conditional branching

The if statement runs the test and executes the first block if true, otherwise the else block.

Step 3: Output message

The script prints a clear message telling if the file is accessible or not.

🔄

Alternative Approaches

Check existence and readability separately
bash
#!/bin/bash
filename="$1"

if [ -e "$filename" ] && [ -r "$filename" ]; then
  echo "File '$filename' exists and is readable."
else
  echo "File '$filename' does not exist or is not readable."
fi
This method explicitly checks existence with <code>-e</code> and readability with <code>-r</code>, which is more verbose but clearer in intent.
Using test command instead of [ ]
bash
#!/bin/bash
filename="$1"

test -r "$filename" && echo "File '$filename' exists and is readable." || echo "File '$filename' does not exist or is not readable."
This uses the <code>test</code> command and short-circuit operators for a concise one-liner.

Complexity: O(1) time, O(1) space

Time Complexity

Checking file existence and permissions is a constant-time operation handled by the OS, so it runs in O(1) time.

Space Complexity

The script uses a fixed amount of memory for variables and commands, so space complexity is O(1).

Which Approach is Fastest?

All approaches use simple OS checks and run in constant time; the difference is mainly readability and style.

ApproachTimeSpaceBest For
Single -r testO(1)O(1)Simple and concise checks
Separate -e and -r testsO(1)O(1)Clearer intent, more verbose
test command with && ||O(1)O(1)Compact one-liner scripts
💡
Always quote your filename variable like "$filename" to handle spaces or special characters safely.
⚠️
Beginners often forget to quote the filename, causing errors if the filename contains spaces.