[SOLVED] Checking multiple directories if they are empty. or if they have files inside.
Linux - SoftwareThis forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
How do you know those are directories? Use the -a or -F options to ls for clarity...see the man page.
Also take a look at the man page for find. There may be options there that will answer your question.
Another possibility is the bash test command. Try man test, and if that gives you the bash man page, search tor test.
Distribution: Arch Linux && OpenBSD 7.4 && Pop!_OS && Kali && Qubes-Os
Posts: 824
Original Poster
Rep:
Quote:
Originally Posted by scasey
How do you know those are directories?
those are directories created by suricata and those might contain files which are named by their sha256sum, for example if that file name starts with 4fxxxx it is placed to folder 4f.
what i am doing is getting possible malware from my network and uploading it to virustotal, that system is automated, written in perl. i just started writing it and dont want to "invent the wheel again", ill check out what you suggested.
He asked for pointers.
Mine works but yours is more elegant and does not necessarily require the redirection. I was looking for a quick way to do what he asked and wrote a quick script that did not use find.
One problem I occasionally have with find even when running as root is the error messages that can be produced and often swamp the actual output, so they have to be gotten rid of as well. For example "find / -name somefilename" may produce a lot of read errors on /proc, /sys, /run, and others, even when run with sudo.
Even with your edits to my test statement it may be necessary to write the $file as "$file" in case the actual directory name contained spaces or shell specific special characters. So the test, to eliminate the short name problem would be more like
Yes, the $file should be in quotes as well.
Another idea: a glob gives pathnames, and a trailing / even restricts the match to directories i.e. omits files
Code:
for dir in /var/log/suricata/filestore/*/
do
if [ -z "$(ls "$dir" 2>/dev/null)" ]
then
echo "$dir is empty
else
echo "$dir has files"
fi
done
Last edited by MadeInGermany; 02-08-2021 at 12:39 AM.
Reason: Added the missing "done"
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.