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:
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.
Ken


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 |
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.
Ken