Programming This 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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
|
 |
12-20-2006, 04:11 PM
|
#1
|
LQ Newbie
Registered: Jan 2004
Location: TX
Distribution: Fedora Core
Posts: 29
Rep:
|
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:
----------------------------
+etc
+home
-usr
-local
bin
sbin
-var
+lib
+mysql
+www
-----------------------------
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.
Any help would be appreciated.
Thanks
David
|
|
|
12-20-2006, 04:55 PM
|
#2
|
LQ Guru
Registered: Jan 2001
Posts: 24,149
|
I don't think there's much else you could use. You could try something perhaps like this:
ls -l | grep "^d"
And perhaps use awk to get the directories listed without the other junk, unless you have another way to get it.
|
|
|
12-20-2006, 05:48 PM
|
#3
|
LQ Guru
Registered: Nov 2003
Location: N. E. England
Distribution: Fedora, CentOS, Debian
Posts: 16,298
Rep:
|
Moved: This thread is more suitable in the Programming forum and has been moved accordingly to help your thread/question get the exposure it deserves.
|
|
|
12-20-2006, 06:36 PM
|
#4
|
LQ Newbie
Registered: Nov 2006
Distribution: gentoo
Posts: 2
Rep:
|
See "stat -c=%f *" Look at info stat and man stat. Switch between -c=%f and -c=%F to get a feel for its output.
|
|
|
12-20-2006, 09:30 PM
|
#5
|
Senior Member
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,821
Rep: 
|
I don't get it. If you are using php then why make calls into the shell for the directory info. Use the php methods:
http://us2.php.net/function.opendir
|
|
|
12-21-2006, 08:51 AM
|
#6
|
LQ Newbie
Registered: Jan 2004
Location: TX
Distribution: Fedora Core
Posts: 29
Original Poster
Rep:
|
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.
|
|
|
12-21-2006, 11:58 AM
|
#7
|
LQ Newbie
Registered: Jan 2004
Location: TX
Distribution: Fedora Core
Posts: 29
Original Poster
Rep:
|
i haven't been able to find much of a solution.
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.
|
|
|
12-21-2006, 12:35 PM
|
#8
|
Member
Registered: May 2006
Location: Argentina
Distribution: SuSE 10
Posts: 173
Rep:
|
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
http://ar.php.net/manual/en/function.glob.php
Cheers!
|
|
|
12-21-2006, 02:26 PM
|
#9
|
LQ Newbie
Registered: Jan 2004
Location: TX
Distribution: Fedora Core
Posts: 29
Original Poster
Rep:
|
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
|
|
|
All times are GMT -5. The time now is 04:56 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|