Tuesday, August 23, 2022
HomeNetworking24 methods to examine the standing of information utilizing if instructions on...

24 methods to examine the standing of information utilizing if instructions on Linux


There are much more methods to examine information utilizing if instructions than many people notice. Though this info is included within the bash man web page, that man web page has 1000’s of strains and you could possibly simply end up paging down greater than 100 instances to succeed in it.

This put up, supplies info on every choice and examples for a few of the most helpful ones.

Checking if a file exists

One of the vital generally used checks for checking on information is if [ -f filename ]. This take a look at will end in true if the file exists and is an everyday file—not a listing or a symbolic hyperlink. You would possibly use it like this:

$ if [ -f myfile.txt ]; then
>   echo myfile.txt exists
> else
>   contact myfile.txt
> fi
myfile.txt exists

The command above would create the file utilizing the contact command if it doesn’t exist already. The next command would solely inform you if the file is or isn’t an everyday file:

$ if [ -f /home/shs ]; then
>   echo file exists
> else
>   echo file will not be an everyday file
> fi
file will not be an everyday file

If you wish to know whether or not a file exists no matter what sort of file it’s, you should utilize the -a or the -e take a look at.

$ if [ -e symlink ]; then
>   echo file exists
> else
>   echo no such file
> fi
file exists

Checking if a file is a listing

Utilizing -d will inform you if a file is a listing.

$ if [ -d /home/shs ]; then
>   echo listing
> else
>   echo not a listing
> fi
listing

Reversing the impact of if checks

You possibly can reverse the consequences of an if take a look at by previous the take a look at with a ! (not) signal. To check if a file has content material, you could possibly use a command like this one:

$ if [ -s emptyfile ]; then
>   echo has content material
> else
>   echo empty
> fi

To vary the command above to check whether or not the file is empty, do that:

$ if [ ! -s emptyfile ]; then
>   echo empty
> else
>   echo has content material
> fi

This of -s as “some content material” and ! -s as “no content material”.

The ! can be utilized with any of the if checks. Listed here are a number of examples:

if [ ! -d file ]      <— file will not be a listing
if [ ! -e file ]      <— file doesn’t exist
if [ ! -r file ]      <— file will not be readable

Checking file permissions

Different particularly helpful checks can inform you for those who can learn a file, write to a file or execute a file.

$ if [ -r ~/bin/loop ]; then
>   echo readable
> fi
readable
$ if [ -w ~/bin/loop ]; then > echo writable > fi writable
$ if [ -x ~/bin/loop ]; then > echo executable > fi executable

Be aware that these checks solely examine your entry rights, not anybody else’s and that the information examined above are all within the bin listing within the consumer’s house listing (~/bin).

It’s also possible to examine whether or not a file is a character-special file, if its SGID (set group ID) bit is about and if its SUID (set consumer ID) bit is about. The chmod command units the bits being examined within the second and third examples.

$ if [ -c /dev/tty11 ]; then
>   echo character-special file
> fi
character-special file
$ chmod 7755 yn $ if [ -g yn ]; then > echo SGID set > fi SGID set
$ if [ -u yn ]; then > echo SUID set > fi SUID set

Checking if a file was just lately modified

The -N checks whether or not a file’s content material was modified because the final time it was learn. The echo command beneath provides a line, so the second take a look at result’s the other of the primary.

$ if [ -N testfile ]; then
>   echo file has been modified
> else
>   echo file not modified
> fi
file not modified
$ echo “——-“ >> testfile $ $ if [ -N testfile ]; then > echo file has been modified > else > echo file not modified > fi file has been modified

Evaluating file ages

You should use if checks to find out if one file has been modified extra just lately than one other. The -nt take a look at means “newer than”.

$ if [ file1 -nt file2 ]; then
>   echo file1 is newer
> else
>   echo file2 is newer
> fi
file2 is newer

Utilizing -ot (older than) instead of -nt has the other impact.

All if instructions for checking the standing of information

