[comp.unix.questions] Can ls be made to only list directories?

SML108@psuvm.psu.edu (Scott the Great) (09/01/90)

As the subject asks, is there a set of options which will limit
ls to listing directories?

Scott Le Grand aka sml10*@psuvm.psu.edu

rouben@math9.math.umbc.edu (Rouben Rostamian) (09/01/90)

In article <90243.151817SML108@psuvm.psu.edu> SML108@psuvm.psu.edu (Scott the Great) writes:
>As the subject asks, is there a set of options which will limit
>ls to listing directories?
>

I use the following on an ultrix machine:
alias lsd '/bin/ls -aF | grep /$ | xargs /bin/ls -Cd'

It may require minor modifications to run on other machines.

--

Rouben Rostamian                               Telephone: (301) 455-2458
Department of Mathematics and Statistics       e-mail:
University of Maryland Baltimore County        rostamian@umbc.bitnet
Baltimore, MD 21228,  U.S.A.                   rostamian@umbc3.umbc.edu

merlyn@iwarp.intel.com (Randal Schwartz) (09/01/90)

In article <90243.151817SML108@psuvm.psu.edu>, SML108@psuvm (Scott the Great) writes:
| As the subject asks, is there a set of options which will limit
| ls to listing directories?

That's a bit ambiguous.  I'll try to answer all the variations...

(1) that's *all* it does anyway (taken literally). :-)  Does yours
send mail too? :-) :-)

(2) if you mean "list the directories by name, rather than listing the
contents of those directories", use the "-d" switch, as in:

	ls -d a*

lists all names in the current directory that begin with "a", regardless
of whether or not it's a directory (rather than listing the content
of the directories).

(3) if you mean "list *only* the directory names, and not their contents",
you can do that with echo (much faster), as in:

	echo a*/.

names all the directories in the current directory that start with "a".

(4) if you mean "list *only* directories along with their contents",
try a combination, as in:

	ls a*/.

lists all the directories in the current directory that start with "a",
along with their contents.

===

Hopefully, your meaning was in here somewhere.  If not, send me email.

Just another Unix hacker,
-- 
/=Randal L. Schwartz, Stonehenge Consulting Services (503)777-0095 ==========\
| on contract to Intel's iWarp project, Beaverton, Oregon, USA, Sol III      |
| merlyn@iwarp.intel.com ...!any-MX-mailer-like-uunet!iwarp.intel.com!merlyn |
\=Cute Quote: "Welcome to Portland, Oregon, home of the California Raisins!"=/

gwyn@smoke.BRL.MIL (Doug Gwyn) (09/02/90)

In article <90243.151817SML108@psuvm.psu.edu> SML108@psuvm.psu.edu (Scott the Great) writes:
>As the subject asks, is there a set of options which will limit
>ls to listing directories?

	$ ls -ld `find . -type d -print`	# for example

hunt@dg-rtp.dg.com (Greg Hunt) (09/02/90)

In article <90243.151817SML108@psuvm.psu.edu>, SML108@psuvm.psu.edu
(Scott the Great) writes:
> As the subject asks, is there a set of options which will limit
> ls to listing directories?
> 
> Scott Le Grand aka sml10*@psuvm.psu.edu
> 

I don't think there is a set of switches to ls that will limit it to
only printing directories.  The quickest thing I could come up with 
was this:

    ls -ld * | awk '/^d/{print $0}'

This runs the output of ls through awk, printing only those lines that
are for directories.  You need the 'ls -l' to get the long listing
which puts a d at the beginning of the line for directories (which awk
then searches for).  You need the 'ls -d' to get ls to list only the
directory itself and not its contents.

If you want to get just the names output instead of the ls -l line,
change the $0 to $9.

Enjoy!

--
Greg Hunt                        Internet: hunt@dg-rtp.dg.com
DG/UX Kernel Development         UUCP:     {world}!mcnc!rti!dg-rtp!hunt
Data General Corporation
Research Triangle Park, NC       These opinions are mine, not DG's.

jak@sactoh0.SAC.CA.US (Jay A. Konigsberg) (09/02/90)

>In article <90243.151817SML108@psuvm.psu.edu> SML108@psuvm.psu.edu (Scott the Great) writes:
>As the subject asks, is there a set of options which will limit
>ls to listing directories?

