LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Slackware
User Name
Password
Slackware This Forum is for the discussion of Slackware Linux.

Notices

Reply
 
Search this Thread
Old 08-14-2010, 03:46 PM   #1
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 3,088

Rep: Reputation: 45
Listing only subdirectories.


Hi:

Being at a certain directory I want to list the names of all subdirs under this dir. I have found this can be done with 'find . -type d -maxdepth 1'. Question: can this be done with the ls command? Regards.
 
Old 08-14-2010, 04:04 PM   #2
Chuck56
Member
 
Registered: Dec 2006
Location: Colorado
Distribution: Slackware
Posts: 421

Rep: Reputation: 58
I might misunderstand your question but have you looked at the man page?

man ls

Try:

ls -d *
 
Old 08-14-2010, 04:09 PM   #3
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 3,088

Original Poster
Rep: Reputation: 45
Hi:
I have read ls man page throughout. But 'ls -d *' does exactly the same as 'ls'. At least in my system (slackware 12.0). Thanks for your reply.
 
Old 08-14-2010, 04:19 PM   #4
Chuck56
Member
 
Registered: Dec 2006
Location: Colorado
Distribution: Slackware
Posts: 421

Rep: Reputation: 58
ls -d */

or

ls | grep /
 
1 members found this post helpful.
Old 08-14-2010, 04:32 PM   #5
bgeddy
Senior Member
 
Registered: Sep 2006
Location: Liverpool - England
Distribution: slackware64 13.37 and -current, Dragonfly BSD
Posts: 1,810

Rep: Reputation: 227Reputation: 227Reputation: 227
Quote:
Hi:
I have read ls man page throughout. But 'ls -d *' does exactly the same as 'ls'. At least in my system (slackware 12.0). Thanks for your reply.
ls -d will give you the listing for the directory entry rather that it's contents - to see the difference try running ls -d mydir and ls mydir where mydir is a directory.
Quote:
ls -d */
@Chuck56: I like that solution - nice bit of thinking there.
 
Old 08-14-2010, 04:34 PM   #6
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 3,088

Original Poster
Rep: Reputation: 45
Well, thank you very much. I can see how bash expands * or */ by issuing 'ls */'. Adding the -d option prevents ls from entering the child directories. But given that in 'ls *' * expands in the same way, I can't see where is the difference. Anyways, the command works, and that's fine with me.
 
Old 08-14-2010, 04:49 PM   #7
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 3,088

Original Poster
Rep: Reputation: 45
Yes. Many times, when I wanted to see the file mode bits of a directory, I was forced to use the -d option. For example, with 'ls -l samples/' I only could see the contents of ./samples. But if I did 'ls -ld samples' then I could see the file mode bits of directory ./samples. The grep trick I also had used it, but I did not like to depend on the graphical output of the listings. The 'find' solution seemed good to me but if I could do it with 'ls' I would have more control over the listing format and information.

In M$-DOS it is very simple: DIR/AD. That's all. Of course, it isn't as powerful as ls is. Well, this is commentary, nothing else. Thanks to you both and regards.
 
Old 08-14-2010, 08:52 PM   #8
bgeddy
Senior Member
 
Registered: Sep 2006
Location: Liverpool - England
Distribution: slackware64 13.37 and -current, Dragonfly BSD
Posts: 1,810

Rep: Reputation: 227Reputation: 227Reputation: 227
Yes - to be honest I have often used "find -d" to show directories somewhere not knowing of Chuck56's clever solution - either using find or just paging through a ls directory listing looking for directories - Chuck56's method is really cool. One for me to store in my "linux tricks" book I think. Thanks again Chuck56.
 
Old 08-14-2010, 10:55 PM   #9
bnguyen
Member
 
Registered: Jul 2010
Distribution: Slackware
Posts: 125

Rep: Reputation: 32
Alternative solution

ls -l | grep ^d

include hidden dirs

ls -la | grep ^d
 
1 members found this post helpful.
  


Reply

Tags
find, ls


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Makefiles and subdirectories Jerry Mcguire Programming 11 03-16-2010 08:07 PM
listing subdirectories files ramesh_manu Red Hat 2 02-18-2007 01:46 PM
about subdirectories nesta Programming 4 02-08-2007 06:18 PM
Makefile and subdirectories pycoucou Programming 4 08-18-2005 04:43 AM
ls question: how to avoid listing contents of subdirectories learnfast Linux - Newbie 2 03-09-2005 08:59 AM


All times are GMT -5. The time now is 09:32 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration