Spare Time Labs 2.0

Welcome


EazyCNC


jDraft 2.0




PureJavaComm


PIC CDC ACM


Weather


Ten-Buck Furnace



H8S Bootloader



Camera Calibration



Multitouch



Myford VFD


Fun with HC08


bl08


printf II


Java Goes Native



Densitometer


printf


jApp


Igloo


New Furnace


New Furnace
Part II


Linux 101


H8S/gcc


Quickie


Gas Fired Furnace


Down Memory Lane


Exlibris


Wheel Patterns


Glitches


CHIP-8


eDice


Animato


jDraft


JNA Benchmark


Contact Info
25.10.2004 / Kusti

Linux 101 (for Dummies)

This is a very brief introduction to Linux that I wrote to amuse myself.

(Actually this is notes to self so I do not need to look this up from the web every time I need it!)

Teletype and Other Arcane Stuff

Traditionally Unix based system are based on command line interface used over a terminal connection. Even if it all runs in a single PC. This is very powerfull but for those born into GUI enviroments it can be a bit intimidating and it has some features that are based on ancient computer history. Things that nobody bothers to explain.

Incidentally, this is part of University 101 - Computer Usage, worth one credit. If you know 1/10th of what's on this page, you'll pass. I did.

Back in the good old days, when I was in my early teens, computers were used via a terminal called Teletype. It was basically an electric typewriter where you punched keys to enter commands into the computer and the computer hammered (literally) it's output to roll paper. What is still significant from this old interaction model is that computer output was and still is static. So if you list the contents of a directory , and then delete a file, the listing stays the same, unless you take another listing. Not like a modern GUI window which tries to be WYSIWYG. (Ok, Windows does a bad job at this but just keep pressing that F5/refresh button...)

An other rudiment is that because output and input at 110 bps was slow and NOISY (try shouting RAT-TAT-TAT-TAT to get a feel of it) you wanted to minimize the number of characters used, hence most Unix descendants have very terse command names and output formats.(Yes, 110 bps is 110 bits/second or about 10 characters/second or to give you some perspective my WLAN is 500 000 times faster! Those were the days, and yet its only a quarter of a centry ago.)

So the main interaction model in Linux is a terminal session using a 'glass teletype'. You type in commands and watch the outpour of characters on the terminal. A session begins when you log into a Linux system and ends when you log off. It is usually possible to open multiple sessions by launching the terminal window multiple times. This can be very usufull as it is like working on multiple computers at the same time.

Of Files and Directories

All large systems, which today means any system, have thousands of files. On this laptop that I'm writing this on I have about 100.000 files. So obviously you want to organize them some how so that you can find anything at all. So the files are organized into directories that can contain further directories and/or files. This is called hierarchical file system. (Pity that the QDOS that Bill bought back in the old days and that later became known as MS-DOS did not have a file hierarchy, not to mention long file names. If he had bought something deacent, like OS-9, boy would we all have been saved from a lot of headache.)

Unlike Windows, Linux uses no drive letters, such as 'C:' to refer to disks. In Linux everything is a directory. The top most directory is equivalent to the drive letter. In Cygwin, which pretends to be a Linux but runs on Windows, the windows 'C:' disk is refered to as directory '/cygdrive/c'. And the root of Cygwin directory tree looking from Windows side is (usually) 'C:/cygwin', under which you find 'home', 'bin', 'etc' ...

File and directory names can contain all kinds of letters but it is best to use names that only use english lowercase letters and digits plus maybe dashes and underlines. Especially you should avoid names with spaces (' ') in them. They will make your life miserable. Names that begin with period '.' are not usually visible in directory listings which is why the many operating system tools store configuration data into files beginning with '.'. Or actually it is the other way round, because tools store info into those files they are hidden.

Names are case sensitive in Linux whereas in Windows they are not. This can be very confusing if you are testing something, say a web page, in Windows and it works but does not work when you upload it to the server which is most likely a Linux box.

