Steve Thompson

He who speaks the truth often talks to himself.

Simplify Terminal Directory Listings

First of all, I have to give credit for this entry to Rob Griffiths from Macworld. It was he who was responsible for getting me to write this blog entry.

Today’s 10-up Tweet featured a simple improvement to the ls -al command. This command lists all files in long format, and the output is quite verbose:
$ ls -l
total 0
drwx------@ 4 Steve staff 136 Jun 9 17:38 Desktop
drwxr-xr-x@ 222 Steve staff 7548 Jun 9 17:45 Documents
drwx------@ 4 Steve staff 136 Jun 9 07:43 Downloads
drwx------@ 51 Steve staff 1734 Jun 9 09:04 Library
drwx------@ 8 Steve staff 272 May 14 19:04 Movies
drwx------@ 5 Steve staff 170 Apr 30 06:58 Music
drwx------@ 9 Steve staff 306 May 16 20:38 Pictures
drwxr-xr-x@ 6 Steve staff 204 May 29 17:31 Public
drwxr-xr-x@ 4 Steve staff 136 Jun 7 18:06 Sites
drwx------ 3 Steve staff 102 May 20 03:06 StuffIt
etc...

But if you’ve got files with lengthy names, this output can get quite wide—and the thing you may be most interested in, the filename, will be a good distance off to the right. The command line suggestion is to use ls -hog instead. This “piggie” version of ls uses three modifiers to simplify the output. The h modifier converts file sizes to human-readable form (7.4K instead of 7548). The next two options work together to eliminate both the owner (Steve) and the group (staff) from the output. When you use this version of the command, the output is much cleaner:
$ ls -hog
total 0
drwx------@ 4 136B Jun 9 17:38 Desktop
drwxr-xr-x@ 222 7.4K Jun 9 17:45 Documents
drwx------@ 4 136B Jun 9 07:43 Downloads
drwx------@ 51 1.7K Jun 9 09:04 Library
drwx------@ 8 272B May 14 19:04 Movies
drwx------@ 5 170B Apr 30 06:58 Music
drwx------@ 9 306B May 16 20:38 Pictures
drwxr-xr-x@ 6 204B May 29 17:31 Public
drwxr-xr-x@ 4 136B Jun 7 18:06 Sites
drwx------ 3 102B May 20 03:06 StuffIt
etc...

Now there’s only a bit of added information at the front—the permissions on the file, and the number of hard links (ln) to that file. If you’d rather not see those columns, you can send the output of ls to the cut command, which can trim columns from the output prior to display. In this case, the file size information starts at the 20th character, so the command (and its output) look like this:
$ ls -hog | cut -c 20-

136B Jun 9 17:38 Desktop
7.4K Jun 9 17:45 Documents
136B Jun 9 07:43 Downloads
1.7K Jun 9 09:04 Library
272B May 14 19:04 Movies
170B Apr 30 06:58 Music
306B May 16 20:38 Pictures
204B May 29 17:31 Public
136B Jun 7 18:06 Sites
102B May 20 03:06 StuffIt
etc...