Skip to content

What is test Linux command?

The Linux test command is used to check conditions and return a successful or failed exit status. It is commonly used in shell scripts to test file attributes and perform string comparisons.

test Syntax:

Terminal window
test [option] [file...]

test Options:

OptionDescription
-fCheck if the file is an ordinary file
-dCheck if the file is a directory
-rCheck if the file is readable
-wCheck if the file is writable
-xCheck if the file is executable

Parameters:

ParameterDescription
fileThe file to be tested

test Command Usage Examples:

Check if a File Exists

Terminal window
test -e file.txt && echo "file.txt exists" || echo "file.txt does not exist"

This command checks if the file “file.txt” exists in the current directory and prints a message based on the result.

Check if a Directory is Empty

Terminal window
test -z "$(ls -A /path/to/directory)" && echo "Directory is empty" || echo "Directory is not empty"

Tests whether the directory “/path/to/directory” is empty or not by checking if the output of ls -A is empty.

Verify File Read Permission

Terminal window
test -r file.txt && echo "file.txt is readable" || echo "file.txt is not readable"

Checks if the file “file.txt” has read permission and displays a message accordingly.

Compare Numerical Values

Terminal window
test 5 -eq 5 && echo "5 equals 5" || echo "5 does not equal 5"

Compares two numerical values using the test command and prints a message based on the comparison result.

Check String Equality

Terminal window
test "hello" = "world" && echo "Strings are equal" || echo "Strings are not equal"

Tests whether the strings “hello” and “world” are equal and outputs the result message.

How do I check if a file exists in a directory in Linux?

To check if a file exists in a directory using the test command, you can use the following syntax:

Terminal window
test -f /path/to/file

How can I verify if a string is empty in a bash script?

To verify if a string is empty in a bash script with the test command, you can use the following example:

Terminal window
[ -z "$mystring" ]

How do I compare two strings in a shell script using test?

To compare two strings in a shell script using the test command, you can use the following syntax:

Terminal window
[ "$string1" = "$string2" ]

How can I check if a number is greater than another in Linux?

To check if a number is greater than another in Linux using the test command, you can use the following example:

Terminal window
[ 5 -gt 3 ]

How do I check if a directory is empty in Linux?

To check if a directory is empty in Linux with the test command, you can use the following syntax:

Terminal window
[ -z "$(ls -A /path/to/directory)" ]

How can I test if a file is writable in a shell script?

To test if a file is writable in a shell script using the test command, you can use the following example:

Terminal window
[ -w /path/to/file ]

How do I check if a variable is set in a bash script?

To check if a variable is set in a bash script with the test command, you can use the following syntax:

Terminal window
[ -n "$myvariable" ]

How can I determine if a file is executable in Linux?

To determine if a file is executable in Linux using the test command, you can use the following example:

Terminal window
[ -x /path/to/file ]

Applications of the test command

  • Checking if a file exists
  • Checking if a file is readable
  • Checking if a file is writable
  • Checking if a file is executable
  • Comparing strings
  • Comparing integers
  • Checking if a variable is set or not
  • Checking if two files are the same
  • Checking if two files are different
  • Checking file types
  • Combining multiple test conditions in a single statement