LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   command to list directories only (https://www.linuxquestions.org/questions/linux-software-2/command-to-list-directories-only-141254/)

frieza 02-02-2004 12:51 AM

command to list directories only
 
i'm sure this has ben asked before, but in MSDOS there was a command that listed (for the most part) only the directories (dir *, as most nondirectory files had 3 letter extentions and just * displayed files without extentsions) is there any way to do this in linux, display just directories that is?

LinuxLala 02-02-2004 12:54 AM

Since Linux considers extensions as part of the file name, I doubt if this can be accomplished. So anything you do will result in listing all the files, and not just the directories.

snacky 02-02-2004 12:57 AM

Maybe not exactly what you're looking for, but try "ls -l |grep ^d"

Or "ls -F |grep \/$"

snacky 02-02-2004 01:01 AM

haha, this looks a lot like the old DOS thing:
ls -xd `ls -F |grep \/$`

Cerbere 02-02-2004 04:09 AM

Try the find command instead of ls. The following will list only subdirectories of the PWD:

find ./ -type d

This will list *ALL* subdirectories (and their subdirectories), so you may want to limit it with the following:

find ./ -type d -maxdepth 1

Take a look at the man page for find for more info.

Enjoy!
--- Cerbere

monster2control 04-29-2009 01:57 PM

A little AWK goes a long way
 
Code:

ls -l | grep ^d | awk '{print $9}'
This code will use the long format of ls then pipe it's through to grep looking only for files that have the d character in the start of the line.
Then using awk to print only the 9th column of data...

Results: Only the directories are listed.

If you want all the info too... remove the AWK pipe

guckpup 03-03-2011 12:18 PM

doesn't work for spaces
 
the last suggestion:
Quote:

Code:

ls -l | grep ^d | awk '{print $9}'

Doesn't take account of spaces in the directory name. Instead the more tortuous:

Code:

ls -l | grep ^d | awk '{for (i = 9; i <= NF; i++) printf("%s ",$i);printf("\n")}'
Will work.

(I prefer the find version myself :) )

supersugoi 05-10-2011 01:34 AM

Old thread!
 
Haha, this thread is so olden! Found this while crawling on google and decided to contribute.
You may use the following to list all dirs in the current dir (not recursive). It also outputs dir permissions so might be useful when permissions are relevant.

Code:

ls -lad */

-jG- 07-31-2012 04:12 AM

Hi,

lets have directory structure:
Code:

$ ls -a
.      ..    .test  test

and the listing:
Code:

$ ls -lad */ |grep test
drwxr-x---  2 user group            96 Jul 31 11:02 test/

nor
Code:

$ ls -ld */ |grep test
drwxr-x---  2 user group            96 Jul 31 11:02 test/

gives the .test directory. this behavior of ls is ok?

Code:

$ uname -a
HP-UX milton B.11.31 U ia64 2677351256 unlimited-user license

thanks,
jG

Wim Sturkenboom 07-31-2012 06:09 AM

'.test' is not a directory ?

// never mind

Yes it's normal

The reasoning is probably the following (not 100% sure):
The wildcard is substituted by bash. And bash does not include the the hidden files in the expansion (substitution).

David the H. 07-31-2012 12:32 PM

Well this is an old thread.

To expand on the previous post, yes, the dotfiles aren't being shown since it's the shell doing the expanding, and shell globbing ignores hidden files by default.

In fact because the shell is the program doing all the work, it's not even necessary to use ls at all (unless you want long-form information). A simple echo will do just as well.

As shown before, the way to glob-match directories is to add a slash after the glob pattern.
Code:

echo */
(I personally like using printf instead of echo, to allow printing each file on a separate line. The remaining examples will use that.)

Bash from version 4 also has a recursive globbing pattern.

Code:

printf '%s\n' **/
Note though that I've personally had problems with ** locking up the shell when using it certain directories. I still haven't figured out exactly what causes it, but I suspect it has something to do with recursive symlinks.


You can also recursively glob for files by adding additional globbing patterns after it.

Code:

printf '%s\n' **/*.txt                #lists all text files in the current directory and below

To print hidden files, simply enable the dotglob option first. To exclude certain entries from the list, you can add patterns that match them to the GLOBIGNORE shell variable (colon-separated), or with extended globbing patterns.

Code:

shopt -s dotglob
GLOBIGNORE=tmp/*:temp/*        #ignores the tmp/temp directories and all sub-entries

shopt -s extglob                #not enabled by default
printf '%s\n' **/!(*.txt)        #prints everything except text files.

See here for more on shell globbing.
globbing
extended globbing


In any case, for more complex matching, find is definitely the tool of choice. Be sure to use the -print0 null-terminator option if you need the list to be read by other commands.

http://mywiki.wooledge.org/UsingFind
http://www.grymoire.com/Unix/Find.html

MarxBro 09-15-2012 08:10 PM

I use this script quite often:
Code:

#!/usr/bin/perl
$|++;
foreach (<*>){
    print "$_\n" if -d;
} exit 0;

Place it on any path folder and:
Code:

chmod +x lsdir.pl
(i call it LSDIR.pl).

gk_2000 08-02-2013 07:36 PM

This worked for me

for f in `ls -R * | grep ":$" | tr ":" " "`
do
#do whatever with $f here, including ..
if [ -d $f ]
then chmod +x $f
fi
done

Habitual 08-02-2013 07:53 PM

DOS =
Code:

/dir /ad /b
Linux =
Code:

\ls -1d ~/*
Yes, I meant to include the \ as many have aliases that take precedence when using ls
Edit: That looks funny here, but it is a -"OneD"

frieza 08-03-2013 04:43 PM

rrow this thread is a question i posted eight years ago, thanks for the info but I'm gonna mark it as 'solved' so it gets a proper burial


All times are GMT -5. The time now is 07:54 PM.