Friday, November 20, 2009

The Unix ls Command

One of my all-time favorite
Unix commands is the ls command.

In its simplest form, ls looks
like this:

ls banana


The above lists a file named banana
if there is such a file.

Here's one that's more useful:

ls


This lists all the files in the current
directory.

OK. Here's something that could be even
more useful:

ls -l


This is the so-called long listing.
It lists eight columns of information on
every file in the current directory.

Included in the long listing is a time-stamp
for the file.

Note that a directory is just simply a
folder. Folders are directories and
directories are folders.

People who work on the command line
call them directories. People who
use a GUI (Graphical User Interface)
call them folders.

OK. Here's one that lists hidden files:

ls -A


The -A option means all.
That is to say, list all, both hidden
and non-hidden files.

Here's one that does a long listing on
all files, including hidden files:

ls -lA


Here's one that will look for subdirectories
inside of directories. Not only does it list
the current directory, it also lists anything
that belongs to the current directory:

ls -R


The -R suggests that the command is
recursive. It recursively descends into
directories finding sub-directories under
those directories going just as deep and
as far as it can.

Here's one that does a recursive long listing
on everything from the current directory on
down:

ls -lAR


Here's one that potentially looks at thousands
of files, listing those most recently modified
last:

ls -lAR | sort -k6


Use the above command to find a needle in a
haystack. The needle? A recently modified
file that is buried somewhere in listings of
thousands of files.

I love Unix commands because they are so simple.
Yet, you can do very complex things with them
when you start to string them together.

Ed Abbott

Thursday, November 19, 2009

The Unix File Command

Another favorite Unix command
that I frequently use is the
file command.

The file command is great
because it allows you to easily
determine what kind of file you
have in front of you.

Here's how you use the file
command:

file mystery-file


The file command tells you
what kind of file mystery-file
is.

Often, you can tell a file by extension.

For example, .jpg is a file extension.
The file, photo.jpg, is one such file.

To those in-the-know, any file with a
.jpg extension is an image or
photograph.

But what if you don't know? It's hard to keep
up with all possible file types out there.

If you don't know, the Unix file command
can be very very handy.

Ed Abbott

Thursday, November 12, 2009

The Unix Sort Utility

OK. This is a new blog.

Here's where I talk about some
of my favorite Unix commands under
Linux.

One of my favorites is the sort
utility. Often, I use this one
with a pipe.

Here's an example:

ls  -l  |  sort  -k6

This one sorts the output of a
long listing by date.

Therefore, if I'm only interested
in files that have been worked on
recently, I will find these at the
end of the listing.

Note that the -k option for
sort picks a whitespace
separated field as the one to do
the sort on.

That is to say, whitespace is what
separates potential sort keys.

Let's see. The long form of the
ls command is ls  -l.
ls  -l has 8 fields.

Therefore, ls  -l has 8
potential sort keys that you can
sort on.

A sort key is something to sort on.
A filename could be a sort key. A
date could be a sort key.

It is the sixth field that contains
the date. Therefore, -k6 means
sort on the sixth field which is the
date field.

Ok. Putting it all together, I'm taking
an ls command and sending
it to a sort command.

Putting the two commands, ls
and sort, together gives me
something greater than either command alone.

In many ways, that's what Unix is all about.
Putting commands together to make something
greater.

In this case, it is the pipe symbol, which lies
between the two commands, that allows me to put
them together to make something greater.

Make sense?

Here's a wonderful web page that describes in
detail many of the sort utility options:

More About Sort

Ed Abbott