Determine if folder has subfolders with out overhead from find or ls
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.
Determine if folder has subfolders with out overhead from find or ls
I am writing a program with a folder tree using php.
I want to determine if a folder has subfolders or not for the folder tree so i can use plus/minus for expand.
For example, assume i'm looking at the following folder structure
----------------------------
+etc
+home
-usr
-local
+bin
+sbin
-var
+lib
+mysql
+www
-----------------------------
where /usr/local and /var are expanded & everything else is collapsed. I want to avoid having a +/- sign if a folder has no subfolders. For example, my /usr/local/bin and /usr/local/sbin don't have subfolders so I would rather it look like this:
The graphics side of things i can handle. The part i'm having a problem with is the determining of subfolders. The command I'm using right now to determine is this:
`find /some/path -type d -maxdepth 1`
However I've got some folders with a bunch of stuff in them. So I get a lot of overhead as it searches through unnecessary stuff.
I've tried something similar with ls but get a lot of overhead as well.
I've searched google but didnt find anything useful.
well it's because of permission reasons. I'm really kinda cheating with sudo to do stuff. I don't want to give the webserver it's self a ton of access to the server, but I'd like to be able to restrict it so sudo made the most sense to me. opendir doesn't give you that option unless I'm missing something. Any thoughts on that would be appreciated.
I even tried writing an external perl script with the following code to exit out as soon as it finds a subfolder.
---------------
opendir (DIR, $ARGV[0]) || die "Can't open directory $ARGV[0]\n";
$ct = 0;
while ($next = readdir(DIR)) {
$ct++ if (-d $ARGV[0]."/".$next);
if ($ct > 2) { print "true\n"; exit;}
}
closedir (DIR);
print "false\n";
-------------------------
It works fine, but the problem is it still has quite a bit of overhead, although is slightly quicker.
For the time being I'm just going to allow +/- signs on folders w/o subfolders but would like to fix that later.
You can use glob with the option GLOB_ONLYDIR which will get you an array with just the directory names of a given directory. Of course that if the array is empty, there are no directory
i'll look into that. for now i will keep it expandable for all directories. it's not super critical to the functionality of the program so i'll hold off on it
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.