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/)

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 05:51 AM.