How to check if a file is writable in Linux

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, you will learn how to check if a file is writable in Linux using various methods. We will begin by using the test -w command to quickly determine writability and understand its use in shell scripting.

Next, you will verify file permissions in detail using the ls -l command, interpreting the output to understand ownership, group, and other permissions. Finally, you will inspect file access information more comprehensively with the stat command.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL 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/BasicFileOperationsGroup -.-> linux/touch("File Creating/Updating") linux/FileandDirectoryManagementGroup -.-> linux/cd("Directory Changing") subgraph Lab Skills linux/echo -.-> lab-558714{{"How to check if a file is writable in Linux"}} linux/test -.-> lab-558714{{"How to check if a file is writable in Linux"}} linux/ls -.-> lab-558714{{"How to check if a file is writable in Linux"}} linux/touch -.-> lab-558714{{"How to check if a file is writable in Linux"}} linux/cd -.-> lab-558714{{"How to check if a file is writable in Linux"}} end

Test writability with test -w

In this step, we will learn how to check if you have permission to write to a file or directory using the test command with the -w option. Understanding file permissions is crucial in Linux, as it determines who can read, write, and execute files.

The test command is a built-in shell command that checks for conditions and returns an exit status of 0 (true) or 1 (false). The -w option specifically checks if a file or directory is writable by the current user.

Let's create a simple file in your ~/project directory. We'll use the touch command, which creates an empty file if it doesn't exist.

Navigate to your project directory if you are not already there:

cd ~/project

Now, create a file named my_test_file.txt:

touch my_test_file.txt

You can verify the file was created using the ls command:

ls

You should see my_test_file.txt listed in the output.

Now, let's use the test -w command to check if you can write to this file. We'll combine it with an echo command to see the result.

test -w my_test_file.txt && echo "Writable" || echo "Not writable"

Let's break down this command:

  • test -w my_test_file.txt: This checks if my_test_file.txt is writable.
  • && echo "Writable": If the test command returns true (exit status 0), the && operator executes the next command, which prints "Writable".
  • || echo "Not writable": If the test command returns false (exit status 1), the || operator executes the next command, which prints "Not writable".

Since you created the file as the labex user in your home directory, you should have write permissions, and the output should be:

Writable

The test -w command is often used in shell scripts to check permissions before attempting to write to a file, preventing potential errors.

Click Continue to proceed to the next step.

Verify permissions with ls -l

In this step, we will use the ls -l command to view detailed information about files and directories, including their permissions. This command is essential for understanding who can access what on a Linux system.

The ls command lists files and directories. The -l option provides a "long listing" format, which includes permissions, ownership, size, and modification time.

Make sure you are in the ~/project directory:

cd ~/project

Now, run the ls -l command to see the details of the files in this directory. You should see the my_test_file.txt file you created in the previous step.

ls -l

The output will look something like this:

-rw-rw-r-- 1 labex labex    0 Month Day HH:MM my_test_file.txt

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

-rw-rw-r--

This string of ten characters tells you about the file type and its permissions for different users.

  1. The first character (- in this case) indicates the file type.
    • -: Regular file
    • d: Directory
    • l: Symbolic link
    • And others...
  2. The next nine characters are grouped into three sets of three:
    • The first set (rw-): Permissions for the owner of the file.
    • The second set (rw-): Permissions for the group the file belongs to.
    • The third set (r--): Permissions for others (everyone else on the system).

Within each set of three characters:

  • r: Read permission (allows viewing the file's contents or listing directory contents).
  • w: Write permission (allows modifying the file or creating/deleting files within a directory).
  • x: Execute permission (allows running the file as a program or entering a directory).
  • -: Indicates that the permission is not granted.

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

  • The owner (labex) has read (r) and write (w) permissions.
  • The group (labex) has read (r) and write (w) permissions.
  • Others have only read (r) permission.

This output confirms why the test -w command in the previous step returned "Writable" for the labex user โ€“ because the owner (labex) has write permission (w).

Understanding ls -l output is fundamental to managing files and permissions in Linux.

Click Continue to move on.

Inspect file access with stat

In this step, we will use the stat command to get detailed information about a file or file system. While ls -l gives you a quick overview, stat provides a much more comprehensive look at file metadata, including access, modification, and change times.

Make sure you are in the ~/project directory:

cd ~/project

Now, let's use the stat command on the my_test_file.txt file you created earlier:

stat my_test_file.txt

The output will be quite detailed and might look something like this:

  File: my_test_file.txt
  Size: 0               Blocks: 0          IO Block: 4096   regular empty file
Device: ---h/---d       Inode: ---         Links: 1
Access: (0664/-rw-rw-r--)  Uid: ( 5000/   labex)   Gid: ( 5000/   labex)
Access: YYYY-MM-DD HH:MM:SS.SSSSSSSSS +ZZZZ
Modify: YYYY-MM-DD HH:MM:SS.SSSSSSSSS +ZZZZ
Change: YYYY-MM-DD HH:MM:SS.SSSSSSSSS +ZZZZ
 Birth: -

Let's look at some of the key pieces of information provided by stat:

  • 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 both octal (0664) and symbolic (-rw-rw-r--) formats. This matches the permissions we saw with ls -l.
  • Uid: ( 5000/ labex): The User ID (UID) and username of the file's owner.
  • Gid: ( 5000/ labex): The Group ID (GID) and group name of the file's group.
  • 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, ownership, etc.) was changed.
  • Birth: The creation time of the file (may not be available on all file systems).

The stat command provides a deeper dive into the properties of a file than ls -l. It's particularly useful when you need precise timestamps or detailed information about file ownership and permissions in different formats.

You can also use stat with options to get specific information. For example, to see only the access permissions in octal format:

stat -c "%a" my_test_file.txt

This command uses the -c option to specify a custom format. "%a" is the format specifier for access permissions in octal. The output should be:

664

This matches the octal permission 0664 shown in the full stat output (the leading zero is often omitted).

Click Continue to complete this lab.

Summary

In this lab, we learned how to check if a file is writable in Linux using the test -w command. This command is a simple and effective way to determine write permissions and is often used in shell scripting to prevent errors. We practiced creating a file and then using test -w combined with && and || operators to display whether the file is writable.

We also explored how to verify file permissions using the ls -l command, which provides detailed information about file ownership, group, and permissions in a human-readable format. Finally, we learned how to inspect file access details using the stat command, which offers more comprehensive information about a file's status, including access, modify, and change timestamps.