LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 02-02-2004, 12:51 AM   #1
frieza
Senior Member
 
Registered: Feb 2002
Location: harvard, il
Distribution: Ubuntu 11.4,DD-WRT micro plus ssh,lfs-6.6,Fedora 15,Fedora 16
Posts: 3,233

Rep: Reputation: 406Reputation: 406Reputation: 406Reputation: 406Reputation: 406
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?
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 02-02-2004, 12:54 AM   #2
LinuxLala
Senior Member
 
Registered: Aug 2003
Location: New Delhi, India
Distribution: Fedora 7
Posts: 1,305

Rep: Reputation: 45
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.
 
Old 02-02-2004, 12:57 AM   #3
snacky
Member
 
Registered: Feb 2004
Distribution: Debian
Posts: 286

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

Or "ls -F |grep \/$"
 
Old 02-02-2004, 01:01 AM   #4
snacky
Member
 
Registered: Feb 2004
Distribution: Debian
Posts: 286

Rep: Reputation: 30
haha, this looks a lot like the old DOS thing:
ls -xd `ls -F |grep \/$`
 
Old 02-02-2004, 04:09 AM   #5
Cerbere
Member
 
Registered: Dec 2002
Location: California
Distribution: Slackware & LFS
Posts: 799

Rep: Reputation: 33
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
 
2 members found this post helpful.
Old 04-29-2009, 01:57 PM   #6
monster2control
LQ Newbie
 
Registered: Apr 2009
Posts: 1

Rep: Reputation: 1
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
 
1 members found this post helpful.
Old 03-03-2011, 12:18 PM   #7
guckpup
LQ Newbie
 
Registered: Mar 2011
Posts: 1

Rep: Reputation: 2
Exclamation 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 )
 
2 members found this post helpful.
Old 05-10-2011, 01:34 AM   #8
supersugoi
LQ Newbie
 
Registered: May 2011
Posts: 1

Rep: Reputation: 0
Smile 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 */
 
0 members found this post helpful.
Old 07-31-2012, 04:12 AM   #9
-jG-
LQ Newbie
 
Registered: Jul 2012
Posts: 1

Rep: Reputation: Disabled
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
 
Old 07-31-2012, 06:09 AM   #10
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Ubuntu 12.04, Antix19.3
Posts: 3,794

Rep: Reputation: 282Reputation: 282Reputation: 282
'.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).

Last edited by Wim Sturkenboom; 07-31-2012 at 06:49 AM.
 
Old 07-31-2012, 12:32 PM   #11
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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
 
Old 09-15-2012, 08:10 PM   #12
MarxBro
LQ Newbie
 
Registered: Apr 2011
Posts: 2

Rep: Reputation: 0
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).
 
Old 08-02-2013, 07:36 PM   #13
gk_2000
LQ Newbie
 
Registered: Oct 2010
Posts: 2

Rep: Reputation: 0
Lightbulb

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
 
Old 08-02-2013, 07:53 PM   #14
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Blog Entries: 37

Rep: Reputation: Disabled
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"
 
Old 08-03-2013, 04:43 PM   #15
frieza
Senior Member
 
Registered: Feb 2002
Location: harvard, il
Distribution: Ubuntu 11.4,DD-WRT micro plus ssh,lfs-6.6,Fedora 15,Fedora 16
Posts: 3,233

Original Poster
Rep: Reputation: 406Reputation: 406Reputation: 406Reputation: 406Reputation: 406
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
 
  


Reply



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
How to just list directories using ls command shazam75 Linux - Newbie 10 01-26-2006 02:34 PM
list sub-directories only CowboyJ Linux - Newbie 1 12-04-2003 11:42 PM
Asking LS for a list of directories only gary knott Linux - Newbie 1 11-12-2003 05:49 PM
list of directories greg108 Linux - Newbie 5 08-08-2003 03:11 AM
How may I list only the directories in command line? geraldomanaus Linux - Newbie 5 03-20-2003 12:40 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

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

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
Open Source Consulting | Domain Registration