Next Previous Contents

4. Files and Programs

4.1 Files: Preliminary Notions

Linux has a structure of directories and files very similar to that of DOS/Win. Files have filenames that obey special rules, are stored in directories, some are executable, and among these most have command switches. Moreover, you can use wildcard characters, redirection, and piping. There are only a few minor differences:

  • under DOS, file names are in the so-called 8.3 form; e.g. NOTENOUG.TXT. Under Linux we can do better. If you installed Linux using a file system like ext2 or umsdos, you can use longer filenames (up to 255 characters), and with more than one dot: for example, This_is.a.VERY_long.filename. Please note that I used both upper and lower case characters: in fact...
  • upper and lower case characters in file names or commands are different. Therefore, FILENAME.tar.gz and filename.tar.gz are two different files. ls is a command, LS is a mistake;
  • Windows users, beware when using long file names under Linux. If a file name contains spaces (not recommended but possible), you must enclose the file name in double quotes whenever you refer to it. For example:
    $ # the following command makes a directory called "My old files"
    $ mkdir "My old files"
    $ ls
    My old files    bin     tmp
    
    Further, some characters shouldn't be used: some of those are !*$&#.
  • there are no compulsory extensions like .COM and .EXE for programs, or .BAT for batch files. Executable files are marked by an asterisk `*' at the end of their name when you issue the ls -F command. For example:
    $ ls -F
    I_am_a_dir/   cindy.jpg    cjpg*   letter_to_Joe    my_1st_script*  old~
    
    The files cjpg* and my_1st_script* are executables, that is ``programs''. Under DOS, backup files end in .BAK, while under Linux they end with a tilde `~'. Further, a file whose name starts with a dot is considered as hidden. Example: the file .I.am.a.hidden.file won't show up after the ls command;
  • DOS program switches are obtained with /switch, Linux switches with -switch or --switch. Example: dir /s<tt> becomes ls -R. Note that many DOS programs, like PKZIP or ARJ, use UNIX-style switches.

You can now jump to Section Translating Commands from DOS to Linux, but if I were you I'd read on.

4.2 Symbolic Links

UNIX has a type of file that doesn't exist under DOS: the symbolic link. This can be thought of as a pointer to a file or to a directory, and can be used instead of the file or directory it points to; it's similar to Windows shortcuts. Examples of symbolic links are /usr/X11, which points to /usr/X11R6; /dev/modem, which points to either /dev/ttyS0 or /dev/ttyS1.

To make a symbolic link:

$ ln -s <file_or_dir> <linkname>

Example:

$ ln -s /usr/doc/g77/DOC g77manual.txt

Now you can refer to g77manual.txt instead of /usr/doc/g77/DOC. Links appear like this in directory listings:

$ ls -F
g77manual.txt@
$ ls -l
(several things...)           g77manual.txt -> /usr/doc/g77/DOC

4.3 Permissions and Ownership

DOS files and directories have the following attributes: A (archive), H (hidden), R (read-only), and S (system). Only H and R make sense under Linux: hidden files start with a dot, and for the R attribute, read on.

Under UNIX a file has ``permissions'' and an owner, who in turn belongs to a ``group''. Look at this example:

$ ls -l /bin/ls
-rwxr-xr-x  1  root  bin  27281 Aug 15 1995 /bin/ls*

The first field contains the permissions of the file /bin/ls, which belongs to root, group bin. Leaving the remaining information aside, remember that -rwxr-xr-x means, from left to right:

- is the file type (- = ordinary file, d = directory, l = link, etc); rwx are the permissions for the file owner (read, write, execute); r-x are the permissions for the group of the file owner (read, execute); (I won't cover the concept of group, you can survive without it as long as you're a beginner ;-) r-x are the permissions for all other users (read, execute).

The directory /bin has permissions, too: see Section Directories Permissions for further details. This is why you can't delete the file /bin/ls unless you are root: you don't have the permission to do so. To change a file's permissions, the command is:

$ chmod <whoXperm> <file>

where who is u (user, that is owner), g (group), o (other), X is either + or -, perm is r (read), w (write), or x (execute). Common examples of chmod use are the following:

$ chmod +x file

this sets the execute permission for the file.

$ chmod go-rw file

this removes read and write permission for everyone but the owner.

$ chmod ugo+rwx file

this gives everyone read, write, and execute permission.

# chmod +s file

this makes a so-called ``setuid'' or ``suid'' file---a file that everyone can execute with its owner's privileges. Typically, you'll come across root suid files; these are often important system files, like the X server.

A shorter way to refer to permissions is with digits: rwxr-xr-x can be expressed as 755 (every letter corresponds to a bit: --- is 0, --x is 1, -w- is 2, -wx is 3...). It looks difficult, but with a bit of practice you'll understand the concept. root, being the superuser, can change everyone's file permissions. RMP.

4.4 Files: Translating Commands

On the left, the DOS commands; on the right, their Linux counterpart.

ATTRIB:         chmod
COPY:           cp
DEL:            rm
MOVE:           mv
REN:            mv
TYPE:           more, less, cat

Redirection and plumbing operators: < > >> |

Wildcards: * ?

nul: /dev/null

prn, lpt1: /dev/lp0 or /dev/lp1; lpr

Examples

DOS                                     Linux
---------------------------------------------------------------------

C:\GUIDO>ATTRIB +R FILE.TXT             $ chmod 400 file.txt
C:\GUIDO>COPY JOE.TXT JOE.DOC           $ cp joe.txt joe.doc
C:\GUIDO>COPY *.* TOTAL                 $ cat * > total
C:\GUIDO>COPY FRACTALS.DOC PRN          $ lpr fractals.doc
C:\GUIDO>DEL TEMP                       $ rm temp
C:\GUIDO>DEL *.BAK                      $ rm *~
C:\GUIDO>MOVE PAPER.TXT TMP\            $ mv paper.txt tmp/
C:\GUIDO>REN PAPER.TXT PAPER.ASC        $ mv paper.txt paper.asc
C:\GUIDO>PRINT LETTER.TXT               $ lpr letter.txt
C:\GUIDO>TYPE LETTER.TXT                $ more letter.txt
C:\GUIDO>TYPE LETTER.TXT                $ less letter.txt
C:\GUIDO>TYPE LETTER.TXT > NUL          $ cat letter.txt > /dev/null
        n/a                             $ more *.txt *.asc
        n/a                             $ cat section*.txt | less

Notes:

  • * is smarter under Linux: * matches all files except the hidden ones; .* matches all hidden files (but also the current directory `.' and parent directory `..': beware!); *.* matches only those that have a `.' in the middle or that end with a dot; p*r matches both `peter' and `piper'; *c* matches both `picked' and `peck';
  • when using more, press <SPACE> to read through the file, `q' to exit. less is more intuitive and lets you use the arrow keys;
  • there is no UNDELETE, so think twice before deleting anything;
  • in addition to DOS' < > >>, Linux has 2> to redirect error messages (stderr); moreover, 2>&1 redirects stderr to stdout, while 1>&2 redirects stdout to stderr;
  • Linux has another wildcard: the []. Usage: [abc]* matches files starting with a, b, c; *[I-N1-3] matches files ending with I, J, K, L, M, N, 1, 2, 3;
  • lpr <file> prints a file in background. To check the status of the print queue, use lpq; to remove a file from the print queue, use lprm;
  • there is no DOS-like RENAME; that is, mv *.xxx *.yyy won't work. A REN-like command is available on ftp://metalab.unc.edu/pub/Linux/utils/file;
  • use cp -i and mv -i to be warned when a file is going to be overwritten.

4.5 Running Programs: Multitasking and Sessions

To run a program, type its name as you would do under DOS. If the directory (Section Using Directories) where the program is stored is included in the PATH (Section System Initialisation Files), the program will start. Exception: unlike DOS, under Linux a program located in the current directory won't run unless the directory is included in the PATH. Escamotage: being prog your program, type ./prog.

This is what the typical command line looks like:

$ command [-s1 [-s2] ... [-sn]] [par1 [par2] ... [parn]] [< input] [> output]

where -s1, ..., -sn are the program switches, par1, ..., parn are the program parameters. You can issue several commands on the command line:

$ command1 ; command2 ; ... ; commandn

That's all about running programs, but it's easy to go a step beyond. One of the main reasons for using Linux is that it is a multitasking os---it can run several programs (from now on, processes) at the same time. You can launch processes in background and continue working straight away. Moreover, Linux lets you have several sessions: it's like having many computers to work on at once!

  • To switch to session 1..6 on the virtual consoles, press <ALT-F1> ... <ALT-F6>
  • To start a new session in the same v.c. without leaving the current one, type su - <loginname>. Example: su - root. This is useful, for instance, when you need to perform a task that only root can do.
  • To end a session, type exit. If there are stopped jobs (see later), you'll be warned.
  • To launch a process in background, add an ampersand '&' at the end of the command line:
    $ progname [-switches] [parameters] [< input] [> output] &
    [1] 123
    
    the shell identifies the process with a job number (e.g. [1]; see below), and with a PID (Process Identification Number; 123 in our example).
  • To see how many processes there are, type ps ax. This will output a list of currently running processes.
  • To kill (terminate) a process, type kill <PID>. You may need to kill a process when you don't know how to quit it the right way.... Unless you're root, you can't kill other people's processes. Sometimes, a process will only be killed by kill -SIGKILL <PID>. In addition, the shell allows you to stop or temporarily suspend a process, send a process to background, and bring a process from background to foreground. In this context, processes are called ``jobs''.
  • To see how many jobs there are, type jobs. Here the jobs are identified by their job number, not by their PID.
  • To stop a process running in foreground, press <CTRL-C> (it won't always work).
  • To suspend a process running in foreground, press <CTRL-Z> (ditto).
  • To send a suspended process into background, type bg <%job> (it becomes a job).
  • To bring a job to foreground, type fg <%job>. To bring to foreground the last job sent to background, simply type fg.
  • To kill a job, type kill <%job> where <job> may be 1, 2, 3,...

Using these commands you can format a disk, zip a bunch of files, compile a program, and unzip an archive all at the same time, and still have the prompt at your disposal. Try this with Windows, just to see the difference in performance (if it doesn't crash, of course).

4.6 Running Programs on Remote Computers

To run a program on a remote machine whose name is remote.machine.edu:

$ telnet remote.machine.edu

After logging in, start your favourite program. Needless to say, you must have a shell account on the remote machine.

If you have X11, you can even run an X application on a remote computer, displaying it on your X screen. Let remote.machine.edu be the remote X computer and let local.linux.box be your Linux machine. To run from local.linux.box an X program that resides on remote.machine.edu, do the following:

  • fire up X11, start an xterm or equivalent terminal emulator, then type:
    $ xhost +remote.machine.edu
    $ telnet remote.machine.edu
    
  • after logging in, type:
    remote:$ DISPLAY=local.linux.box:0.0
    remote:$ progname &
    
    (instead of DISPLAY..., you may have to write: setenv DISPLAY local.linux.box:0.0. It depends on the remote shell.)

Et voila! Now progname will start on remote.machine.edu and will be displayed on your machine. Don't try this over the modem though, for it's too slow to be usable. Moreover, this is a crude and insecure method: please read the ``Remote X Apps mini-HOWTO'' at http://www.linuxdoc.org/HOWTO/mini/Remote-X-Apps.html.


Next Previous Contents
Copyright © 2010-2024 Platon Technologies, s.r.o.           Home | Man pages | tLDP | Documents | Utilities | About
Design by styleshout