Each session has current directory assosiated with it. What this means in Finglish is that if you do not specify (in a command) otherwise it is assumed that you are refering to the current directory. By having multiple sessions it is easy to work in multiple directories because in effect every terminal window has its own current directory.

Here are the most common commands that are used to work with (current) directories.


 pwd                Show the directory you are currently in
 cd dirname         Change current directory to 'dirname' directory 
 cd ..              Move to (make current) the parent of your current directory
 cd ~               Move to your home directory
 mkdir dirname      Make a new directory named 'dirname'
 rmdir dirname      Remove a directory named 'dirname'  Note:  All files must first be deleted

Paths

Files and directories are referd to by path names.

There are relative and absolute path names. A relative pathname begins with a letter and it refers to a file or directory that is in the current directory or a subdirectory of current directory. An absolute path name begins with a slash and specifies the full 'path' from the highest level of the file system to the file/directory in question.

The current directory can be referenced as '.' and the parent directory of the current directory as './..' and so on.

Every user, based on their username, has a special directory called home directory. Immediatelly after login current director is set to your home directory. The home directory can be referenced as '~' in a path.

The asterisk ('*') can be used as a wild card in most situations when specifying files on a command line.

There are conventions in Linux world on how to organize the file hierarchy. There is nothing to enforce these conventions but most system are organized along these lines.

Here are some often used directories:


 /bin               for binary (executables),
 /dev               for device files,
 /etc               for admin and personal information, 
 /tmp               for temporary files, and
 /home              for home directories of individual users 

Working with Files

Here are the most common commands that you need to work with files.

 ls                  List the contents of the current directory
 ls -l               Give a long-form listing, with lots of information about each file and directory
 ls -a               Do a normal ls, but include all files whose names begin with a period (hidden files)
 ls -F               Do a normal ls, but mark all executable files with a *, and all directories with a /
 ls -laF             Do all of the above
 ls -R1              List all files in current directory and all sub directories, each file on its own line
 
 mv oldname newname  Move (or, more precisely, rename) the file oldname to the file newname.
 mv file dirname     Move a file into directory named dirname.

 cp filename newname Copy the file filename to the file newname.
 cp filename dirname Copy a file into a directory.

 rm file             Remove (that is, delete) a file.  There's no "undelete" command, so be careful.

 cat file            "Concatenate" the file--that is, print its entire contents to the screen, all at once.  
                     For big files, use more file.

Getting Help

Most commands list a short description of their usage and possible argumenst i.e. parameters when given the parameter --help. For example:

ls --help
You can also access a lot of documention for each command with the man command. This lists the manual pages for a given command page by page. Press 'space' to get to the next page, press 'q' to quit reading. For example:

man ls

Commands, Scripts and Shells

In Linux all commands are actually small programs that you run. You run them by typing their names to the command interpreter which the in-crowd calls 'shell'. There are quite a few shells around but most resemble each other. In Cygwin the shell (which is just another program) is called 'bash'.

In DOS/Windows you can store commands into batch files that end with the name extension '.BAT' which allows you to execute those commands by typing the file name. In effect creating more commands out of existing commands. This is often called scripting and the the files that contain commands are called , you guessed it, scripts.

In Linux any text file is a potential script. When you type a file name the shell checks if the file is marked executable and if it is it will execute the commands in that file. A nice example of this is the way GNU tools are configured. You usually configure them by going to the directory that contains the source code and type

configure something
The 'configure' is actually a script that contains shell commands to do the configuration. While I'm on the subject: the 'configure' script usually creates a Makefile, which is used to build the tools. More on that later.

To make a file executable (for everybody) you use the chmod command, like this:

chmod a+x filename
Instead of 'x' you could add (with '+') or remove (with '-') read ('r') or write ('w') access to a given file.

To see if a file is executable and other stuff use:

ls -l filename

Pipes and more (or less)

