LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   ls to view directories only (https://www.linuxquestions.org/questions/linux-newbie-8/ls-to-view-directories-only-156254/)

mikemrh9 03-11-2004 07:21 AM

ls to view directories only
 
Hi. I realise that the answer to this question is probably in the man page, but I seem to be incapable of finding it!

I would like to use the 'ls' command to display directories only. (Equivalent of MSDOS 'dir /ad')

I've tried:

ls -d *
ls --directory *
ls -ldR *

together with various combinations of the above. Nothing I have tried seems to give me what I want, and I am starting to get a little frustrated. Please put me out of my misery!...

michaelk 03-11-2004 07:35 AM

What is your current working directory? Are there any subdirectories in the current working directory?

Have you tried the command without the *

LinuxLala 03-11-2004 07:43 AM

Take a look at this thread

http://www.linuxquestions.org/questi...hreadid=141254

mikemrh9 03-11-2004 07:51 AM

Thanks for replying. I'm in my home directory, and I do have sub-directories. I've tried with and without the '*'.

The only results I seem to be able to get are either a complete listing including subdirectories and files, or just the current directory, '.'

ls -d
ls -dR
ls -dr
ls --directory -R
ls --directory -r
ls --directory

all just return '.'

mikemrh9 03-11-2004 08:00 AM

Thanks, LinuxLaLa. I appear to have been mixing up directories with directory entries...

For anybody else who may be interested,

tree -d

gives the same results. A bit simpler than all that 'grep' malarky!

LinuxLala 03-11-2004 11:53 PM

Cool. Thanks for sharing with us all.

Cheers.

bigearsbilly 03-12-2004 09:13 AM

is this what you want?

Code:

    ls -d */

bigearsbilly 03-12-2004 09:20 AM

Code:


billym.primadtpdev>ls -d */
apps/            include/          outlook/          tkcvs-7_03/
backup/          info/            paste/            tmp/
bash/            ipedo/            Pix/              vim-5.8/
bin/              lib/              PRIMA_HOME/      W/
CVS/              logs/            RCS/              w/
desktop/          Mail/            repository.old2/  wrox/
DEVSERVER/        man/              share/            XML/
doc/              NSCPcom/          sql/
etc/              nsmail/          temp/
GNUstep/          opt/              templates/
billym.primadtpdev>^C


bigearsbilly 03-12-2004 10:06 AM

you can put loads of "*/" together like so to go deeper:

Code:

>    ls -1d  ileaf/*/*/

ileaf/billy-lib/Binary_Files.cab/
ileaf/billy-lib/CVS/
ileaf/billy-lib/Nicky_Reports.cab/
ileaf/billy-lib/batch-report/
ileaf/billy-lib/generate-report/
ileaf/billy-lib/iconid.cab/
ileaf/billy-lib/new-releasedoc.cab/
ileaf/billy-lib/xmlprima-html-preview.cab/
ileaf/debugging_scripts.cab/CVS/
ileaf/java-recall.cab/CVS/
ileaf/lisp-exercises/CVS/

it's quite cool really, dontcha fink?



billy

mikemrh9 03-17-2004 04:11 AM

Spot on. Thanks, bigearsbilly.

frenchn00b 06-21-2011 10:44 AM

The problem is that is gives the full path of the folder with ls -d1

:(


Quote:

ls -d Documents/* | while read i ; do basename "$i" ; done
gives both files and directories

:(

maxpolaris 07-16-2011 12:42 PM

confused
 
Quote:

Originally Posted by bigearsbilly (Post 811430)
Code:


billym.primadtpdev>ls -d */
apps/            include/          outlook/          tkcvs-7_03/
backup/          info/            paste/            tmp/
bash/            ipedo/            Pix/              vim-5.8/
bin/              lib/              PRIMA_HOME/      W/
CVS/              logs/            RCS/              w/
desktop/          Mail/            repository.old2/  wrox/
DEVSERVER/        man/              share/            XML/
doc/              NSCPcom/          sql/
etc/              nsmail/          temp/
GNUstep/          opt/              templates/
billym.primadtpdev>^C


I wonder why it does not work inb my Music or MP3 directories?

[norm@localhost ~]$ ls -d */
bin// Dropbox// Movies// playground// tmp//
Desktop// FAH6.02-Linux// MP3// pyNeighborhood// Videos//
Documents// Linux Cookbook - allfreeebook.tk// Music// smb4k// work//
Downloads// lmms// Pictures// Templates//

then
cd MP3

[norm@localhost MP3]$ ls -d */
ls: invalid option -- '='
Try `ls --help' for more information.
[norm@localhost MP3]$

rknichols 07-16-2011 01:21 PM

Keep in mind that it's really your shell that is expanding that "*/" to a list of directories that is then seen by ls. What is probably happening is that your MP3 directory has a subdirectory with a name beginning with '-', and ls tries to process that entire name as a set of flag arguments. Try this:
Code:

ls -d -- */

maxpolaris 07-17-2011 02:51 PM

Quote:

Originally Posted by rknichols (Post 4416685)
Keep in mind that it's really your shell that is expanding that "*/" to a list of directories that is then seen by ls. What is probably happening is that your MP3 directory has a subdirectory with a name beginning with '-', and ls tries to process that entire name as a set of flag arguments. Try this:
Code:

ls -d -- */

Gee that worked just swell! Thanx!
The offending directory is appropriate -----> -=A3-Sopranos Soundtrack=-//
You're absolutely right!

Do I understand you to mean

[norm@linux-2 ~]$ ls -l -a # aaaah! I see how the -=A3Sopranos would get expanded by bash
total 10492
drwx------ 79 norm norm 4096 Jul 17 14:06 ./


ls -d */ shell expands */ to all lines ending in / then passes it all to ls -d -=A3Sopranos

BUT -=A3Sopranos is not the first entry so ls must be checking to see if it can list EVERYTHING without errors before ANYTHING is sent to stdout ie display. Is that right?

HOWTO Train Brain to expand like Bash!

chrism01 07-17-2011 05:50 PM

As rknichols said, when you type that cmd, bash is expanding the wildcard, then passing to ls.
The only way to think like bash is practice... ;)
Try these for ref
http://rute.2038bug.com/index.html.gz
http://tldp.org/LDP/Bash-Beginners-G...tml/index.html
http://www.tldp.org/LDP/abs/html/

David the H. 07-18-2011 04:09 AM

That's right, the directory list isn't being generated by ls, but by shell globbing. ls is only reading the list to give you detailed info.

http://mywiki.wooledge.org/glob

For a simple list, you can even do this:
Code:

echo */

rknichols 07-18-2011 03:44 PM

Quote:

Originally Posted by maxpolaris (Post 4417510)
BUT -=A3Sopranos is not the first entry so ls must be checking to see if it can list EVERYTHING without errors before ANYTHING is sent to stdout ie display. Is that right?

Different commands process their option arguments in different ways. Common variants are:
  1. Flag options must appear at the beginning of the argument list. Once the first non-flag argument or explicit "--" argument is encountered, everything that follows will be treated as an ordinary (non-flag) argument even if it happens to begin with a '-' character. This is the old-style Unix method of option processing.
  2. Flag options can occur anywhere in the argument list until a "--" argument explicitly terminates option processing, and all options apply globally. This implies that the entire argument list is scanned for flag arguments before any non-flag arguments are processed.
  3. Flag arguments can be interspersed with non-flag arguments (again, subject to "--" terminating option processing), and each option applies only to the arguments that follow it in the list. (Example: the tar "--directory=xxx" option)

It appears that the ls command is in the second category.

drmopwater 09-28-2011 02:18 PM

It seems everyone is doing it the hard way. The simplest way to list a directory and only the folders, without the full path is:

ls -F <whatever path you want> | grep /

For example

ls -F ./ | grep /

will list the local directories

ls -F Documents/ | grep /

will list all of the directories in the folder Documents

Hope this helps!

bigearsbilly 10-04-2011 04:41 AM

the simplest way is:

Code:

ls -d */

RockDoctor 10-04-2011 05:34 PM

To also show hidden directories, I prefer:
Code:

ls -al | grep ^d

PTrenholme 10-04-2011 06:16 PM

If you're not stuck on using ls, the command find . -maxdepth 3 -type d would list all directories under ./, for no more than three levels. Here's an example:
Code:

$ cd Scripts/
$ find ./ -maxdepth 2 -type d
./
./dash
./InitRD
./gawk
./gawk/igawk_backup
./gawk/Gutenburg
./tmp
./bash
./bash/Xfce
./bash/Mounting
./bash/Housekeeping
./bash/Network
./bash/bashsimplecurses-0.0.1-alpha
./bash/Testing
./SQLite
./Floppy
./Older Scripts
./Init
...

<edit>
Or you could use this:
Code:

$ tree -dL 2
.
├── bash
│** ├── bashsimplecurses-0.0.1-alpha
│** ├── Housekeeping
│** ├── Mounting
│** ├── Network
│** ├── Testing
│** └── Xfce
├── dash
├── Floppy
├── gawk
│** ├── Gutenburg
│** └── igawk_backup
├── Init
├── InitRD
├── Older Scripts
├── SQLite
└── tmp

17 directories


ktmariappan 02-08-2012 01:45 AM

command to list directories only
 
Hi,

Please try below command. it should be listed the directories only.
ls -l|grep "^d"

mlavannis 04-20-2012 12:59 PM

ls -dl `find /location/of/your/dir -type d`

Thanks to Marius Ducea
http://www.ducea.com/2009/06/05/linu...nd-full-paths/

David the H. 04-21-2012 10:42 AM

Quote:

Originally Posted by mlavannis (Post 4658562)
ls -dl `find /location/of/your/dir -type d`

Sorry, but not only is this command unnecessarily complicated, it also doesn't work. It will break on any directory containing whitespace, thanks to the shell performing word-splitting after the command is expanded.

($(..) is highly recommended over `..`, BTW.)

Code:

$ mkdir 'aa bb'

$ ls -dl $( find . -type d )
ls: cannot access ./aa: No such file or directory
ls: cannot access bb: No such file or directory

If you want to run ls on the output of find, use its -exec option instead.

Code:

$ find . -type d -exec ls -dl '{}' \+
drwxr-xr-x 3 david david 72 Apr 22 00:33 .
drwxrwxr-x 2 david david 48 Apr 22 00:33 ./aa bb

Or if you're using gnu find, which you should be on Linux, you can use it's own built-in -ls option to get pretty much the same output.

Code:

$ find . -type d -ls
 80205    0 drwxr-xr-x  3 david    david          72 Apr 22 00:33 .
313973    0 drwxrwxr-x  2 david    david          48 Apr 22 00:33 ./aa\ bb


doc1623 07-08-2015 01:02 PM

for ZSH ;)
 
alias lsd="ls -d \.#*(/)"


with setopt extendedglob

Habitual 07-08-2015 01:26 PM

Just the DIRs please.
 
Code:

ls -lF

astrogeek 07-08-2015 01:27 PM

'Nother necro...


All times are GMT -5. The time now is 01:38 PM.