-
Bash Script for checking folder
Im trying to write a script that checks certiant folders and depending if theres any files in them it will start a program. I have been looking through a few of the bash scripting toutorials that have been posted and i can't find how to do this. I have all the script figured out exept how to tell if there are files in a folder.
Thanks
VoiDeR
-
Here's one way you can probably do it...
Listing a directory and piping it to the "wc" command with the "-l" option will yield the number of files in a given directory. Take the output of that into a variable and test for a value greater than zero. below is a quick sample of ls against an empty directory vs one that contains 93 files. You can check the man or info pages for options to ls , wc, if and test.
[ernieg@manito ernieg]$ ls Shared |wc -l
0
[ernieg@manito ernieg]$ ls Video |wc -l
93
You could put something like this in a script:
[ernieg@manito ernieg]$ if ((`ls Video |wc -l`)); then echo "there are files"; else echo "there are no files"; fi
there are files
[ernieg@manito ernieg]$ if ((`ls Shared |wc -l`)); then echo "there are files"; else echo "there are no files"; fi
there are no files
There are of course a ton of variations to this...
This is just one example.
Hope that helps!
-
Why can't you just run the program and trap the error status that it would return if there aren't any files?
What program are you running?
-
giFT is the program. Basicaly what i want to do is on boot up check the incoming folder and if there are any files in it to start giftd so it will continue downloading. Its kinda pointless to start it if im not currently downloading anything.
VoiDeR
ps wheres the spell check button i cant seem to find it
-
Ah, yeah that makes sense.
There isn't really a spellcheck button that I've ever seen...
As for the issue, I think a:
Code:
files=$(ls /directory | wc -l
files=${files:-0} # set it to 0 if it's blank for some reason
if [ "$files" -gt 0 ] ; then
# do whatever
fi
would probably work...
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
|