3

How can you check if a command can be executable or not in Linux.

Example:

xeyes 
0

    3 Answers 3

    10

    From man bash in the conditional expressions paragraph:

    -x file

    True if file exists and is executable.

    So, you can use:

    [ -x /usr/bin/xeyes ] && echo "File is executable" || echo "File is not an executable or does not exist" 
    3
    • This fails completely when using -x $(which bogus) instead of -x /usr/bin/bogus. For it, which bogus >/dev/null && echo "available" || echo "unavailable" works just fine for me.
      – Asclepius
      CommentedNov 8, 2020 at 21:25
    • -x $(which bogus) does not work for me either, but adding quotes around the expression as such: -x "$(which bogus)" does.CommentedAug 9, 2022 at 22:45
    • Gee, both these work -- suppose our command is needrestart: if [[ -x "$(command -v needrestart)" ]] ; then echo "yes"; else echo "nope"; fi or: if [[ -x "$(which needrestart)" ]] ; then echo "yes"; else echo "nope"; fi It just happens that this command is not available to regular users, only to root, so...CommentedJan 11, 2024 at 15:58
    3

    If you know the location of where the command binary is kept, just do an ls -l. If you don't know the location first find out location using which command

    $ which xeyes /usr/bin/xeyes 

    If the command has execute permission ( x ) set, then it is executable.

    $ ls -l /usr/bin/ -rw-rw-r-- 1 arushirai arushirai 0 May 23 11:58 123 -rwxrwxr-x 1 arushirai arushirai 0 May 23 11:58 xeyes 

    The -x <filename> actually checks if the file has execute permission set

    Explanation:

    THe first column of ls -l shows the permission on file.

    -rwxrwxr-x 
    • r is read permission
    • w is write permission
    • x is execute permission

    -rwxrwxr-x

    • 1st bit: tells type of file ( - is for regular file )
    • Next 3 bits: owner permission ( rwx : read, write and execute )
    • Next 3 bits: group permission ( rwx : read , write and execute )
    • Next 3 bits: other permission (r-x : read and execute permission )

    For more information on file permissions read: https://wiki.archlinux.org/index.php/File_permissions_and_attributes

    1
    • In case your command is symlinked (is a shortcut), you can find the source of the symlink: readlink -f $(which your-command-here)CommentedJan 11, 2024 at 16:00
    1

    If you don't known a path to the command you can use which to check where it is (of course, if you have it in $PATH). If you know a path to command file use if -x /path/to/command statement.

      Start asking to get answers

      Find the answer to your question by asking.

      Ask question

      Explore related questions

      See similar questions with these tags.