Most Linux commands read in some text or parameters and then produce and output some text. It is possible to redirect both the input and output of a program. You do that with '<inputfilename' for input and with '>outputfilename' for output. For example, if you want to create a list of all files in your current directory and store it into a text file use:
ls >mydirlistfile.txt
It also possible to direct the output of one program to the input of another. This is called piping and is effected with '|'. Very often used with 'more' command, (or maybe with 'less' command as 'more' does not seem to be available in Cygwin) , which is a command that just outputs what it inputs, but does it page by page so that you have a chance to read it. For example if you invoke the 'ls' command recursively to see all the files in your current directory or any subdirectory you will be creating a looooooooong listing, which will whirl by at speed of light. To cut it to readable chunks use:
ls -R | less
One of the most common uses for piping is to use it to feed input to grep command.

Using Grep as a Filter

Lots of people love grep, but I'm not one of them. Everytime I need it I need to look it up from the web. So I frequently don't use it, hence I don't learn and need look it up everytime I need it...

Simply (very simply) put grep grep reads the input and passes through lines that match the criteria you give to the grep command.

Anyway, here are some usefull grep's :

grep ".java"     Pass through every line that contains the string ".java"
grep ".java\|.c"     Pass through every line that contains either the string ".java" or ".c"
grep -v ".class"     Pass through every line that does NOT contain either the string ".java"

Following is a way to list all '*.java' files and the directories in which they are starting recursively from current directory:

ls -R1 | grep -e ".java\|/"

Finding (in) files

To find a file containing a certain word use:

grep -n void example.h
Finds word 'void' in file 'example.h'
grep -n 'int x' *.h
Finds fragment 'int x' in all '.*' files in current directory
grep -Rn 'func' *.h
Finds word 'func' in allfiles in current directory or any sub directory
grep -Rn 'b*lean' filename
Finds word 'b*lean' where '*' stands for anything

Counter intuitively and unfortunatelly the following DOES NOT work:

grep -Rn void *.h

If you want search all files of certain type in current directory and sub directories you need to use:

find . -name "*.c" -exec grep -l -n 'this' {} \;

Finds 'this' in any '*.c' file.

Following gibberish executes wc< (word count) command on each '*.java' file in current directory and any subdirectory and stores the results to a text file 'wc.txt'. Handly for collecting statistics of you lates software project.

find . -name "*.java" -exec wc {} \; >wc.txt 

Unpacking Files

A lot of things in the Linux world are distributed as tar (aka tarball) files which are compressed with gzip or gzip2.
tar xfj collection1.bz2     Uncompresses and then extract all the files from the arcieve 'collection1'
tar xfz collection1.tar.gz  Uncompresses and then extract all the files from the arcieve 'collection2'

Most Linux/Free software is delivered in source code format.

makeing Programs

This is a way too complex subject, but amazingly a lot of GNU based Free software actually can be installed and compiled by going to top level directory of the source distribution and entering following:

./configure
make     
make install  
Note that many packages can take hours to compile! If you do not see any output, that is a good sign, in the best tradition of "no news is good news" most Linux tool produce no output when things are going smoothly!

Compiling Software

This is even more complex subject but a simple hello word program such as

#include <stdio.h>

int
main (void)
{
  printf ("Hello, world!\n");
  return 0;
}
can be compiled with:

cc hello.c -o hello

and executed (run) with:

./hello

thats all folks,
cheers Kusti

Miscallenious

To create a check an md5sum index of files (to facilitate dumplicate file finding):


find DIRECTORY -type f -exec md5sum "{}" \; | sort >/tmp/index


find . -name "*.java" -exec grep -v '^[[:space:]]*$' $ {} \; | wc

Current Directory is NOT in the Patch

So you write your script and want to test it. You remember to give it execution rights with the chmod command and confidently type myscript. No such luck, you use the ls command to check that it is there, so what the heck? The thing is that current directory is not in the path. You need to type ./myscript to execute your script.