How to check if a file is executable in Linux

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, you will learn how to determine if a file is executable in Linux using various methods. You will begin by using the test command with the -x option to check for executable permissions and understand how to interpret its exit status.

Next, you will explore how to use the ls -l command to view detailed file permissions and identify the executable flag. Finally, you will learn how to use the stat command to inspect file access information, providing another way to confirm executability.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("Linux")) -.-> linux/FileandDirectoryManagementGroup(["File and Directory Management"]) linux(("Linux")) -.-> linux/BasicSystemCommandsGroup(["Basic System Commands"]) linux(("Linux")) -.-> linux/BasicFileOperationsGroup(["Basic File Operations"]) linux/BasicSystemCommandsGroup -.-> linux/echo("Text Display") linux/BasicSystemCommandsGroup -.-> linux/test("Condition Testing") linux/BasicFileOperationsGroup -.-> linux/ls("Content Listing") linux/BasicFileOperationsGroup -.-> linux/chmod("Permission Modifying") linux/FileandDirectoryManagementGroup -.-> linux/cd("Directory Changing") subgraph Lab Skills linux/echo -.-> lab-558712{{"How to check if a file is executable in Linux"}} linux/test -.-> lab-558712{{"How to check if a file is executable in Linux"}} linux/ls -.-> lab-558712{{"How to check if a file is executable in Linux"}} linux/chmod -.-> lab-558712{{"How to check if a file is executable in Linux"}} linux/cd -.-> lab-558712{{"How to check if a file is executable in Linux"}} end

Test executability with test -x

In this step, you will learn how to check if a file is executable using the test command with the -x option. The test command is a built-in shell command that evaluates conditional expressions. The -x option specifically checks for the executable permission.

First, let's create a simple text file in your ~/project directory. We'll use the echo command to put some text into a file named my_script.sh.

echo "echo 'Hello from the script!'" > ~/project/my_script.sh

Now, let's check if this file is executable. By default, when you create a new file this way, it won't have execute permissions.

Use the test -x command followed by the path to the file:

test -x ~/project/my_script.sh

After running this command, you won't see any output if the test is successful (meaning the file is NOT executable). If the file were executable, test -x would return a status of 0, which is typically interpreted as "true" or "success" in shell scripting, but it doesn't print anything to the terminal by default.

To see the result of the test command, we can check the exit status of the previous command using $?. An exit status of 0 means the test was true (the file is executable), and a non-zero status (usually 1) means the test was false (the file is not executable).

echo $?

You should see an output of 1, indicating that my_script.sh is currently not executable.

Now, let's make the file executable using the chmod command. chmod is used to change the permissions of files and directories. We'll use +x to add the execute permission for the owner of the file.

chmod +x ~/project/my_script.sh

Now, let's test for executability again:

test -x ~/project/my_script.sh

And check the exit status:

echo $?

This time, the output should be 0, confirming that the file is now executable.

Finally, let's try running the script to see the output:

~/project/my_script.sh

You should see:

Hello from the script!

This confirms that you successfully made the file executable and ran it.

Check permissions with ls -l

In this step, you will learn how to view detailed file permissions using the ls -l command. This command provides a long listing format, showing various attributes of files and directories, including permissions, ownership, size, and modification time.

Navigate to your ~/project directory if you are not already there. You can use the cd command:

cd ~/project

Now, list the files in this directory using the ls -l command:

ls -l

You should see output similar to this (the exact details like date and time will vary):

-rwxr-xr-x 1 labex labex   30 <date> <time> my_script.sh

Let's break down the first part of the output, which represents the file permissions: -rwxr-xr-x. This string of ten characters tells you a lot about who can do what with the file.

The first character indicates the file type:

  • -: A regular file (like our my_script.sh)
  • d: A directory
  • l: A symbolic link

The next nine characters are grouped into three sets of three, representing permissions for:

  1. Owner: The user who owns the file (in this case, labex).
  2. Group: The group that owns the file (also labex).
  3. Others: Everyone else on the system.

Within each set of three characters, the permissions are represented as:

  • r: Read permission (allows viewing the file's contents)
  • w: Write permission (allows modifying or deleting the file)
  • x: Execute permission (allows running the file as a program)
  • -: Indicates that the permission is not granted

So, for -rwxr-xr-x:

  • The owner (labex) has rwx permissions: read, write, and execute.
  • The group (labex) has r-x permissions: read and execute, but not write.
  • Others have r-x permissions: read and execute, but not write.

This output confirms that after using chmod +x in the previous step, the owner (labex) now has execute permission (x) for my_script.sh.

You can also use ls -l to check the permissions of directories. For example, let's look at the permissions for the ~/project directory itself:

ls -ld ~/project

The -d option tells ls to list the directory itself, not its contents. The output might look like this:

drwxr-xr-x 2 labex labex 4096 <date> <time> /home/labex/project

Here, the first character d indicates it's a directory. The permissions rwxr-xr-x mean:

  • Owner (labex) has read, write, and execute permissions on the directory. For a directory, 'execute' permission allows you to enter the directory and access its contents.
  • Group (labex) and Others have read and execute permissions on the directory, but not write.

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

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 a more structured format than ls -l.

The stat command displays file status. It can be particularly useful for scripting or when you need precise timestamps.

Let's use stat on the my_script.sh file you created in the previous steps. Make sure you are in the ~/project directory or provide the full path to the file.

stat ~/project/my_script.sh

You will see output similar to this:

  File: /home/labex/project/my_script.sh
  Size: 30              Blocks: 8          IO Block: 4096   regular file
Device: <device_id>     Inode: <inode_number>  Links: 1
Access: (<permissions>/-rwxr-xr-x)  Uid: ( 5000/   labex)   Gid: ( 5000/   labex)
Access: <date> <time>.<nanoseconds> +<timezone_offset>
Modify: <date> <time>.<nanoseconds> +<timezone_offset>
Change: <date> <time>.<nanoseconds> +<timezone_offset>
 Birth: -

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

  • File: The name and path of the file.
  • Size: The size of the file in bytes.
  • Access: (<permissions>/-rwxr-xr-x): This line shows the file permissions in both octal format (e.g., 0755 which corresponds to rwxr-xr-x) and the symbolic format you saw with ls -l. It also shows the User ID (Uid) and Group ID (Gid) of the file's owner and 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 more detailed view of file information compared to ls -l. It's especially helpful when you need to examine timestamps or the numerical representation of permissions.

You can also use stat on directories:

stat ~/project

The output will be similar, but the File type will indicate it's a directory.

Experiment with stat on different files and directories in your ~/project directory to see the variations in the output.

Summary

In this lab, you learned how to check if a file is executable in Linux using the test -x command. You created a simple script file, used test -x and checked the exit status (echo $?) to confirm its initial non-executable state (exit status 1). You then used chmod +x to add execute permissions and verified the change with test -x and echo $?, observing an exit status of 0.

You also explored alternative methods for checking file permissions. Using ls -l, you learned to interpret the permission string, specifically the 'x' character, to identify executable files. Finally, you used the stat command to view detailed file information, including access permissions, providing another way to confirm executability.