Monday, May 28, 2007

Howto: Understand Output From ls

When you type in ls -la, you might get something like this:

~/stuff fergie$ ls -l
total 6584
-rw-r--r-- 1 fergie fergie 1252146 Jan 28 19:51 009.jpg
-rw-r--r-- 1 fergie fergie 1251616 Jan 28 19:50 Adam.jpg
-rw-r--r-- 1 fergie fergie 13587 Jan 30 20:04 BUN103-DXHZ.pdf
-rw-r--r-- 1 fergie fergie 141821 Apr 17 09:53 Ryanair_flight_19_04_2007.pdf
-rw-r--r-- 1 fergie wheel 114710 Feb 14 20:12 glasgow.pdf
drwxr-xr-x 73 fergie fergie 2482 Jan 28 19:40 linnea_jan_07
-rw-r--r-- 1 fergie fergie 583168 Jan 25 21:29 presentation.pps

So what does all that stuff actually mean? Well the format of ls -l output is as follows: file mode, number of links, owner name, group name, item size (in bytes by default), month of last modification, day of last modification, time of last modification, name. So for example the first entry can be interpreted as follows.
file mode:-rw-r--r--
number of links:1
owner name:fergie
group name:fergie
item size:1252146 bytes
month of last modificationJanuary
day of last modification28th
time of last modification19:51
name:009.jpg

Howto: Display Directory Contents Using ls

Use ls to display the contents of a directory:

~ fergie$ ls
Desktop Movies Pictures
Documents Music Public
Library Opera 7 Preferences.new Sites

Use the -l flag by typing ls -l to get more details on the files and directories:

~ fergie$ ls -l
total 0
drwx------ 26 fergie fergie 884 May 28 12:02 Desktop
drwx------ 4 fergie fergie 136 Feb 1 15:54 Documents
drwx------ 29 fergie fergie 986 Feb 1 16:19 Library
drwx------ 3 fergie fergie 102 Dec 20 18:20 Movies
drwx------ 3 fergie fergie 102 Dec 20 18:20 Music
-rw-r--r-- 1 root fergie 0 Feb 1 16:01 Opera 7 Preferences.new
drwx------ 185 fergie fergie 6290 Feb 27 15:35 Pictures
drwxr-xr-x 5 fergie fergie 170 Feb 27 10:42 Public
drwxr-xr-x 5 fergie fergie 170 Dec 20 18:20 Sites

Add the -a flag by typing ls -la to show 'hidden' items generally, but not exclusively, used to store configuration information. Hidden items are prefixed with a period- for example .Trash:

~ fergie$ ls -la
total 40
drwxr-xr-x 17 fergie fergie 578 Feb 6 15:45 .
drwxrwxr-t 7 root admin 238 Jan 31 17:21 ..
-rw-r--r-- 1 fergie fergie 3 Dec 20 18:20 .CFUserTextEncoding
-rw-r--r-- 1 fergie fergie 6148 Feb 27 17:30 .DS_Store
drwx------ 28 fergie fergie 952 May 24 17:09 .Trash
-rw------- 1 fergie fergie 1252 May 28 11:48 .bash_history
-rw-r--r-- 1 fergie fergie 13 Feb 6 15:45 .lpoptions
drwx------ 3 fergie fergie 102 Dec 20 20:18 .ssh
drwx------ 26 fergie fergie 884 May 28 12:02 Desktop
drwx------ 4 fergie fergie 136 Feb 1 15:54 Documents
drwx------ 29 fergie fergie 986 Feb 1 16:19 Library
drwx------ 3 fergie fergie 102 Dec 20 18:20 Movies
drwx------ 3 fergie fergie 102 Dec 20 18:20 Music
-rw-r--r-- 1 root fergie 0 Feb 1 16:01 Opera 7 Preferences.new
drwx------ 185 fergie fergie 6290 Feb 27 15:35 Pictures
drwxr-xr-x 5 fergie fergie 170 Feb 27 10:42 Public
drwxr-xr-x 5 fergie fergie 170 Dec 20 18:20 Sites

