How to check if a file is readable in Linux

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, you will learn how to check if a file is readable in Linux using various command-line tools. You will begin by using the fundamental test -r command to programmatically check for read permissions, understanding its use in conditional expressions.

Next, you will explore the ls -l command to visually inspect file permissions, gaining insight into the permission string and its meaning. Finally, you will delve into the stat command to obtain detailed information about a file, including its access permissions and other metadata. By completing these steps, you will gain practical skills in determining file readability in a Linux environment.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("Linux")) -.-> linux/InputandOutputRedirectionGroup(["Input and Output Redirection"]) linux(("Linux")) -.-> linux/BasicSystemCommandsGroup(["Basic System Commands"]) linux(("Linux")) -.-> linux/BasicFileOperationsGroup(["Basic File Operations"]) linux(("Linux")) -.-> linux/FileandDirectoryManagementGroup(["File and Directory Management"]) linux/BasicSystemCommandsGroup -.-> linux/echo("Text Display") linux/BasicSystemCommandsGroup -.-> linux/test("Condition Testing") linux/BasicFileOperationsGroup -.-> linux/ls("Content Listing") linux/FileandDirectoryManagementGroup -.-> linux/pwd("Directory Displaying") linux/InputandOutputRedirectionGroup -.-> linux/redirect("I/O Redirecting") subgraph Lab Skills linux/echo -.-> lab-558713{{"How to check if a file is readable in Linux"}} linux/test -.-> lab-558713{{"How to check if a file is readable in Linux"}} linux/ls -.-> lab-558713{{"How to check if a file is readable in Linux"}} linux/pwd -.-> lab-558713{{"How to check if a file is readable in Linux"}} linux/redirect -.-> lab-558713{{"How to check if a file is readable in Linux"}} end

Test readability with test -r

In this step, you will learn how to check if you have read permission for a file using the test command with the -r option.

The test command is a built-in shell command that evaluates conditional expressions. It's often used in shell scripts to make decisions. The -r option specifically checks if a file exists and is readable by the current user.

Let's create a simple file to test this. We'll use the echo command to put some text into a file named my_file.txt in your ~/project directory.

Type the following command and press Enter:

echo "This is a test file." > ~/project/my_file.txt

This command creates the file ~/project/my_file.txt and writes the string "This is a test file." into it. The > symbol redirects the output of the echo command to the specified file.

Now, let's use test -r to check if you can read this file. The test command itself doesn't produce output if the condition is true. We usually combine it with other commands like echo to see the result.

Type the following command and press Enter:

test -r ~/project/my_file.txt && echo "File is readable."

The && operator means "execute the command on the right only if the command on the left succeeds (returns a zero exit status)". If test -r ~/project/my_file.txt is true (meaning the file is readable), the echo command will execute.

You should see the output:

File is readable.

Now, let's try checking a file that doesn't exist.

Type the following command and press Enter:

test -r ~/project/non_existent_file.txt && echo "This won't be printed."

Since ~/project/non_existent_file.txt does not exist, test -r will return a non-zero exit status (failure), and the echo command will not be executed. You should see no output from this command.

The test command is a fundamental tool for scripting in Linux. Understanding how to use its various options, like -r, is crucial for writing robust scripts that can check for file permissions and existence before attempting operations.

Click Continue to proceed to the next step.

Check permissions with ls -l

In the previous step, you used test -r to check if a file is readable. Now, let's use the ls command with the -l option to see the detailed permissions of a file.

The ls command lists directory contents. The -l option provides a "long listing" format, which includes information about file type, permissions, number of hard links, owner, group, size, modification time, and filename.

Let's look at the permissions for the my_file.txt file you created in the previous step. Make sure you are in the ~/project directory. You can confirm your current directory using the pwd command:

pwd

You should see /home/labex/project.

Now, type the following command and press Enter:

ls -l my_file.txt

You will see output similar to this:

-rw-rw-r-- 1 labex labex XX Month XX XX:XX my_file.txt

Let's break down the first part of the output, which represents the file type and permissions:

