LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices



Reply
 
Search this Thread
Old 03-11-2004, 08:21 AM   #1
mikemrh9
Member
 
Registered: Nov 2003
Distribution: Arch
Posts: 136

Rep: Reputation: 21
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!...
 
Old 03-11-2004, 08:35 AM   #2
michaelk
Moderator
 
Registered: Aug 2002
Posts: 12,175

Rep: Reputation: 784Reputation: 784Reputation: 784Reputation: 784Reputation: 784Reputation: 784Reputation: 784
What is your current working directory? Are there any subdirectories in the current working directory?

Have you tried the command without the *
 
Old 03-11-2004, 08:43 AM   #3
LinuxLala
Senior Member
 
Registered: Aug 2003
Location: New Delhi, India
Distribution: Fedora 7
Posts: 1,305

Rep: Reputation: 45
Take a look at this thread

http://www.linuxquestions.org/questi...hreadid=141254
 
Old 03-11-2004, 08:51 AM   #4
mikemrh9
Member
 
Registered: Nov 2003
Distribution: Arch
Posts: 136

Original Poster
Rep: Reputation: 21
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 '.'
 
Old 03-11-2004, 09:00 AM   #5
mikemrh9
Member
 
Registered: Nov 2003
Distribution: Arch
Posts: 136

Original Poster
Rep: Reputation: 21
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!
 
1 members found this post helpful.
Old 03-12-2004, 12:53 AM   #6
LinuxLala
Senior Member
 
Registered: Aug 2003
Location: New Delhi, India
Distribution: Fedora 7
Posts: 1,305

Rep: Reputation: 45
Cool. Thanks for sharing with us all.

Cheers.
 
Old 03-12-2004, 10:13 AM   #7
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,314

Rep: Reputation: 175Reputation: 175
is this what you want?

Code:
    ls -d */
 
Old 03-12-2004, 10:20 AM   #8
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,314

Rep: Reputation: 175Reputation: 175
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
 
Old 03-12-2004, 11:06 AM   #9
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,314

Rep: Reputation: 175Reputation: 175
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
 
Old 03-17-2004, 05:11 AM   #10
mikemrh9
Member
 
Registered: Nov 2003
Distribution: Arch
Posts: 136

Original Poster
Rep: Reputation: 21
Spot on. Thanks, bigearsbilly.
 
Old 06-21-2011, 11:44 AM   #11
frenchn00b
Senior Member
 
Registered: Jun 2007
Location: E.U., Mountains :-)
Distribution: Debian, Etch, the greatest
Posts: 2,546

Rep: Reputation: 51
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


Last edited by frenchn00b; 06-21-2011 at 11:46 AM.
 
Old 07-16-2011, 01:42 PM   #12
maxpolaris
LQ Newbie
 
Registered: Jul 2011
Posts: 5
Blog Entries: 1

Rep: Reputation: Disabled
confused

Quote:
Originally Posted by bigearsbilly View Post
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]$
 
Old 07-16-2011, 02:21 PM   #13
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: CentOS
Posts: 1,624

Rep: Reputation: 677Reputation: 677Reputation: 677Reputation: 677Reputation: 677Reputation: 677
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 -- */
 
2 members found this post helpful.
Old 07-17-2011, 03:51 PM   #14
maxpolaris
LQ Newbie
 
Registered: Jul 2011
Posts: 5
Blog Entries: 1

Rep: Reputation: Disabled
Cool

Quote:
Originally Posted by rknichols View Post
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!
 
Old 07-17-2011, 06:50 PM   #15
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.6, Centos 5.10
Posts: 16,324

Rep: Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041
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/
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
CHMOD directories.sub-directories.files zerojosh Linux - Software 2 11-19-2005 04:22 PM
Searching multiple directories and sub directories for a file jeep99899 Linux - Newbie 2 10-13-2005 01:23 PM
W2K cannot view samba share but can view NT FiveFlat Linux - Software 3 06-10-2005 03:54 PM
How to view directories through putty of a server jai_linux Linux - Software 2 07-10-2004 03:04 PM
View more than 25 threads on the "view new posts" page lfslinux LQ Suggestions & Feedback 2 02-02-2002 01:07 PM


All times are GMT -5. The time now is 06:58 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration