making the most of “ls”

ls” is the age-old command to list files and directories in Unix.
Over the years it has permuted and grown very functional.
At the moment, I typically use two “forks in the road” versions of ls.

The first is the “ls” that comes pre-installed in Mac OS X.
It’s the BSD version.
The second is GNU “ls“, which I install on Macs using
Darwin ports.
Ironically enough, the term “ports” there comes from BSD.

No what I want out of “ls” is the following:

  1. use colour to hilight files types (*.jpg/gif/png/bmp vs *.mp3/m4a/m4b/ogg vs *.tgz/zip/rar)
  2. show file sizes in MB (not k, not 512-byte blocks)

So this is how you do this.

Using BSD “ls“, we would set the BLOCKSIZE to 1048576 and set the environment variable CLICOLOR (not to any particular value, just ensure it exists; under csh I do “setenv CLICOLOR”).

Using GNU “ls“, we need the flags “–block-size=1MB” and “–color”.

Now when you use “ports”, GNU ls shows up as gls, so you also need to alias “ls” to be “gls“.

There, now isn’t that fun?
Throw in to the mix some favorite other flags as well as some combinations and permutations of
listing only files, only directories, and maybe all the source code around here,
and you get the bottom of my .cshrc file
(the equivalent .profile is left as an exercise for the Bash user):

if($?prompt) then
	# make ls a nice, happy thing that:
	#	(a) uses color to show different file types
	#	(b) shows file sizes in megabytes
	#CLICOLOR - makes BSD ls always list in color
	setenv CLICOLOR
	#BLOCKSIZE - used by BSD ls to determine how to print the size of files
	setenv BLOCKSIZE		1048576
	setenv LS_OPTIONS		-AsCF
	setenv LS_COLOURS		'di=1;34:ln=1;36:ex=1;35'
	if("$term" == "xterm") set term=xterm-color
	# start off using the usual 'ls' in /bin
	alias base_ls /bin/ls
	# try to use gls, if it's available
	if ( "`which gls`" !~ *'not found'* ) then
		# GNU ls is better than BSD ls in general
		alias base_ls gls
		setenv LS_OPTIONS		"-AsCF --block-size=1MB --color"
	alias ls		base_ls ${LS_OPTIONS}
	alias ll		'base_ls -al'
	alias ls		'base_ls ${LS_OPTIONS}'
	alias ls.		"find . \!* -name '.[a-z]*' -maxdepth 1 -print0 | perl -p0 -e 's#^\./##;' | xargs -0 base_ls -da ${LS_OPTIONS}"
	alias lsd		"find . \!* -type d \! -name '.*' -maxdepth 1 -print0 | perl -p0 -e 's#^\./##;' | xargs -0 base_ls -d ${LS_OPTIONS}"
	alias lsf		"find . \!* -type f \! -name '.*' -maxdepth 1 -print0 | perl -p0 -e 's#^\./##;' | xargs -0 base_ls -d ${LS_OPTIONS}"
	alias lsfa		"find . \!* -type f               -maxdepth 1 -print0 | perl -p0 -e 's#^\./##;' | xargs -0 base_ls -Ad ${LS_OPTIONS}"
	alias lss		'base_ls -a ${LS_OPTIONS} \!* {,*/,*/*/}*.{h,cc,c,cpp,cxx,hxx,hpp,m,mm,py,rb,sh}'
	alias zappaths	rm ~/.cshpaths

We won’t even mantion that I sometimes run across versions of find that do not grok “-maxdepth”..

2 Responses to “making the most of “ls””

  1. wolf Says:

    Learn more about Perl from


  2. Lhasa Tour Says:

    That Sounds interesting, I agree with you.Please keep at your good work, I would come back often.*

Leave a Reply