To order by time of most recent update add the -t flag:

~ fergie$ ls -lat
total 40
drwx------ 26 fergie fergie 884 May 28 12:02 Desktop
-rw------- 1 fergie fergie 1252 May 28 11:48 .bash_history
drwx------ 28 fergie fergie 952 May 24 17:09 .Trash
-rw-r--r-- 1 fergie fergie 6148 Feb 27 17:30 .DS_Store
drwx------ 185 fergie fergie 6290 Feb 27 15:35 Pictures
drwxr-xr-x 5 fergie fergie 170 Feb 27 10:42 Public
drwxr-xr-x 17 fergie fergie 578 Feb 6 15:45 .
-rw-r--r-- 1 fergie fergie 13 Feb 6 15:45 .lpoptions
drwx------ 29 fergie fergie 986 Feb 1 16:19 Library
-rw-r--r-- 1 root fergie 0 Feb 1 16:01 Opera 7 Preferences.new
drwx------ 4 fergie fergie 136 Feb 1 15:54 Documents
drwxrwxr-t 7 root admin 238 Jan 31 17:21 ..
drwx------ 3 fergie fergie 102 Dec 20 20:18 .ssh
-rw-r--r-- 1 fergie fergie 3 Dec 20 18:20 .CFUserTextEncoding
drwx------ 3 fergie fergie 102 Dec 20 18:20 Movies
drwx------ 3 fergie fergie 102 Dec 20 18:20 Music
drwxr-xr-x 5 fergie fergie 170 Dec 20 18:20 Sites

The -r flag reverses the order of any sorting, so adding it to the previous example would return a cronologically ordered list with the oldest item first:

~ fergie$ ls -latr
total 40
drwxr-xr-x 5 fergie fergie 170 Dec 20 18:20 Sites
drwx------ 3 fergie fergie 102 Dec 20 18:20 Music
drwx------ 3 fergie fergie 102 Dec 20 18:20 Movies
-rw-r--r-- 1 fergie fergie 3 Dec 20 18:20 .CFUserTextEncoding
drwx------ 3 fergie fergie 102 Dec 20 20:18 .ssh
drwxrwxr-t 7 root admin 238 Jan 31 17:21 ..
drwx------ 4 fergie fergie 136 Feb 1 15:54 Documents
-rw-r--r-- 1 root fergie 0 Feb 1 16:01 Opera 7 Preferences.new
drwx------ 29 fergie fergie 986 Feb 1 16:19 Library
-rw-r--r-- 1 fergie fergie 13 Feb 6 15:45 .lpoptions
drwxr-xr-x 17 fergie fergie 578 Feb 6 15:45 .
drwxr-xr-x 5 fergie fergie 170 Feb 27 10:42 Public
drwx------ 185 fergie fergie 6290 Feb 27 15:35 Pictures
-rw-r--r-- 1 fergie fergie 6148 Feb 27 17:30 .DS_Store
drwx------ 28 fergie fergie 952 May 24 17:09 .Trash
-rw------- 1 fergie fergie 1252 May 28 11:48 .bash_history
drwx------ 26 fergie fergie 884 May 28 12:02 Desktop

-S (capitalised) sorts by size:

