Simplify Terminal directory listings

As someone who spends a fair bit of time in Terminal, I’m always looking for handy timesaving Unix tips. A great source of such information is commandlinefu.com, a site where users contribute their favorite tips. Much of what you’ll find there may not be directly applicable to OS X—as the site covers all versions of Unix and Linux—but a lot of it is.

After a command is submitted, other users can vote the submission up or down, based on how well they think it works. By focusing on the highly-rated submissions, you can quickly collect a good number of useful commands. To make this weeding-out process easier, commandlinefu.com has RSS and Twitter feeds for tips with a minimum of three “up” votes (Twitter, RSS) or ten “up” votes (Twitter, RSS). I use the 10-up Twitter feed, and it’s a great way to only see the really popular submissions.

Read more ...

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------@  13 robg  staff   442 Jun  5 09:23 Desktop
drwxr-xr-x@  56 robg  staff  1904 Jun  5 09:08 Documents
drwx------+ 128 robg  staff  4352 Jun  5 04:53 Downloads
drwx------+  73 robg  staff  2482 Jun  3 10:43 Library
drwx------+  13 robg  staff   442 Mar 26 16:23 Movies
drwx------+   8 robg  staff   272 Jun  1 14:04 Music
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 commandlinefu 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 (4.3K instead of 4352). The next two options work together to eliminate both the owner (robg) 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------@  13    442B Jun  5 09:29 Desktop
drwxr-xr-x@  56    1.9K Jun  5 09:28 Documents
drwx------+ 129    4.3K Jun  5 09:28 Downloads
drwx------+  73    2.4K Jun  3 10:43 Library
drwx------+  13    442B Mar 26 16:23 Movies
drwx------+   8    272B Jun  1 14:04 Music
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-

442B Jun  5 09:34 Desktop
1.9K Jun  5 09:28 Documents
4.3K Jun  5 09:28 Downloads
2.4K Jun  3 10:43 Library
442B Mar 26 16:23 Movies
272B Jun  1 14:04 Music
etc...

Now the output from ls shows only the most basic information for each entry: size in human-readable form, time of last modification, and filename. As this little hint shows, there are many ways to do things on the Unix side of OS X. Following the tips on commandlinefu.com—and our occasional Terminal hints here on Macworld—is a good way to get more familiar with the Unix side of your Mac.

Subscribe to the MacWeek Newsletter

Comments