A simple solution is to pipe the output of 'ls -l' through
'grep' and anchor the leading "d" in "drwxrwxrwx" to the
begining of the string.

ls -l | grep "^d"

-- 
-------------------------------------------------------------
Jay @ SAC-UNIX, Sacramento, Ca.   UUCP=...pacbell!sactoh0!jak
If something is worth doing, its worth doing correctly.

ag@cbmvax.commodore.com (Keith Gabryelski) (09/03/90)

In article <13725@smoke.BRL.MIL> gwyn@smoke.BRL.MIL (Doug Gwyn) writes:
>In article <90243.151817SML108@psuvm.psu.edu> SML108@psuvm.psu.edu (Scott the Great) writes:
>>As the subject asks, is there a set of options which will limit
>>ls to listing directories?
>
>	$ ls -ld `find . -type d -print`	# for example

Which will list all subdirectories also; which is probably not what
Scott wanted.

	$ ls -ld `echo */.`

is probably more in tune to what was requested.  CAVEAT: directories
starting with `.' are not displayed.  Modify as needed.

Pax, Keith

merlyn@iwarp.intel.com (Randal Schwartz) (09/03/90)

In article <14171@cbmvax.commodore.com>, ag@cbmvax (Keith Gabryelski) writes:
| Which will list all subdirectories also; which is probably not what
| Scott wanted.
| 
| 	$ ls -ld `echo */.`
| 
| is probably more in tune to what was requested.  CAVEAT: directories
| starting with `.' are not displayed.  Modify as needed.

A tip from someone who knows launching processes is expensive:

You *never* need

	`echo something`

Just use

	something

Think about it.  And that'd make your command line pretty close to mine
(at least *one* of mine :-), as in:

	$ ls -ld */.

Just another UNIX hacker,
-- 
/=Randal L. Schwartz, Stonehenge Consulting Services (503)777-0095 ==========\
| on contract to Intel's iWarp project, Beaverton, Oregon, USA, Sol III      |
| merlyn@iwarp.intel.com ...!any-MX-mailer-like-uunet!iwarp.intel.com!merlyn |
\=Cute Quote: "Welcome to Portland, Oregon, home of the California Raisins!"=/

brnstnd@kramden.acf.nyu.edu (Dan Bernstein) (09/03/90)

In article <1990Sep3.032037.487@iwarp.intel.com> merlyn@iwarp.intel.com (Randal Schwartz) writes:
> A tip from someone who knows launching processes is expensive:
> You *never* need
> 	`echo something`
> Just use
> 	something

