Monday, May 4, 2009

AUC #2: ls

auc2AUC 2 - Arbitrary Unix Command #2
I thought it might be useful to cover some obscure unix commands that you may find useful in your day to day system administration. My primary work machine is a Macbook Pro these days, so I'll lean towards cover commands that come with OS X. Most unix operating systems will likely have the commands I cover.

Today I wanted to talk about a command that we use every day: ls. Yes, this isn't a very arbitrary pick, but I decided sharing useful commands to be more important than obscure.

If you aren't a regular unix user, the command 'ls' is equivalent to the Windows 'dir' command when using cmd.exe.

uac2-a

It is so frequently used that nearly 10% of my bash history contains 'ls'. When considering the unix mantra that every command should do only one thing, but do it well, ls is surprisingly robust.

auc2-b

While you may be quite familiar with this command, I always recommend checking the man page for a unix command as command switches may differ depending on your system (ie: gnu commands) and more importantly you may learn something new and useful. You can do so by typing:
man ls

auc2-c

This should look familiar. The -l switch produces a vertical list (just like dir /p in cmd.exe.) The -a switch includes hidden files, which are typically preceded by a period. I won't go into detail on how to read the permissions as the internet has many such guides.

auc2-d

Lets say you are interested in only file names of a particular directory. The -1 switch will declutter the output quite nicely and only spit out a list of filenames and not any of the meta information such as file permissions, file ownership, size, and last file modify time.

auc2-e

Ok. So what? Perhaps you are interested in performing a command against every file (not directory) in your current working directory. The first of the two commands above produces a single line of output, space delimited list of filenames. (I have see Internet postings frowning upon the use of xargs to convert newlines into spaces as I have, but still need to discover a better way.)

The second command builds upon the first by embedding it in backticks (usually shared with the tilde key) in use with a for loop. The command string inside the backticks is executed first and produces a space seperated list for the x variable in the for loop to iterate through. The result is a copy and paste-able start for a bash script. I've used the head command here as a placeholder example.

auc2-f

Another useful application of ls is to produce a full, recursive directory listing.

auc2-g

Saving the best for last is my favorite ls command. The -t switch sorts by modify time, newest at top and the -r switch reverses the order. This is the perfect way to quickly find your recent files. It's such a time saver, I rarely use ls -al anymore in favor of the above.

Until next time!

No comments:

Post a Comment