Linux Bash test Command Tutorial With Examples

RSS
EMAIL
FACEBOOK
FACEBOOK
GOOGLE
GOOGLE
https://www.poftut.com/linux-bash-test-command-tutorial-examples/
PINTEREST
PINTEREST
INSTAGRAM

Bash have programmatic features which gives system administrators a lot of abilities. One of them is test command which will check given conditions and return logic results like true or false . There are a lot of different check functions where we will look most popular and needed ones.

 Syntax

Check If A File Exists

One of the most popular usage of the test command is checking a files existence. We will check if the file named key exists.

Check If A File Exists

Check If A File Exists&& echo “regular file”

Check If A File Is Regular File

In previous example we have checked the files or directory existence. There are different type of files in Linux. One of them which is the most used one is regular file. Regular file contains data like picture, voice, text, SQL data etc.

Check If A File Is Regular File

Check If A File Is Regular File

Check If A File Is Directory

We can also check if a file is directory with -d option.

Check If A File Is Directory

Check If A File Is Directory

Check If A File Is Symbolic& echo “symlink” Link

Symbolic links provides flexibility to use same file in multiple paths as a single file. We can check if the file is symbolic link or not with -L option.

Check If A File Is Symbolic Link

Check If A File Is Symbolic Link

Check If A File Is Writable

Before trying to write data to file we can check whether the file is writable by the current user. The writable status may change according to current user who try to write. We will use -w option.

LEARN MORE  Git - File Operations

Check If A File Is Readable

Check If A File Is Readable

Check If A File Is Readable

Another access check is checking if a file is readable. We will use -r option with the file name in the example below.

Check If A File Is Readable

Check If A File Is Readable

Check If File1 Is Newer Than File2

Another useful check operation is comparing two files. We will compare if file1 is newer than file2 . We will use -nt option in this example.

Check If File1 Is Newer Than File2

Check If File1 Is Newer Than File2

Check If File1 Is Older Than File2

We can check the reverse of the previous example. We will check if file1 is older than file2 with -ot option with the following example.

Check If File1 Is Older Than File2

Check If File1 Is Older Than File2

RSS
EMAIL
FACEBOOK
FACEBOOK
GOOGLE
GOOGLE
https://www.poftut.com/linux-bash-test-command-tutorial-examples/
PINTEREST
PINTEREST
INSTAGRAM

You may also like...

Leave a Reply

Your email address will not be published.

Enjoy this blog? Please spread the word :)