LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Slackware (https://www.linuxquestions.org/questions/slackware-14/)
-   -   Listing only subdirectories. (https://www.linuxquestions.org/questions/slackware-14/listing-only-subdirectories-826255/)

stf92 08-14-2010 03:46 PM

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.

Chuck56 08-14-2010 04:04 PM

I might misunderstand your question but have you looked at the man page?

man ls

Try:

ls -d *

stf92 08-14-2010 04:09 PM

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.

Chuck56 08-14-2010 04:19 PM

ls -d */

or

ls | grep /

bgeddy 08-14-2010 04:32 PM

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.

stf92 08-14-2010 04:34 PM

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.

stf92 08-14-2010 04:49 PM

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.

bgeddy 08-14-2010 08:52 PM

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.

bnguyen 08-14-2010 10:55 PM

Alternative solution

ls -l | grep ^d

include hidden dirs

ls -la | grep ^d


All times are GMT -5. The time now is 04:46 PM.