Bash Script to Check if File is Readable – Examples

Bash Script to Check if File is Readable

Bash Script to Check if File is Readable – To check if the specified file has read permission in bash scripting, we shall use  [ -r FILE ]  expression with bash if statement.

  • Example 1 – Simple if statement to check if file is readable
  • Example 2 – Check if argument passed to function is a file and is readable

For the following examples, we shall use two files as shown below :

sample.txt has no read permissions.
dummy.txt has read permissions for all users.

 

Example 1 – Simple if statement to check if file is readable

A simple script with only if statements to check if the file specified is readable.

When the above script file is run in Terminal

 

Example 2 – Check if argument passed to function is a file and is readable

In this Example we shall write a function whose first argument is a FILE. And in the function we shall check if the passed argument (FILE) exists and is readable.

When the above script file is run in Terminal,

 

Conclusion :

In this Bash Tutorial – Bash Script to Check if File is Readable, we have learnt how to check if the specified file has read permissions.