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/


All times are GMT -5. The time now is 10:54 AM.