Article 57SRQ Test if ANY file exists in a directory - bash script

Test if ANY file exists in a directory - bash script

by
taylorkh
from LinuxQuestions.org on (#57SRQ)
I thought I would post this question here as a) it is simple, b) I do offer a workaround which might help someone and c) the answer may just be "don't do that, it does not work.' :)

I have for a long time I have tested for the presence of a file thus Code:if [ -f /some/path/myfile ]
then
# take some action
fiToday I had the need to test for the presence of ANY files in the directory of interest. This did not go wellQuote:
#!/bin/bash
#example.sh
if [ -f /data/nzbget/queue/* ]
then
echo starting nzbget
else
echo nothing to do
fi

ken@t29:~$ example.sh
/home/ken/bin/example: line 4: [: /data/nzbget/queue/somefile.nzb: binary operator expected
I chaged my script to use the following logic to test for the presence of any files in the directoryCode:work2do=$(ls /data/nzbget/queue/*.nzb | wc -l)
if [ $work2do -gt 0 ]
then
echo starting nzbget
...This worked fine.

My question is... Is it possible to use the if [ -f logic construct to test for the presence of ANY files? I have looked at several example pages on-line and have not found anything to match what I am trying to do. The man page for bash in the section on CONDITIONAL EXPRESSIONS shows how to do all manner of examination of a file and its properties PROVIDED I know the name of the file. I see nothing about wild cards/regular expressions.

Kenlatest?d=yIl2AUoC8zA latest?i=l2ckrJ5yIwo:a2n61GsjMDE:F7zBnMy latest?i=l2ckrJ5yIwo:a2n61GsjMDE:V_sGLiP latest?d=qj6IDK7rITs latest?i=l2ckrJ5yIwo:a2n61GsjMDE:gIN9vFwl2ckrJ5yIwo
External Content
Source RSS or Atom Feed
Feed Location https://feeds.feedburner.com/linuxquestions/latest
Feed Title LinuxQuestions.org
Feed Link https://www.linuxquestions.org/questions/
Reply 0 comments