Here’s a listing of all of the if checks that examine the standing of information. Simply keep in mind that any may be reversed with a ! image (e.g., if [ ! file1 -ot file2 ]).

  • if [ -a FILE ] — True if FILE exists
  • if [ -b FILE ] — True if FILE exists and is a block-special file
  • if [ -c FILE ] — True if FILE exists and is a character-special file
  • if [ -d FILE ] — True if FILE exists and is a listing
  • if [ -e FILE ] — True if FILE exists
  • if [ -f FILE ] — True if FILE exists and is an everyday file
  • if [ -g FILE ] — True if FILE exists and its SGID bit is about
  • if [ -h FILE ] — True if FILE exists and is a symbolic hyperlink
  • if [ -k FILE ] — True if FILE exists and its sticky bit is about
  • if [ -p FILE ] — True if FILE exists and is a named pipe (FIFO)
  • if [ -r FILE ] — True if FILE exists and is readable
  • if [ -s FILE ] — True if FILE exists and has a measurement better than zero
  • if [ -t FD ] — True if file descriptor FD is open and refers to a terminal
  • if [ -u FILE ] — True if FILE exists and its SUID (set consumer ID) bit is about
  • if [ -w FILE ] — True if FILE exists and is writable
  • if [ -x FILE ] — True if FILE exists and is executable
  • if [ -O FILE ] — True if FILE exists and is owned by the efficient consumer ID
  • if [ -G FILE ] — True if FILE exists and is owned by the efficient group ID
  • if [ -L FILE ] — True if FILE exists and is a symbolic hyperlink
  • if [ -N FILE ] — True if FILE exists and has been modified because it was final learn
  • if [ -S FILE ] — True if FILE exists and is a socket
  • if [ FILE1 -nt FILE2 ] — True if FILE1 has been modified extra just lately than FILE2 or if FILE1 exists and FILE2 doesn’t
  • if [ FILE1 -ot FILE2 ] — True if FILE1 is older than FILE2 or if FILE2 exists and FILE1 doesn’t
  • if [ FILE1 -ef FILE2 ] — True if FILE1 and FILE2 consult with the identical system and inode numbers

<!—

if take a look at outcome
[ -a FILE ] True if FILE exists
[ -b FILE ] True if FILE exists and is a block-special file
[ -c FILE ] True if FILE exists and is a character-special file
[ -d FILE ] True if FILE exists and is a listing
[ -e FILE ] True if FILE exists
[ -f FILE ] True if FILE exists and is an everyday file
[ -g FILE ] True if FILE exists and its SGID bit is about
[ -h FILE ] True if FILE exists and is a symbolic hyperlink
[ -k FILE ] True if FILE exists and its sticky bit is about
[ -p FILE ] True if FILE exists and is a named pipe (FIFO)
[ -r FILE ] True if FILE exists and is readable
[ -s FILE ] True if FILE exists and has a measurement better than zero
[ -t FD ] True if file descriptor FD is open and refers to a terminal
[ -u FILE ] True if FILE exists and its SUID (set consumer ID) bit is about
[ -w FILE ] True if FILE exists and is writable
[ -x FILE ] True if FILE exists and is executable
[ -O FILE ] True if FILE exists and is owned by the efficient consumer ID
[ -G FILE ] True if FILE exists and is owned by the efficient group ID
[ -L FILE ] True if FILE exists and is a symbolic hyperlink
[ -N FILE ] True if FILE exists and has been modified because it was final learn
[ -S FILE ] True if FILE exists and is a socket
[ FILE1 -nt FILE2 ] True if FILE1 has been modified extra just lately than FILE2 or if FILE1 exists and FILE2 doesn’t
[ FILE1 -ot FILE2 ] True if FILE1 is older than FILE2 or if FILE2 exists and FILE1 doesn’t
[ FILE1 -ef FILE2 ] True if FILE1 and FILE2 consult with the identical system and inode numbers

—>

Wrap-Up

There are a number of methods to examine and confirm information on the command line or inside scripts. A few of these choices will help be certain that your scripts examine information earlier than attempting to make use of them or keep away from overwriting them in the event that they exist already. These checks typically play an essential function when utilizing scripts to automate your work.

Be part of the Community World communities on Fb and LinkedIn to touch upon matters which are high of thoughts.

Copyright © 2022 IDG Communications, Inc.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments