IT Questions and Answers :)

Thursday, August 22, 2019

The following Linux command was run with an exit status of zero: " find /home/terry -name "*".txt -name "*".text " What is the command's output?

The following Linux command was run with an exit status of zero: " find /home/terry -name "*".txt -name "*".text " What is the command's output?

  • All file names that end in ".text"
  • Nothing
  • Two files literally named "*.txt" and "*.text"
  • All file names that end in ".txt" 

 
The following Linux command was run with an exit status of zero: " find /home/terry -name "*".txt -name "*".text " What is the command's output?

EXPLANATION

The expression    -name "*".txt   -name "*".text   is evaluated left to right right for each name encountered as the directories are traversed.  Logical AND is the default when an operator is not specified between tests.  The asterisk is quoted, so it is not expanded by the shell when the ENTER key is pressed, but is used by "find" to match any value to the left of the period.   Each name is also evaluated for  "txt"   and  "text" to the right of the period.  Since the single name being tested cannot end in both .txt and .text, no name can be matched by the expression, and there will be no output to "find".
The following Linux command was run with an exit status of zero: " find /home/terry -name "*".txt -name "*".text " What is the command's output?
# find /home/terry -name "*".txt   -name "*".text  ##Running as root
# echo $?      ## Show the exit status of the above "find"
0
Note that the command was successful (exit status 0), even though there were no matches. An exit status of 0 means that "find" was successful in walking the directory tree.  An exit status of 0 does not mean that the expression was matched.
Using the  " -or "  operator, this "find" command will output names ending in  .txt  or  .text
# find /home/terry -name "*".txt  -or    -name "*".text
/home/terry/.local/share/contacts/WARNING_README.txt
/home/terry/tg/test.text
/home/terry/.config/libreoffice/4-suse/user/uno_packages/cache/log.txt
/home/terry/.mozilla/firefox/v0h37x7l.default/revocations.txt
/home/terry/.mozilla/firefox/v0h37x7l.default/SiteSecurityServiceState.txt
# echo $?      ## Show the exit status of the above "find"
0
Note that if the user does not have permission to traverse the directory, the exit status will indicate failure, and there won't be any hits for matching names in the directories for which access is denied to a non-root user:
$ find /home/terry -name "*".txt -o -name "*".text  2>/dev/null  ##redirecting stderr to remove clutter
/home/terry/tg/test.text
$ echo $?      ## Show the exit status of the above "find"
1

"GNU find searches the directory tree rooted at each given starting-point by evaluating the given expression from left to right, according to the rules of precedence (see section OPERATORS), until the outcome is known....
Operators join together the other items within the expression. They include for example -o (meaning logical OR) and -a (meaning logical AND).  Where an operator is missing, -a is assumed....
find exits with status 0 if all files are processed successfully, greater than 0 if errors occur. This is deliberately a very broad description, but if the return value is non-zero, you should not rely on the correctness of the results of find."

See also:
https://pubs.opengroup.org/onlinepubs/009695399/utilities/find.html

"EXIT STATUS
The following exit values shall be returned:
0  All path operands were traversed successfully.
>0  An error occurred."

SOURCE

http://man7.org/linux/man-pages/man1/find.1.html
Share:

0 comments:

Post a Comment

Popular Posts