The playful side of ls

Introduction

I use ls way to many times to count (2215 times in my 10k line ~/.histfile). And while I use this amazing application daily, I didn’t know the power lying in the depths of ls. A short visit to the manfile showed me the beautiful side behind ls. The little things that matter, but didn’t use because it was faster to whip up a quick awk script instead of reading the manpage.

ls && ls -la

First things first, lets show the side of ls that most of us use most:

I would say that ls and ls -la are the most commonly used variations of ls. One shows a simple directory structure, while the other shows executable permissions, ownership, filesize, modification date; all the necessary useful bits that you need to see.

Lets start with some visuals. If you didn’t notice, my listings are in color (I have ls –color as an alias to ls). But say I wanted to have directories stand out a little more, or symlinks:

According to the manpage there are many indicators (one of */=>@|), yet I’m unsure what the other indicators are? If I knew I would have included a few more examples.

Other than displaying little symbols to specify what the file is, there are a couple formatting options:

ls -m

commas (and a similar example with tr):

ls -1

single-column (and example with sed – since sed is a stream editor, no options will output results on a new line):

ls –group-directories-first

list folders first & combo list folders first with single-column layout:

ls -I

hide items while listing (–hide is ignored with -a|-A , yet -I|–ignore isn,t):

ls -lh

display with file sizes (will not size folders, only files):

ls -og

don’t show owner/user | both

ls -q

wrap names in quotes (great for scripts that need to follow directories)

ls -R

*list recursively (prints prettier than ls **

ls -t

sort by time (and formatting how time is shown (–time-style=+)

ls -v

sort by version (this is my favorite, no need for sort -V; this will sort by version syntax

ls -X

sort by extension (alpha order – great for grouping extensions)

Conclusion:

I hope that something new sprouted into your brain pertaining to ls. It seems that the program was written and rewritten to provide a full featured directory listing utility without the need to break out any stream editors like sed or awk. In fact a lot of the simple flags that encompass what is ls I would emulate with piping commands together just because I didn’t think that ls had useful options. Well now I know the power of ls, and learned that I should check out the flags pertaining to even the simplest of programs to find gems of programming prowess.


Related posts

Published

17 May 2012

Tags


blog comments powered by Disqus