Linux test Command Tutorial for Beginners (with Examples)

When you work with the Linux command line, you sometimes want to test certain things, such as integer values or whether or not a file is of a certain type. There is a built-in test command line utility that you can use to do most of these comparisons and tests.

In this tutorial, we will explain the basics of this tool with some easy-to-understand examples. But before we do that, we should mention that all the examples in this article have been tested on Ubuntu 22.04 LTS.

Linux test command

As already mentioned above, the test command is used to perform checks and comparisons. Here's its syntax:

test EXPRESSION

And here's what the man page says about this utility:

test - check file types and compare values

Following are some Q&A-styled examples that should give you a good idea on how the tool works.

Note: Keep in mind that some of the command line options test provides are most useful when used within shell scripts.

Q1. How to compare two strings?

Simple, you just need to use the equal to (=) sign between them. For example:

test howto = forge

A better way would be to write something like this:

test howto = forge && echo "same"

So if the strings are same, the word "same" should be printed in output, else nothing should be printed.

How to compare two strings?

Similarly, you can use the following template if you want to test for inequality.

STRING1 != STRING2

Q2. How to compare integers using test?

This is also very straight forward - just compare them using '-eq'. For example:

test 5 -eq 7 && echo "same"

Here's a screenshot showing how this command line option works:

How to compare integers using test?

Similarly, you can use '-ge' to test greater than or equal to, '-gt' for greater than, '-le' for less than or equal to, '-lt' for less than, and '-ne' for not equal.

Q3. How to test/compare files using test?

To test which of the two files are newer, use '-nt'. For example:

test file1 -nt file2

Here's how I tested it on my system:

How to test/compare files using test?

Other file comparisons that you can perform include which among the two files is older (-ot) and whether two files have same device and inode numbers (-ef).

To check whether a given file is a directory, use the -d option in the following way:

test -d [filename]

For example:

test -d new_dir

Following are some other file-type test options the 'test' command offers:

Result of test command use

The test command dereferences symbolic links, although there are a couple of exceptions. Following is what the man page says about this:

Except for -h and -L, all FILE-related tests dereference symboliclinks.

In case you aren't aware, both -h and -L check if a file exists and is a symbolic link - so their exclusion makes sense, right?

Conclusion

The test command offers a lot of options, but broadly speaking, you can club them into 3-4 categories. We have provided examples on each category. So try these out, and when you're done, head to the utility's man page for more info.

Share this page:

2 Comment(s)