LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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


Reply
  Search this Thread
Old 12-20-2006, 04:11 PM   #1
davstin2002
LQ Newbie
 
Registered: Jan 2004
Location: TX
Distribution: Fedora Core
Posts: 29

Rep: Reputation: 15
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
 
Old 12-20-2006, 04:55 PM   #2
trickykid
LQ Guru
 
Registered: Jan 2001
Posts: 24,149

Rep: Reputation: 269Reputation: 269Reputation: 269
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.
 
Old 12-20-2006, 05:48 PM   #3
reddazz
LQ Guru
 
Registered: Nov 2003
Location: N. E. England
Distribution: Fedora, CentOS, Debian
Posts: 16,298

Rep: Reputation: 77
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.
 
Old 12-20-2006, 06:36 PM   #4
hvc2
LQ Newbie
 
Registered: Nov 2006
Distribution: gentoo
Posts: 2

Rep: Reputation: 0
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.
 
Old 12-20-2006, 09:30 PM   #5
crabboy
Senior Member
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,821

Rep: Reputation: 121Reputation: 121
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
 
Old 12-21-2006, 08:51 AM   #6
davstin2002
LQ Newbie
 
Registered: Jan 2004
Location: TX
Distribution: Fedora Core
Posts: 29

Original Poster
Rep: Reputation: 15
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.
 
Old 12-21-2006, 11:58 AM   #7
davstin2002
LQ Newbie
 
Registered: Jan 2004
Location: TX
Distribution: Fedora Core
Posts: 29

Original Poster
Rep: Reputation: 15
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.
 
Old 12-21-2006, 12:35 PM   #8
demon_vox
Member
 
Registered: May 2006
Location: Argentina
Distribution: SuSE 10
Posts: 173

Rep: Reputation: 30
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!
 
Old 12-21-2006, 02:26 PM   #9
davstin2002
LQ Newbie
 
Registered: Jan 2004
Location: TX
Distribution: Fedora Core
Posts: 29

Original Poster
Rep: Reputation: 15
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
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Determine if folder has subfolders with out overhead from find or ls davstin2002 Programming 1 12-20-2006 05:49 PM
how to determine the folder contain data or not izza_azhar Programming 2 05-24-2005 07:38 PM
need a script to delete all files from a folder and subfolders cccc Programming 1 03-04-2005 10:54 AM
How to make JHEAD work on all subfolders within a folder SupaDucta Linux - Newbie 0 11-26-2004 06:02 PM
changeing permissions for a folder + subfolders and files ? YBA^[x] Slackware 7 08-06-2004 04:07 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:51 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration