[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: Unix 101: ls with file count



* Kent West (westk@acu.edu) [020625 05:51]:
> Here's my alias, which I believe works like I want it to:
> 
> 
> alias dolsa='ls -la --color | more ; echo -------------------; echo `ls 
> -la | grep ^d | wc -l` Directories; echo `ls -la | grep ^- | wc -l` 
> "File(s)" ; echo `ls -la | grep ^l | wc -l` "Symbolic Link(s)"; echo 1 
> Byte Count ; echo `ls -la | wc -l` Total Items'
> 
> Thanks, everyone!

Glad you got that working! But it's a bit hairy, and would be a headache
to change, if the need ever arose. How about this, instead: make a
script called ~/bin/dolsa that looks like this:

#!/bin/sh

ls -la --color | more
echo -------------------
echo `ls -la | grep ^d | wc -l` Directories
echo `ls -la | grep ^- | wc -l` "File(s)"
echo `ls -la | grep ^l | wc -l` "Symbolic Link(s)"
echo 1 Byte Count
echo `ls -la | wc -l` Total Items

<EOF>

Then 'chmod a+x ~/bin/dolsa' and ensure that ~/bin/ is in your path, by
uncommenting the appropriate section in your ~/.bash_profile .

The above is equivalent to your alias, but much easier to read, no?

You could also optimize it (though I'm sure it runs fast enough that the
time is trivial) by storing the output of ls -la instead of calling it 3
times and by using grep -c instead of piping to wc -l . You could also
wrap the whole thing in $( ) | more instead of just piping the ls output
to more. Better yet, use less. Better still, use
${PAGER:-/usr/bin/pager} .

But don't stress out about it. These suggestions are the type that a
software engineer sees but that really don't make much difference to
the user. If you got it working, that's what counts! 

good times,
Vineet
-- 
http://www.doorstop.net/
-- 
"Computer Science is no more about computers
than astronomy is about telescopes." -E.W. Dijkstra

Attachment: pgptrnwmI79Ja.pgp
Description: PGP signature


Reply to: