        Some Linux/Unix tips for Newbies
        =========================
by M. Andreoli
Translated by: Mimmo Simeone (sime1brt@topvideo.net)
Reviewed by: Peter Tapolyai (peter@uhu.com)

INDEX
------

    o The "wild-cards"
    o Strange behaviors using "wild-cards"
    o Devices
    o How to duplicate a floppy
    o Coping "parts" from a source to a destination
    o Breaking in two parts a file and coping it on two floppies
    o Coping and deleting files
    o Using "pipes": tar and gzip
    o Making experiments on RAM disks
    o Making experiments on "natcat": file transfer
    o Playing with tty terminals
    o A "dd" exercise: what kind of file is this?
    o File System inside a file: "loop devices"
    
The wild-cards
--------------
Wild-cards like * can be directly passed on to executable commands as
parameter list (in C: argv[1], argv[2], ....)

Example:

If a directory contains the files { more, mount, mv, od }, a 
command like "ls *" gets expanded in:

    # ls more mount mv od

Strange behaviors using "wild-cards"
------------------------------------
While in the same directory I give the command:

    # cp *

this is interpreted like:

    # cp more mount mv od

with the result that those files {more mount mv} are appended to the file
"od", and maybe this is not what you wanted to do!

For executing arithmetical operations in UNIX the "expr" command is available.

        expr x op y

In the same directory, a command like:

    # expr 10 * 2

is expanded in:
    
    # expr 10 more mount mv od 2

resulting in an error.

To clarify these snags, the UNIX special characters have to be "protected" by
interpretations (like the Bible?); example:

    # expr '10 * 2' 
    # expr 10 \* 2

The single quotes (e.g. 'something') are not used for protecting, but to 
"gather" as parentheses do in mathematics.

Devices
-------
There are two kinds of devices: "char devices" (modem, mouse, etc.) or
"blocks devices" (CD, harddisk, etc.) that are accessed in sequential 
or random order.

It's possible to access a device by special files placed in the /dev directory.
Examples:

        /dev/hd*    hd=hard-disk
        /dev/tty*   tty=char devices
        /dev/ttyS*  Serial ports (mouse, modem)
        /dev/fd*    fd=floppy disk
        /dev/sd*    sd=SCSI device
        

The fd0H1722, for example, refers to the first (0) floppy, (H) high
density, formatted disk with 1722Kbytes.

Since "files" and "devices" can be used nearly everywhere with similar
commands, in this document the two names will be used interchangeably.

"Block" devices on which there is a "file system" can be "mounted" in a
"mounting point" and so they become part of the directories's tree.

For example, with the command:

        # mount /dev/fd0H1722 /a

a floppy disk can be accessed simply with "cd /a".


How to duplicate a floppy disk. (DOS's "diskcopy")
--------------------------------------------

With UNIX, you can copy the entire content of a device inside a file, with a
command like:

    # cat /dev/fd0H1440 > myfloppy

So, insert the destination floppy and type:

    # cat myfloppy > /dev/fd0H1440


Coping "parts" from a source to a destination
---------------------------------------------

For this kind of activity is useful command is the  "dd" command. 
With "dd" you are able to copy blocks of data from a place to the other, 
to control the amount of "goods" to transport and to give the 
exact point where copy will begin.

Example 1:

    # dd if=/dev/fd0 of=myfile skip=200 bs=1k count=2

This command copies a floppy on "myfile", using 1K blocks (bs=block size), but
just 2 blocks (count=2) and jumping first 200 ones (skip=200).


Example 2:

    # dd if=/dev/ttyS0

Copy "to the screen" (in the stdout) anything coming from COM1, until you
stop it by pressing CTRL-C.

Fitting a large file on two floppies
----------------------------------------------------------
You can do this, for exercise, using "dd":

    # dd if=myfile bs=1k count=1440 of=part.1
    # dd if=myfile bs=1k skip=1440 of=part.2
    # cat part.1 > /dev/fd0H1440 { cambia il floppy}
    # cat part.2 > /dev/fd0H1440

To put the two "parts" together from floppy, type:

    # cat part.1 part.2 > myfile
    # rm part.1 part.2

Look at my /usr/bin/mkfloppy script.


Copying and deleting files: cp and rm commands
---------------------------------------------
"cp" command knows that the last paramenter given is destination. Recursive
copy is activated by -r.

The "rm" command works the same way. Adding  -v  will give you a list of files on which you are operating on.

Examples:

    # cp -vr /bin /sbin /tmp
    
    Recursive copying of /bin and /sbin dirs in /tmp.

    # cp -vr /bin/* /tmp

    in this case in /tmp won't create the subdir /tmp/bin.

    # rm -r /

    Without comment.

Using "pipes": tar and gzip
---------------------------

Commands often used like "tar" (for blending files and directories) and
"gzip" (standard GNU compressor) can receively/produce "goods" directly from
"standard input/output", and so they work very good via "pipe".  That is:
without the need to create "temporary" files. Usually in LINUX omitting the file
name the operation  is expecting the input from stdin/stdout..
In some cases, we can refer to the "standard input" with the "-" symbol
(like tar does), or with other kind of switches (like "-c" in gzip).

Example 1: making a TGZ file

    # tar -cf- /usr/doc | gzip -c > doc.tgz

    In this case TAR creates ("-c") an archive of all the sub-tree=20
    /usr/doc and copies it in the stdout ("f-"), where it will be taken=20
    by gzip, compressed in the stdout ("-c") and anything will be out=20
    in the doc.tgz file


Example 2: decompressing a TGZ file

    # gzip -dc doc.tgz | (cd /some/dir ; tar -xf- )

    This is the opposite of the above operation. GZIP decompress=20
    doc.tgz file ("-d"); copies it to the stduot ("-c"), where TAR
    takes it ("f-"), expands it ("-x") in single files of witch it's=20
    made. Files are created starting from directory specified with -C
    (in this case /)

For your information, TAR gets the "-z" switch for decompress files itself
(with using GZIP).


Making experiments on RAM disks
--------------------------------

We can make experiment on a file-system (creating, mounting, coping, etc)
without having a real partitions on our hard disk.
For making this we can use "RAM disks", accessing by "/dev/ram0, /dev/ram1,
etc" files.
By the way, don't use the first 4 devices, because they are already used by
muLinux, and don't even make the RAM disk bigger than 4Mb.

Example 1:

    # mkdir /ram            { creates /ram mounting point }
    # mkfs.ext2 /dev/ram5 1440  { creates a 1,44M fs EXT2 }
    # mount /dev/ram5 /ram      { mounts it in /ram}

Example 2:

    # mkdir /ram            { creates /ram mounting point }
    # cat /dev/fd0H1440 > /dev/ram5 { copies a DOS-floppy in RAM }
    # mount -t dos /dev/ram5 /ram   { mounts in /ram the obtained fs}


In the example 2, fs has not been created yet, just copied from the first
floppy (io out case, a DOS fs).


Making experiments on "netcat": file transfer
----------------------------------------------

The tiny program "nc" (see nc -h) is the "base" for most muLinux TCI/IP
activity. I think it is wonderful little program. I want to describe how it would be
possible with "nc" to make a "file transfer" between two UNIX boxes on LAN.
Obviously, if you haven't installed muLinux on a LAN with al least two PCs,
you can skip this section.

-- client & server
nc can be used to experiment with the client/server philosophy by programming a
UNIX Shell, instead of C:

Here's how to activate a server (-l =3D listening) that listens on port 5555.

    host-A> nc -l -p 5555

Here's instead how to activate a client on host-B on the same port:

    host-B> nc A 5555

With this sample procedure, A and B are now connected through port 5555.
Typing something on the keyboard, on both sides, chars are transferred
from one computer to the other (in UNIX this is called TALK).

With tricks like these is how the web server PYGMY is implemented in muLinux and other applications such as: telnet, sendmail and so on.


-- file transfer

If we want to transfer a file using this technique, stdin/stdout can do this for you.

In the next example, host-A archives a part of its file system, compresses
it with GZIP and sends it through the port. Host-B takes it from its port it is listening on,
decompresses it and recreates it on its file system. The directory transferred is /usr/doc.

    host-A> tar -cf- /usr/doc | gzip -9c | nc -l -p 5555
    host-B> nc A 5555 | gzip -dc | tar -C/ -xvf-=20

As we can see, dir /usr/doc in transferred from A to B between the net, in
compressed format. At the end of the operation on B we have created the
sub-tree /usr/doc!

For this case of "TAR" and "GZIP" see the precedent section.

If on host-B you have a Windows client with a browser like Netscape, you
could use the same trick transferring like this:

    Give the browser the following URL:

        http://A:5555/

File would appear directly in browser window. All you have to do is save it
with a name. I haven't tried it, but it should work..


Playing with tty terminals
---------------------------

Every open console is referred by a device called /dev/tty*.
For example: the first one is /dev/tty1, the second one /dev/tty2, and so  on.
The current terminal is referred to as: /dev/tty.

It is possible to send data from one of the open consoles in a simple way.
Example:

    # echo hello > /dev/tty3
    # date > /dev/tty3
    # cat /etc/profile > /dev/tty5

If instead we want to access in reading mode from a console, we can use
/dev/vcs* devices, one for every tty*.

If, for example, we want to see what is been typed on tty1m all we have to
do is type:
    # cat /dev/vcs1


A "dd" exercise: what kind of file is this?
-------------------------------------------

Let's suppose that we have an X file with no exact suffix that indicates the
type of that file; how do we know what kind of data this file contains? In the 
UNIX world this task is done by "file" command". For example, if i type:

        # file DOSTOOLS.zip

I obtain:

     "Zip archive data, at least v2.0 to extract"

"file" command extracts some bytes and it compares the binary value with the
"magic values" of the data base (/etc/magic).

For example: a compressed pkzip file has the first two bytes set as
"PK", while a ".tar" file has 5 bytes added "ustar", starting from the 257th 
byte.

This way, if I type:

        dd if=X.zip bs=1c count=2

I get "PK", and if I type:  

        dd if=X.tar bs=1c skip=257 count=5

the result is: "ustar".
Searching in the /etc/magic directory (but in muLinux this file is not available) we can
do lots of other discoveries like these. It's just this way I made the "rustic" version of muLinux "file"

File System inside a file: "loop devices"
-----------------------------------------

The "mkfs.ext2" command can create an EXT2 file system (Linux native) in a
file, too, instead of creating a partition.
To do that, we need to create a file of any length (for example 300K) full
of "0" (zeros) with this command:

        # dd if=/dev/zero of=my_fs bs=1k count=300

Than, we can create fs:

        # mkfs.ext2 -F my_fs

For accessing to any fs in UNIX, we have to "mount" it. This is done by:

        # mount -o loop my_fs /mnt

Note the "-o loop" option that tells the kernel to use these particular
"mount" features.
Type "df" if you want to see the new fs mounted and its available space etc.

Now we can go on /mnt, create files, dirs, copy datas upon them just like
if it was a normal "hard device".
At the end, we can umount it by typing:

        # umount /mnt

and store file "my_fs" somewhere

The entire muLinux file system could be created this way and indeed
some floppy-Linux are made this way.


