Bash Script – Check if file has read permissions
To check if a file is readable in bash, in other words if the file has read permissions for the user running the script, use the [ -r FILE ] expression. The -r file test returns true when the specified path exists and is readable.
This check is useful before reading a configuration file, processing an input file, opening a log file, or validating a file path passed as a command-line argument. In practical scripts, you may also combine -r with tests such as -f and -w to confirm that the path is a regular file and has the required permissions.
- Syntax to check if a file is readable in Bash
- Examples using readable and non-readable files
- Check if a path is both a regular file and readable
- Check if a Bash file is readable and writable
- FAQs on Bash readable file checks
Syntax to check if a file is readable in Bash
The syntax of the expression to check if the file is readable or not is given below.
[ -r path/to/file ]
replace path/to/file with the actual path to file, whose read permissions you need to check.
The expression returns a value of true if the file is present and has read permission, or a false if the file does not have read permissions.
When you use a variable in the readable-file test, quote the variable. This prevents errors when the path contains spaces or when the variable is empty.
if [ -r "$file" ]; then
echo "$file is readable"
else
echo "$file is not readable"
fi
In Bash scripts, you may also write the condition using the Bash-specific double bracket form.
if [[ -r "$file" ]]; then
echo "$file is readable"
fi
Examples using readable and non-readable files in Bash
For the sake of examples, we will use two files shown below.
$ ls -lr
total 125016
--w------- 1 root root 12 Oct 5 09:35 sample.txt
-rwxr--r-- 1 root root 20 Oct 5 15:33 dummy.txt
If you observe the permissions for these files,
- sample.txt has no read permissions.
- dummy.txt has read permissions for all users.
1. Check if file is readable for dummy.txt
In the following example, we shall use -r expression, and check if the file dummy.txt is readable. We use the -r option as a condition in bash if else statement.
example.sh
#!/bin/bash
file="dummy.txt"
if [ -r "$file" ]
then
echo "$file is readable."
else
echo "$file is not readable."
fi
Bash Version: GNU bash, version 5.2.15(1)-release (aarch64-apple-darwin22.1.0)
Run this script file in a Terminal, and you shall see the following output, provided you have the file dummy.txt mentioned in your system.
Output
sh-3.2# bash example.sh
dummy.txt is readable.
The condition is true because dummy.txt is readable by the user running the script.
2. Check if file is readable for sample.txt
In the following example, we shall use -r expression, and check if the file sample.txt is readable.
example.sh
#!/bin/bash
file="sample.txt"
if [ -r "$file" ]
then
echo "$file is readable."
else
echo "$file is not readable."
fi
Bash Version: GNU bash, version 5.2.15(1)-release (aarch64-apple-darwin22.1.0)
Output
sh-3.2# bash example.sh
sample.txt is not readable.
The condition is false because sample.txt does not have read permission for the user running the script.
Check if a path is both a regular file and readable in Bash
The -r test answers whether a path is readable. If your script specifically expects a normal file, add -f as well. This prevents a readable directory or another special path from being accepted as an input file.
#!/bin/bash
file="$1"
if [ -z "$file" ]; then
echo "Usage: $0 FILE_PATH"
exit 1
fi
if [ -f "$file" ] && [ -r "$file" ]; then
echo "$file is a readable regular file."
else
echo "$file is not a readable regular file."
fi
Example output for a readable file and a missing path:
$ bash example.sh dummy.txt
dummy.txt is a readable regular file.
$ bash example.sh missing.txt
missing.txt is not a readable regular file.
Use this form when the next command in the script will read file contents, for example with cat, grep, awk, sed, or a program that expects a file path.
Check if a Bash file is readable and writable
Some scripts need to both read from and write to the same file. In that case, combine -r with -w. The -w test checks whether the file is writable by the user running the script.
#!/bin/bash
file="settings.conf"
if [ -f "$file" ] && [ -r "$file" ] && [ -w "$file" ]; then
echo "$file is a regular file with read and write permissions."
else
echo "$file is missing, not a regular file, or does not have read/write permissions."
fi
This pattern is helpful before updating configuration files, temporary data files, or application-generated text files. If the script only needs to read the file, checking -r is enough. If the script also modifies the file, include -w.
Bash file permission tests related to readable files
Bash provides several file test operators. The following tests are commonly used with readable-file checks.
| Bash test | What it checks | When to use it |
|---|---|---|
[ -r "$file" ] | The path exists and is readable. | Before reading a file or passing it to a command that needs read access. |
[ -f "$file" ] | The path exists and is a regular file. | When directories and special files should not be accepted. |
[ -e "$file" ] | The path exists. | When any existing path is acceptable. |
[ -w "$file" ] | The path exists and is writable. | Before modifying or appending to a file. |
[ -s "$file" ] | The file exists and has a size greater than zero. | When the script needs a non-empty input file. |
For a script that reads a normal input file, [ -f "$file" ] && [ -r "$file" ] is usually more precise than [ -r "$file" ] alone.
Common mistakes while checking file readability in Bash
- Not quoting the filename variable: Use
[ -r "$file" ], not[ -r $file ]. Quoting handles spaces and empty values safely. - Checking readability but not file type: If your script expects a regular file, also check
-f. - Assuming root behaves like a normal user: Running a script with elevated privileges can change the result of permission checks.
- Confusing readable with writable:
-rchecks read permission, while-wchecks write permission. - Using a relative path without knowing the current directory: If the script runs from another location, a relative path may point to a different file than expected.
QA checklist for a Bash readable-file script
- Use
-rwhen the script must confirm read access before reading a file. - Quote file path variables in every test expression.
- Use
-fwith-rif directories should not be accepted. - Add an empty-argument check when the filename is passed as
$1. - Test the script with a readable file, a non-readable file, a directory, and a missing path.
- Add
-wonly when the script needs to modify the file.
FAQs on Bash script to check if a file is readable
How do I check if a file is readable in Bash?
Use [ -r "$file" ] inside an if statement. The condition is true when the path exists and is readable by the user running the script.
Does Bash -r also check whether the file exists?
Yes. The -r test returns true only when the path exists and is readable. If the path does not exist, the condition returns false.
What is the difference between -r and -f in Bash?
-r checks whether a path is readable. -f checks whether a path is a regular file. Use both when your script needs a readable regular file.
How can I check if a file is readable and writable in Bash?
Use [ -r "$file" ] && [ -w "$file" ]. Add [ -f "$file" ] if the path must be a regular file.
Why should the filename be quoted in [ -r “$file” ]?
Quoting the variable prevents syntax errors and incorrect tests when the filename contains spaces, special characters, or an empty value.
Conclusion: using -r to check readable files in Bash
In this Bash Tutorial, we learned how to check if specified file is readable or not. Use [ -r "$file" ] for a readable-file check, combine it with -f when the path must be a regular file, and add -w only when the script must also write to the file.
TutorialKart.com