~ fergie$ ls -laS
total 40
drwx------ 185 fergie fergie 6290 Feb 27 15:35 Pictures
-rw-r--r-- 1 fergie fergie 6148 Feb 27 17:30 .DS_Store
-rw------- 1 fergie fergie 1252 May 28 11:48 .bash_history
drwx------ 29 fergie fergie 986 Feb 1 16:19 Library
drwx------ 28 fergie fergie 952 May 24 17:09 .Trash
drwx------ 26 fergie fergie 884 May 28 12:02 Desktop
drwxr-xr-x 17 fergie fergie 578 Feb 6 15:45 .
drwxrwxr-t 7 root admin 238 Jan 31 17:21 ..
drwxr-xr-x 5 fergie fergie 170 Feb 27 10:42 Public
drwxr-xr-x 5 fergie fergie 170 Dec 20 18:20 Sites
drwx------ 4 fergie fergie 136 Feb 1 15:54 Documents
drwx------ 3 fergie fergie 102 Dec 20 20:18 .ssh
drwx------ 3 fergie fergie 102 Dec 20 18:20 Movies
drwx------ 3 fergie fergie 102 Dec 20 18:20 Music
-rw-r--r-- 1 fergie fergie 13 Feb 6 15:45 .lpoptions
-rw-r--r-- 1 fergie fergie 3 Dec 20 18:20 .CFUserTextEncoding
-rw-r--r-- 1 root fergie 0 Feb 1 16:01 Opera 7 Preferences.new

...and -h makes those sizes human readable:

~ fergie$ ls -laSh
total 40
drwx------ 185 fergie fergie 6K Feb 27 15:35 Pictures
-rw-r--r-- 1 fergie fergie 6K Feb 27 17:30 .DS_Store
-rw------- 1 fergie fergie 1K May 28 11:48 .bash_history
drwx------ 29 fergie fergie 986B Feb 1 16:19 Library
drwx------ 28 fergie fergie 952B May 24 17:09 .Trash
drwx------ 26 fergie fergie 884B May 28 12:02 Desktop
drwxr-xr-x 17 fergie fergie 578B Feb 6 15:45 .
drwxrwxr-t 7 root admin 238B Jan 31 17:21 ..
drwxr-xr-x 5 fergie fergie 170B Feb 27 10:42 Public
drwxr-xr-x 5 fergie fergie 170B Dec 20 18:20 Sites
drwx------ 4 fergie fergie 136B Feb 1 15:54 Documents
drwx------ 3 fergie fergie 102B Dec 20 20:18 .ssh
drwx------ 3 fergie fergie 102B Dec 20 18:20 Movies
drwx------ 3 fergie fergie 102B Dec 20 18:20 Music
-rw-r--r-- 1 fergie fergie 13B Feb 6 15:45 .lpoptions
-rw-r--r-- 1 fergie fergie 3B Dec 20 18:20 .CFUserTextEncoding
-rw-r--r-- 1 root fergie 0B Feb 1 16:01 Opera 7 Preferences.new


Technorati tags:, , ,

Howto: Move Around Using cd

Use the cd (change directory) command to navigate through directories.

Using the shell

The shell is the window where you type in commands. The unix shell is conceptually synonymous with the Windows 'command-line' although the commands are different. Most flavours of unix use bash, so this reference is intended to be bash-specific.

To find out what shell you are using type the following:

>echo $0

You could also type in:

>echo $SHELL

But this would only give you the default shell. If for some reason you had changed shell since logging in, echo $SHELL would not give you the current shell.

The terminal will now display a path which ends in either /sh, /csh, /tcsh, /ksh or /bash.

You can change shell by typing in 'sh', 'csh', 'tcsh', 'ksh' or 'bash'. Note that you are not strictly speaking changing shells but spawning new shells on top of the existing ones. To get back to the previous shell, use the 'exit' shortcut (ctrl-d) or type exit.

For the purposes of this manual echo $0 should return a path which ends in /bash, meaning that you are working in a bash shell. If for some reason you are not, type in bash which will invoke the bash shell.

Now we need to look at some commands!

What is Unix?

Unix is more widespread than you might think. Unix is a catch-all term that can describe a family of operating systems including Linux, BSD, Solaris, AIX and MacOSX (which is the operating system that runs on every Mac). In other words, these days, we can pretty much say that any desktop computer that is not running windows is running a form of unix. Clearly most desktop computers run windows, however most servers run Unix.