Skip to main content

How to Check if File is Executable

β€’ 2 min read
bash

Quick Answer: Check if File is Executable

To check if a file is executable in Bash, use the -x operator: if [ -x "$file" ]. This returns true if the file exists and your current user has execute permission. Use which to find executable files in PATH.

Quick Comparison: File Executable Testing

TestPurposeSyntaxBest For
-xExecutable permission[ -x "$file" ]Simple check
-f -xFile + executable[ -f "$f" -a -x "$f" ]Script check
whichIn PATHwhich scriptnameFinding programs
typeCommand typetype commandnameShell commands
stat %AFull permissionsstat -c %A "$file"Detailed view

Bottom line: Use -x to check executable bit, use which to find in PATH.


Testing Execute Permission

The -x operator tests if a file is executable. This is essential before running scripts or binaries. An executable file might be a shell script, compiled program, or any file with the execute bit set.

Basic Execute Permission Check

#!/bin/bash

script="/usr/local/bin/myscript.sh"

if [ -x "$script" ]; then
  echo "Script is executable"
  "$script"
else
  echo "Script is not executable"
fi

Running Files Only if Executable

#!/bin/bash

SCRIPT_DIR="/opt/scripts"

# Only run executable scripts
for script in "$SCRIPT_DIR"/*; do
  if [ -x "$script" ]; then
    echo "Running: $(basename "$script")"
    "$script"
  fi
done

Practical Example: Command Availability

#!/bin/bash

require_executable() {
  local program="$1"

  # Try to find program in PATH
  if which "$program" &>/dev/null; then
    if [ -x "$(which "$program")" ]; then
      echo "$program is available and executable"
      return 0
    fi
  fi

  echo "ERROR: $program is not available or not executable"
  return 1
}

require_executable "bash"
require_executable "python"
require_executable "missing_program"

Checking Scripts Before Running

#!/bin/bash

run_script() {
  local script="$1"

  # Verify it's a regular file
  if [ ! -f "$script" ]; then
    echo "ERROR: Not a regular file"
    return 1
  fi

  # Verify it's executable
  if [ ! -x "$script" ]; then
    echo "ERROR: Script is not executable"
    echo "To make it executable, run: chmod +x $script"
    return 1
  fi

  # Run the script
  echo "Executing: $script"
  "$script"
  return $?
}

run_script "/home/user/myscript.sh"

Real-World Example: Cron Job Validator

#!/bin/bash

validate_cron_script() {
  local script="$1"

  echo "Validating cron script: $script"

  # File must exist
  [ -f "$script" ] || {
    echo "ERROR: Script does not exist"
    return 1
  }

  # Must be executable
  [ -x "$script" ] || {
    echo "ERROR: Script is not executable"
    echo "Run: chmod +x $script"
    return 1
  }

  # Should have valid shebang
  first_line=$(head -1 "$script")
  if [[ "$first_line" != "#!"* ]]; then
    echo "WARNING: Script doesn't have shebang"
  fi

  echo "Script is valid for cron"
  return 0
}

validate_cron_script "/etc/cron.daily/backup.sh"

Batch Check: Find Executable Files

#!/bin/bash

find_executables() {
  local dir="$1"

  echo "Executable files in $dir:"

  for file in "$dir"/*; do
    if [ -x "$file" ]; then
      echo "  βœ“ $(basename "$file")"
    fi
  done
}

find_executables "/usr/local/bin"

Comparing File States

#!/bin/bash

compare_files() {
  local file1="$1"
  local file2="$2"

  echo "File: $file1"
  [ -x "$file1" ] && echo "  - Executable: YES" || echo "  - Executable: NO"
  [ -r "$file1" ] && echo "  - Readable: YES" || echo "  - Readable: NO"
  [ -w "$file1" ] && echo "  - Writable: YES" || echo "  - Writable: NO"

  echo ""
  echo "File: $file2"
  [ -x "$file2" ] && echo "  - Executable: YES" || echo "  - Executable: NO"
  [ -r "$file2" ] && echo "  - Readable: YES" || echo "  - Readable: NO"
  [ -w "$file2" ] && echo "  - Writable: YES" || echo "  - Writable: NO"
}

compare_files "/bin/bash" "/tmp/test.txt"

Making a File Executable

When execute permission is missing:

#!/bin/bash

ensure_executable() {
  local script="$1"

  if [ -f "$script" ]; then
    if [ ! -x "$script" ]; then
      echo "Making script executable: $script"
      chmod +x "$script"
    fi
    echo "Script is executable"
  else
    echo "ERROR: Script not found"
    return 1
  fi
}

ensure_executable "/home/user/newscript.sh"

Important Notes

  • Execute permission on a file allows running it as a program
  • Execute permission on a directory allows listing its contents
  • A directory needs read AND execute permission to access files in it
  • The -x test checks if the file has execute permission for you
  • Scripts need both a shebang and execute permission to run

Quick Reference

# Check if executable
[ -x "$file" ] && echo "Executable"

# Check if NOT executable
[ ! -x "$file" ] && echo "Not executable"

# Make executable
chmod +x "$file"

# Check multiple attributes
[ -f "$file" ] && [ -x "$file" ] && echo "Executable file"

# Find all executables in directory
find "$dir" -type f -executable

Summary

Before running any script or program, verify it’s executable using the -x test operator. This prevents errors and helps you catch permission issues early. Always make your scripts executable with chmod +x before running them.