-rw-rw-r--

This string of ten characters tells you a lot about the file's permissions.

  1. The first character indicates the file type:

    • - means it's a regular file.
    • d means it's a directory.
    • l means it's a symbolic link.
    • (There are other types, but these are the most common).
  2. The next nine characters are grouped into three sets of three:

    • The first set (rw-) shows the permissions for the owner of the file.
    • The second set (rw-) shows the permissions for the group the file belongs to.
    • The third set (r--) shows the permissions for others (everyone else).

Within each set of three characters:
_ The first character indicates read permission (r). If a hyphen (-) is present, read permission is denied.
_ The second character indicates write permission (w). If a hyphen (-) is present, write permission is denied. * The third character indicates execute permission (x). If a hyphen (-) is present, execute permission is denied.

So, for my_file.txt with permissions -rw-rw-r--:

  • The owner (labex) has read (r) and write (w) permissions, but no execute (-) permission.
  • The group (labex) has read (r) and write (w) permissions, but no execute (-) permission.
  • Others have read (r) permission, but no write (-) or execute (-) permission.

This output confirms that the owner (labex) has read permission, which is why test -r worked in the previous step.

Understanding the output of ls -l is fundamental to managing files and directories in Linux and controlling who can access them.

Click Continue to move on.

Inspect file access with stat

In this step, you will use the stat command to get detailed information about a file, including its access, modification, and change times, as well as its permissions in both symbolic and numeric formats.

While ls -l gives you a good overview of permissions, stat provides a more in-depth look at the file's metadata.

Let's use stat on the my_file.txt file you created earlier in the ~/project directory.

Type the following command and press Enter:

stat ~/project/my_file.txt

You will see output similar to this:

  File: /home/labex/project/my_file.txt
  Size: XX          Blocks: X          IO Block: XXXX   regular file
Device: XXh/XXd Inode: XXXXXXXX    Links: X
Access: (0664/-rw-rw-r--)  Uid: (  XXXX/   labex)   Gid: (  XXXX/   labex)
Access: XXXX-XX-XX XX:XX:XX.XXXXXXXXXX +XXXX
Modify: XXXX-XX-XX XX:XX:XX.XXXXXXXXXX +XXXX
Change: XXXX-XX-XX XX:XX:XX.XXXXXXXXXX +XXXX
 Birth: -

Let's look at some key lines in the output:

  • File: The name of the file.
  • Size: The size of the file in bytes.
  • Access: (0664/-rw-rw-r--): This line shows the permissions in two formats:
    • 0664: This is the numeric (octal) representation of the permissions. Each digit represents the permissions for owner, group, and others, respectively.
      • 6 for owner: rwx (read=4, write=2, execute=0) -> 4+2+0 = 6
      • 6 for group: rwx (read=4, write=2, execute=0) -> 4+2+0 = 6
      • 4 for others: rwx (read=4, write=0, execute=0) -> 4+0+0 = 4
      • The leading 0 is often used to indicate octal notation.
    • -rw-rw-r--: This is the symbolic representation of the permissions, which you learned about with ls -l.
  • Uid and Gid: The User ID and Group ID of the file's owner and group, along with their names.
  • Access, Modify, Change: These lines show the timestamps for the file:
    • Access: The last time the file was accessed (read).
    • Modify: The last time the file's content was modified.
    • Change: The last time the file's metadata (like permissions, owner, group) was changed.

The stat command is very useful when you need detailed information about a file's properties, especially for scripting or troubleshooting permission issues.

You have now learned three different ways to inspect file permissions and access information in Linux: test -r, ls -l, and stat. These are essential tools for working with files on the command line.

Click Continue to complete this lab.

Summary

In this lab, you learned how to check if a file is readable in Linux using the test command with the -r option. You practiced creating a file and then using test -r in conjunction with the && operator to conditionally print a message based on the file's readability. You also observed that test -r returns false for non-existent files, preventing subsequent commands from executing. This step demonstrated a fundamental method for checking file permissions programmatically in shell scripting.