Changing the behaviour of ls, possibly via a script

Tim Chase blinux.list at thechases.com
Fri Aug 13 21:40:54 UTC 2004


Lorenzo,

Sed and/or awk are great tools for this sort of thing, and you 
don't have to know much about them to get this to work 
(especially as I'm handing them to you on a cut-n-paste platter 
[grins]).  The following 2-line scriptlet should do the trick:

#!/bin/sh
ls -l $@ |awk '/^/{printf "%-20s%s %s %s\n", $9 $10 $11 $12, $1, 
$3, $4;}'

Just paste it into something like "myls" in your path and "chmod 
u+x myls" the file to make it executable.  You can adjust the 
width of the first column (currently set to 20) by changing that 
number in the script.  It currently handles up to 3 spaces in 
your file name, assuming there aren't more than one consecutive 
space.

Alternatively, if you prefer to use Sed, you can do the same sort 
of thing with this two-liner

#!/bin/sh
ls -l $@ |sed 
"s/\(\S*\)\s*\S*\s*\(\S*\s*\S*\)\s*\S*\s*\S*\s*\S*\s*\S*\s*\(.*\)/\3\t\1\2/g"

The sed version is a bit more convoluted, but handles filenames 
with an arbitrary number of spaces.  However, it doesn't nicely 
columnize things.  If you're reading linearly, it shouldn't make 
a bit of difference.  If, however, you're scanning by column, the 
awk version may be more useful as long as you don't have lots of 
arbitrary spaces in your file names.

Both should allow you to pass parameters as well, so you can 
filter the filespec down to just what you want.

Best wishes,

-tim

PS:  those are both two-liners...the first line is the 
shell-specifier, and the second line is all one line, no matter 
how badly Mozilla-mail bungles it and tries to make it wrap.

PPS:  You may also have to hard-code the path to "/bin/ls" in 
there, as some distros attempt to alias "ls" to their colorized 
versions, and funky stuff can happen if you use that.  The sed 
version should handle that pretty well, but the awk version does 
funny stuff.








More information about the Blinux-list mailing list