Not always. When echo is built in and the shell is sensible, `echo
something` is hardly an efficiency loss, and it's sometimes useful for
playing with the spacing. (Consider csh's word breaks.)

---Dan

shwake@raysnec.UUCP (Ray Shwake) (09/05/90)

Create a shell script - call it dirs - then run "dirs *"

for i in `file $* | fgrep directory | sed 's/\([^:]*\):.*/\1/'`
do
	/bin/echo "$i \c"	# feel free to modify for built-in echo
done
echo

One can then run, for example:

	ls `dirs [a-z]*`

Variants are possible for other file types (like text, for example).
Of course, there are a few LS variants around supporting the -D option.
In which case, (well, you can finish this one yourself!)

gt0178a@prism.gatech.EDU (BURNS,JIM) (09/05/90)

in article <10914:Sep306:06:3490@kramden.acf.nyu.edu>, brnstnd@kramden.acf.nyu.edu (Dan Bernstein) says:
> In article <1990Sep3.032037.487@iwarp.intel.com> merlyn@iwarp.intel.com (Randal Schwartz) writes:
>> You *never* need >> 	`echo something`

} Not always. When echo is built in and the shell is sensible, `echo
} something` is hardly an efficiency loss, and it's sometimes useful for
} playing with the spacing. (Consider csh's word breaks.)

I thought so at first too, but don't the backquotes launch a process?
-- 
BURNS,JIM
Georgia Institute of Technology, Box 30178, Atlanta Georgia, 30332
uucp:	  ...!{decvax,hplabs,ncar,purdue,rutgers}!gatech!prism!gt0178a
Internet: gt0178a@prism.gatech.edu

brnstnd@kramden.acf.nyu.edu (Dan Bernstein) (09/06/90)

In article <13343@hydra.gatech.EDU> gt0178a@prism.gatech.EDU (BURNS,JIM) writes:
> in article <10914:Sep306:06:3490@kramden.acf.nyu.edu>, brnstnd@kramden.acf.nyu.edu (Dan Bernstein) says:
> > In article <1990Sep3.032037.487@iwarp.intel.com> merlyn@iwarp.intel.com (Randal Schwartz) writes:
     [ `echo foo`  is always better expressed as just  foo ]
> > Not always. When echo is built in and the shell is sensible, `echo
> > something` is hardly an efficiency loss, and it's sometimes useful for
> > playing with the spacing. (Consider csh's word breaks.)
> I thought so at first too, but don't the backquotes launch a process?

Backquotes don't have to fork any more than output redirection does.

---Dan

jak@sactoh0.SAC.CA.US (Jay A. Konigsberg) (09/06/90)

Come on people, this really is trivial! The basics are _simple_.
Just do `ls -l | grep "^d"`. However, here is a little shell script
I wrote a longgg time ago for splunking around the system.

You don't need awk, sed, ed or anything else so tough. If a script
can be done simple way, *do it in a simple way*.
------
# sdir - Get a sub-directory listing of your current directory
# Jay 9-15-88

(ls -l | grep "^d" || echo "There are no sub-directories")|pg

NOTE: The || works as an 'if' stmt for the times there aren't any
      subdirectories and the parens are there to make sure the
      output goes through `pg'. 

      The program lived for about 2 years as `ls -l | grep "^d"`
      before getting upgraded and that was only done when I got
      in the mood to hack something :-)


-- 
-------------------------------------------------------------
Jay @ SAC-UNIX, Sacramento, Ca.   UUCP=...pacbell!sactoh0!jak
If something is worth doing, its worth doing correctly.

ajm@icc.com (Al Marmora) (09/06/90)

In article <1990Sep3.032037.487@iwarp.intel.com> merlyn@iwarp.intel.com (Randal Schwartz) writes:
>
>You *never* need
      ^^^^^
>
>	`echo something`
>
>Just use
>
>	something
>
>Think about it.

I did;  in a Bourne Shell try:
	echo hello > foobar ; cat < foo*
instead of
	echo hello > foobar ; cat < `echo foo*`

--al

staff@cadlab.sublink.ORG (Alex Martelli) (09/06/90)

shwake@raysnec.UUCP (Ray Shwake) writes:

>Create a shell script - call it dirs - then run "dirs *"

>for i in `file $* | fgrep directory | sed 's/\([^:]*\):.*/\1/'`
>do
>	/bin/echo "$i \c"	# feel free to modify for built-in echo
>done
>echo

Just a little observation - 's/:.*//' seems a tad simpler than the
sed command suggested (I use it all the time for similar purposes,
e.g. "file * | fgrep text | sed 's/:.*//' | xargs ls -l", interactively,
to long-list text-files only).  Either will give problems on filenames 
with embedded colons, but my suggestion's a bit easier to fix - just
place a tab between the colon and the dot.

-- 
Alex Martelli - CAD.LAB s.p.a., v. Stalingrado 45, Bologna, Italia
Email: (work:) staff@cadlab.sublink.org, (home:) alex@am.sublink.org
Phone: (work:) ++39 (51) 371099, (home:) ++39 (51) 250434; 
Fax: ++39 (51) 366964 (work only; any time of day or night).

anagram@desire.wright.edu ((For Mongo)) (09/07/90)

In article <3899@sactoh0.SAC.CA.US>, jak@sactoh0.SAC.CA.US (Jay A. Konigsberg) writes:
> Come on people, this really is trivial! The basics are _simple_.
> Just do `ls -l | grep "^d"`. However, here is a little shell script
> I wrote a longgg time ago for splunking around the system.

You think that's really trivial?  I replied to the original question, and from
his response, I take it that my suggestion worked for him.  On the machine I
use, Utek SysV (BSD), there is a flag (-X) that lists just directories.

That's trivial!

Anagram