[SOLVED] Technique - alternatives to tell if directory empty or not
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
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.
Technique - alternatives to tell if directory empty or not
So this is just a query on personal preferences about what method you would use in a bash script to tell if a directory contained any (this needs to include hidden) files or directories?
The current method I am using is:
Code:
anyford=$(ls -A | wc -l)
if (( anyford > 0 ))
then
<do stuff here>
fi
I am normally abhorred to use ls if I can help it so would appreciate any feedback on what others would use in this scenario?
So this is just a query on personal preferences about what method you would use in a bash script to tell if a directory contained any (this needs to include hidden) files or directories?
The current method I am using is:
Code:
anyford=$(ls -A | wc -l)
if (( anyford > 0 ))
then
<do stuff here>
fi
I am normally abhorred to use ls if I can help it so would appreciate any feedback on what others would use in this scenario?
Though I've never studied internals of Linux file systems and can't tell whether "Links: 2" is correct/portable.
Each directory contains 2 hard-linked directories: . and .. (I'm not sure if this is technically true for the base of a filesystem, but at least the kernel pretends it's that way). If a directory has 2 links that only means it has no subdirectories; it could still have files.
ls might be the way to go, except it will fail if your user has --x access and not r-x. Then again, why would you need to know that if you had --x?
Kevin Barry
I have reviewed options for stat and confirmed ta0kira's information is correct that Links: only refers to the number of
directories (or hard links) within a directory, when used in this manner, so not appropriate for my current task.
I have reviewed options for stat and confirmed ta0kira's information is correct that Links: only refers to the number of
directories (or hard links) within a directory, when used in this manner, so not appropriate for my current task.
hmmm ... now I think you are stretching. I ran stat on an empty directory and one with files in it and the Blocks count
is the same ... so no real value to that measurement
Thanks for the new idea I would probably have to add maxdepth as well as only interested i current directory.
the idea I am trying to use it for is when a user removes an application I need to test a directory that may need to be removed as other applications
may also have installed files in the directory and hence it should not be removed so as to not break the other applications.
Thanks for the new idea I would probably have to add maxdepth as well as only interested i current directory.
the idea I am trying to use it for is when a user removes an application I need to test a directory that may need to be removed as other applications
may also have installed files in the directory and hence it should not be removed so as to not break the other applications.
Maybe you just need to rmdir /path || true.
Kevin Barry
Thanks for the suggestion Kevin. May I ask what purpose the '|| true' serves here? It does not seem to alleviate any error messages (which of course I can through to /dev/null).
Thanks for the suggestion Kevin. May I ask what purpose the '|| true' serves here? It does not seem to alleviate any error messages (which of course I can through to /dev/null).
Just a habbit from writing makefiles and controlling the return value of shell functions, I suppose.
Kevin Barry
This is true only if the directory is empty, otherwise you get an error. Single square brackets are mandatory, since filename expansion doesn't take place inside double square brackets.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.