A Beginners Guide to Unix and Linux
Introduction
UNIX is a multiuser, multitasking operating system originally developed at Bell Laboratories in 1969 by Ken Thompson, Brian Kernighan and Dennis Richie. UNIX has become significant over the years due to the central role of the C programming language in its development.
Unix/Linux is about shared services Personal computing usually means you have a single machine all to yourself. Unix was designed at a time when computing was considered a "service" like telephony or electricity - where you "plug-in" to a high-reliability, shared service. Don't assume that you can just swich off a Unix/Linux machine --- other people may be using it over the network. Other people might even have programs scheduled to run during the night. Many Unix users access their system over a network, having never seen the machine in person. Normally, you have to issue a special command to the Unix software before it is safe to turn off the power. (Under Linux, the ctrl-alt-del sequence of keypresses is often set up to trigger a shutdown.)
Unix/Linux provides named accounts and "ownership" Until comparatively recently, most operating systems designed for personal computing allowed all users to control everything in the machine. Not so in the Unix/Linux world. All Unix/Linux users have an account. Each file and directory in a Unix/Linux system has an owner and a set of permissions. Unix/Linux also controls which programs you can run based on your account. Limits can be placed on disk space used and time spent running programs on an account-by-account basis.
How is Linux different from Unix? Unix was first released from AT&T around 1974. By the mid-1980's there were already many extensions. Linux was developed in the early 1990's by Linus Torvalds as a small project. Technically, Linux is not Unix. From the user's point of view, there is little difference between Linux and Unix as both systems follow roughly the same specifications.
Logging in
In this section we explain how to log in to UNIX from a text-terminal. UNIX differentiates between different users for the purposes of job scheduling and file ownership by means of user names and passwords. At the UNIX console or text-terminal you will see a prompt that looks something like this:SunOS UNIX (FunnyMachineNameGoesHere)login:
At this point, enter your username. (Like most UNIX applications and languages the username and password are case-sensitive.) After entering the username, you will be prompted for a password:
password:
You will then either get a "banner" containing the "message of the day" (system-wide announcements), or a short message, typically "Login incorrect" (If you are logging in from a graphics console you will not see a banner or the message of the day.)
The login process will not warn of unrecognized usernames --- it simply accepts the username and password or rejects them both. (This is to ensure that unauthorized users must discover a username and password.)
Changing your password
The first thing you should do when issued with a UNIX account is to change your password. On a stand-alone UNIX machine, you usually issue the passwd command. The UNIX lab contains a network of machines that share the same login data over the network (using the NIS networking software). To change your password in the UNIX lab, you must issue the passwd command. You will be prompted for your old password, and then if entered successfully, you will be prompted for the new password twice (to check for typing mistakes).
- To log-out from the CDE environment, click on the "exit" icon.
- To log-out from a text-based terminal you usually type logout.
The UNIX Manual
The UNIX manual is provided on-line. Each command and each C-programming library can be found in the manual. You can view manual entries using the command line man program. For instance, typing man man retrieves the page about the manual viewing program. To scroll down through the manual press space, to scroll up press b, and to quit press q. The UNIX manual is traditionally split into eight volumes. The volumes are:
- Commands
- System Calls
- Library Functions
- Special Files
- File Formats
- Games and Demos
- Miscellaneous Information
- Maintenance Commands
Sometimes a keyword appears in more than one volume. For instance, kill. If you wish to search volume 2 only for kill, issue man 2 kill. If you omit a manual volume number, it will search in ascending order from volume 1 onwards and return the first matching page. (You may find a reference to the keyword in other volumes under the "see also" section of the manual page returned.)
X Windows
Modern UNIX typically involves using the X windows system. This system separates components of the GUI into clients and servers. The system responsible for drawing on the screen is called the server as it provides a "graphics service" for the client programs. The user interface elements (including the menu bars, maximising and minimising facilities, scrolling, colours and backgrounds) are managed by a window manager. The window manager is an X-client like the application programs themselves. (There are many window managers available, the commonest include the Common Desktop Environment, OpenLook, Motif and fvwm.) We use the Common Desktop Environment window manager in the UNIX lab (called dtwm).
Due to the client/server nature of X windows, you may use your present machine as a server, but in principle execute the application on another machine anywhere on the Internet. (There are even X window servers for the PC and Mac. A server called Exodus is available for the Mac. )
- Under the Common Desktop Environment, you can bring up a shell by clicking the right mouse button to bring up the menu, selecting the "Programs" submenu and then selecting "Terminal". A shell is similar to a command-line interpreter on small computer systems.
Most UNIX machines which do not offer a graphical login screen start the X window system (with the currently set window manager) by the script startx.
The Command-line
Traditionally, UNIX is used by typing commands at a command-line prompt. When you log-in, you are presented with a login prompt for your default shell. The shell interprets typed commands. You can compile lists of commands into a file called a "shell-script", which can be executed as a program. UNIX systems usually provide many different shells. Your default shell is called the C-shell (written csh.) The C-shell provides an environment that resembles the C programming language and is well-suited for interactive use.
File Management
The file is the most important concept in UNIX. Files are stored in directories. Directories are be contained within directories, creating a hierarchial structure.
Directory Navigation (using ls and cd To list the contents of the current directory, you issue ls. This will print a listing of the files and directories contained within the current directory. To enter a subdirectory named dirName issue
cd dirName
To move back up to the parent directory issue cd ... (The ".." represents the parent directory.
All users have a "home directory". This is the directory that is entered when you login in to the system. To change directory directly to your home directory, just type the cd command with no arguments. There is also a special syntax used by most shells for the home directory. Typically, for username csfred the home directory is written ~csfred (this will be translated by the shell into the full pathname, say /compsci/saturn/ugrad/ugrad1/csfred.
You type ls to list the current directory. Here is the result of issuing ls -la. The -la flags indicate display in "long format" and to display "all files" including hidden files. A hidden file is simply a file whose name begins with a period (.).
total 41 drwx------ 6 daf users 1024 Aug 27 10:56 ./ drwxr-xr-x 5 root root 1024 Aug 15 16:06 ../ -rw-r--r-- 1 daf users 69 Aug 16 19:27 .cshrc drwxr-xr-x 2 daf users 1024 Aug 13 16:38 .hotjava/ -rw-r--r-- 1 daf users 211 Aug 24 21:00 .plan drwxr-xr-x 2 daf users 1024 Aug 27 10:49 ccourse/ ...
The first character in the first column indicates the type of entry (either - for an ordinary file, d for directory, or l for a "link" to another file or directory held elsewhere in the file system. The remaining 9 characters in the first column show file-permissions. These characters show the access permissions in terms of read, write and execute. The leftmost block of three permissions shows the owners permissions, the second block, the group permissions, and the third block, the "other" permissions. For example, the file .plan can be read and written but not executed by the owner, and only read by users in the same group or by others. Notice the directories ".. and ".". The entry "." always denotes the current directory. On UNIX systems, the directory ".." denotes the directory above the current directory (i.e. the "parent directory").
If there is a - in the read, write or execute field that corresponds to your user account, then you do not have that permission. Attempting to change to a directory for which you don't have execute permission will yield the error message permission denied. The same is true for using ls on directories for which the relevant read permission is not set. (Remember to set your home directory permissions so that others do not copy your work. For example, by setting your home directory to drwx------. See the chmodcommand below.).
We summarise the rules for filenames:
- File names are case-sensitive.
- Historically, filenames have a maximum limit of 14 characters (though far longer filenames are allowed on modern UNIX systems.)
- A file is considered "hidden" it if begins with a period (".").
The most common file extensions include:
- .c
- C programs or files of C code
- .h
- C and C++ program "header" files
- .p
- Pascal code
- .o
- Compiled (but not executable) code. (The file contents are similar to the .OBJ files output from Turbo Pascal on the PC.)
- .cxx and .C
- C++ code
- .a
- Archive files. (Archives are files usually containing code which can be copied into source code of programs. For further details, see the man page for ar.)
- .tar
- A "TApe Archive": An archive of files and directories. (Not compressed.)
- .Z
- A compressed file. (Use gzip -d or uncompress to restore.
- .gz
- A compressed file using the GNU gzip utility.
- .tar.gz .tar.Z and .tgz
- Combinations of .tar .Z and .gz as explained above. Note that .tgz is the contention on the PC for .tar.gz files.
Other important commands
- alias
- This keyword allows the user to set an identifier which represents a certain command-line. When the user issues that identifier, the corresponding command-line is run. For example, issuing:
alias "gcc" "gcc -Wall -ansi -pedantic-errors" allows the user to issue:
gcc myprog.c
causing the shell to substitute and run:
gcc -Wall -ansi -pedantic-errors myprog.c (Note that alias is built-in to the C-Shell, rather than a stand-alone program.) - rm
- Remove a file. To remove a file named fileName from the current directory issue:
rm fileName - cp
- Copy a file or directory. To copy the file tstdata.dat from the current directory to the bak subdirectory, issue:
cp tstdata.dat bak To copy a file named thisFile from the current directory to your home directory issue:
cp thisFile ~ - mv
- Move a file or directory. Issue:
mv /data/current/db1.dat /data/historic
to move the file /data/current/db1.dat to /data/historic/db1.dat. - mkdir
- Make a directory. Issue:
mkdir nameOfNewDir to create nameOfNewDir in the current directory. - rmdir
- Remove a directory. Issue:
rmdir dirNameHere to remove the directory dirNameHere from the current directory. If the command is used in this form the directory must be empty. (There is a recursive remove option -R, and a "force" option -f which allow removal of subdirectories, even if marked read-only. Use these options with care.) - cd
- This command changes the current directory. There are three basic means of use:
- Change from a specified path to a specified path, for instance change from /home/hardSums/sum1 to /home/hardProgs/c1. This is issued as:
cd /home/hardProgs/c1 - Change from current directory to parent directory:
cd .. - Change from current directory to your "user root" or "home directory":
cd
- Change from a specified path to a specified path, for instance change from /home/hardSums/sum1 to /home/hardProgs/c1. This is issued as:
- pwd
- Print working directory. This command prints the full pathname of the current directory. (Simply issue pwd.)
- chmod
- Change permissions. There are two basic methods for using chmod. The simplest is to issue the chown command symbolically, for instance chmod ug+rx filename, to allow the user and those in the same group to have read and execute access on a file or directory called filename. (The plus may be replaced by a minus to remove permissions instead of conferring them.)
The traditional way to use chmod involves the use of octal digits. You issue a command of the form chmod yxz filename where xyz is a sequence of octal digits. The leftmost digit specifies the user permissions, the middle digit is the group permission and the rightmost digit specifies other user permissions. For each position, start with 0, and add 4 for read permission, 2 for write permission, 1 for execute permission, and leave as 0 for no permission. For instance, read and write by owner, read by group, and no access to all others, is specified as 640 (that is 4+2,4,0).
- chown
- Change file ownership. (The Unix system software limits the functionality of chown if you are not the user root, the super-user. For instance, you may not set ownership of some other user's file to become your file.)
- cat
- Print a series of one or more files with no separating characters (i.e. Concatenate files.) For example, to print the file header then data.dat followed by the file footer, issue:
cat header data.dat footer without any separation.) - more
- Page though a file one screenful at a time. At the foot of each screen you see the --More xx%-- prompt, showing the percentage of the file read so far. (Press space to scroll down a screenful, Enter to scroll down one line, b to scroll up one screenful, v to enter the vi text editor, and q to quit.
Software development Commands
- gcc
- The GNU C compiler. A C prorgam stored in the file myprog.c is compiled into a binary file called a.out by issuing:
gcc -Wall -ansi -pedantic-errors myprog.c
The name a.out is the default output filename. Appending -o myexe to the above command line would output the binary file under the name myexe. - cc
- The traditional way to invoke the standard C compiler. (The C compiler is often sold separately from the operating system and may not be installed.)
- gdb
- The GNU debugger. A program for diagnosing the source of crashes, watching variables inside a program at runtime and other debugging facilities. (This will be discussed in the C course.)
- make
- A program for managing multi-source file software development. (The book Managing Projects with Make is quite useful.)
- script
- A program that starts a shell in which all input and output is logged into a file in the current directory called typescript. This command is useful for documenting test runs. Once the program has commenced, all input and output is logged in the file until the shell is closed. The program can be closed by typing exit at the command prompt.
Other useful commands
- wc
- Word count. Print the number of characters, words and lines in a file. For example, to discover the number of lines in the file myAnswer.c issue:
wc -l myAnswer.c
(The -l form will print only the number of lines in a file or list of files. Calling this command without -l above will print the number of characters, words and lines in the file instead of printing only the number of lines.) - head
- Print the first few lines of a file or files. To print the first few lines of all files in the current directory with the suffix .c issue:
head * .c
For example, executing the head command shown above with the following files in the current directory:
.c one.c two.c three.c.old would cause the command line:
cat .c one.c two.c
to be executed. - file
- Test the file type.
- nl
- Print file with line numbers. (Use the -ba switch to set nl to include blank lines in the count.) For example, to print the file thisFile in the current directory (including empty lines), issue:
nl -ba thisFile - grep
- Print only those lines in the specified files that match a search pattern.
- script
- This captures all input and output from a shell into a file (called typescript by default.) You can use this for documenting tests.
- tar
- Tape ARchiver. This command will create a file which contains a specified list of files and directories. You can save files to a .tar file and they can be restored with the same permissions, ownership and access times. Typical usage for:
- creating a .tar file
- tar -cvf newarchive.tar mywork/ would archive the directory mywork into newarchive.tar. (The -c means create.)
- testing or listing a file
- tar -tvf newarchive.tar (The -t means test.)
- extracting files from a .tar file
- tar -xvf newarchive.tar * would extract all the contents of newarchive.tar into the current directory. (Directories are created as necessary.)
- gzip
- File compression utility. (This is the GNU file compression tool.) The "standard"' or "traditional" compression tool is compress, but that tool is not usually available on FreeBSD or Linux platforms.
Metacharacters
UNIX shells typically provide metacharacters (also known as wildcards.) These are shorthands for a list of files. These strings of special characters are used to match against file names on the command line. For instance, if you pass the command cat test.* given the following files in the current directory:
test.sun.result, test.linux.result, tst.cray.result, and test.
the shell will translate the * (meaning zero-or more characters may match here), so that it executes:
cat test.sun.result test.linux.result test.
(This process is known as globbing.)
A table of commonly used metacharacters is shown below. Metacharacters can be used in combination, for instance:
cat messages.Jan.[0-3][0-9].*.am
will print all files starting with the prefix messages.Jan, followed a number between 0 and 3, inclusive, then followed by any digit, followed by an arbitrary sequence of zero or more characters, ending in the suffix .am.
Some of the C-shell metacharacters:
- *
- Zero or more characters may match.
- ?
- Any single character may match.
- [ ]
- An enclosed list of characters, where the match is on any single character. A minus sign between two characters in the brackets denotes any character sequentially ordered between the two characters may match.
Customizing the C-shell
There are several C-shell scripts that run automatically at certain times. These files are held you your home directory:
- .cshrc
- This is run whenever the C-shell starts.
- .login
- This is run only when you log-in.
- .logout
- This is run when you log-out.
You can adjust default settings by placing commands in the .login file.
If you are logging into the Common Desktop Environment (CDE) the .login will not execute automatically on log-in until certain modifications are made. uncommenting of the last line of a file called .dtprofile will cause .login to execute automatically under the CDE. (Replace \#DTSOURCEPROFILE=true with DTSOURCEPROFILE=true.) Be careful however, as errors in your .login file may then terminate the login process. More information can be found by searching the CDE help Viewer for the phrase .login.
Default file permissions You may wish to set the "file creation mask" in your .cshrc file. This specifies which file permissions to remove from the usual default permissions of rw- (666) for non-executables, rwx (777) for executables and directories. For instance to ensure that newly created files are not writable by group members or others, add the line umask 022.
Hint You should seriously consider ensuring that alias rm "rm -i" exists in your .cshrc file. This will run rm with the -i option, even if it isn't specified explicitly. (This prompts interactively whether you wish to remove each specified file.)
The C-shell History If you type history on the command line, you will see a list of previously entered command lines. If you issue the command !n where n is the number of a command line, the command line numbered n will be executed again. Similarly, if you type !v the most recently issued command line commencing with the letter v will be executed.
Programming with the Shell
Unix shells can be used for scripting applications. By capturing a series of command lines in a file, repetitive tasks can be automated. Here, we consider only the C-shell, although other shells can be used for scripting (e.g. sh,bsh,ksh). In order to run these scripts you must:
- In a text file, ensure the first line reads: #!/usr/bin/csh
- Ensure the script has its execute permission set. For example, given script backupNow, issue: chmod u+x backupNow. To copy all the c files (.c) and c header files (.h) from ~/work/current to ~/work/backup, and append the current date to a log file, we could write the following script:
#!/usr/bin/csh cd ~/work/current cp *.[ch] ~/work/backup date >> ~/work/backup/backups_log # append date to log fileNote that hash symbol # is used to denote a comment, which automatically terminates at the end of the current line.
- The command line arguments are represented in the C-shell by $1, $2,...
- The name of the current script is held in $0.
#!/usr/bin/csh cat $1 $2 > $3
Further C-Shell Programming
The C-shell can be used to automate a large number of Unix tasks. For further reference, see the manual page man csh and the following guidebook:
Gail Anderson and Paul Anderson, The Unix C Shell Field Guide, Prentice-Hall
Pipes and Filters
Unix systems allow the output of one command to be used as input for another command. Complex tasks can be performed by a series of commands one piping input into the next. For example, the following command line, decompresses a temporary copy of a file and compares it against an older copy.
gzip -dc new.gz | diff old
Basically a pipe is written generator | consumer. For instance, in the above example, the gzip program produces a decompressed output, which is read by the diff program.
Files can be piped into processes instead of prompting the user for input. (Input and output to files using pipes is called redirection.) For instance the mail program can be used to mail a file by issuing mail csperson < file. Similarly, output can be captured to a file by issuing >. For instance, man ls > how_lsWorks, which will dump the man page for ls to a file.
Hint: You might want to set a shell variable called noclobber in your .cshrc file, by adding the line set noclobber. This shell variable indicates that use of > are not allowed to overwrite existing files. Consider what would happen if you accidentally issued:
csio % cat someFile > someFile
The shell will open someFile for writing new output, consequently erasing someFile before starting cat someFile to produce the output. The empty output file replaces the input data in someFile!.
In addition to simple redirection, it is possible to append to a file with the >> operator.
(You can issue unset noclobber manually to reverse noclobber should the need arise.)
Standard output (stdio) and Standard Error (stderr) Unix systems provide at least two output files for each process, standard output and standard error. Data and program control information is usually written to standard output. Debugging traces and error messages should always be written to the standard error file. Standard error is usually visible to the user (unless redirected). The > symbol redirects standard output. Standard error and standard input can be simultaneously redirected by the >& symbol. (The C compiler outputs compile-time errors on standard error, so you may use >& to obtain a file containing all the warnings and compile errors.)
gcc mysource.c >& errors
- To end a file begin typed on the standard input, type CTRL-d. (The DOS equivalent is CTRL-z.)
Printing
The print command is usually lpr. The types of file that can be printed vary from one machine setup to the next. The only type of file that is likely to print successfully on every system is plain text. Sending binary files, images or postscript to a print system that doesn't recognise it could result in hundreds of pages of wasted paper!
You can specify a list of files to print as arguments (e.g. lpr work.c work.txt, or use lpr as a filter (e.g. diff oldwork.c newwork.c |lpr.) You can view the print queue with lpq, for instance:
csantibiotix % lpq SPARCprinter is ready and printing Rank Owner Job Files Total Size active daf 476 uguide.xml 35279 bytes ...
You may remove print jobs with lprm. For instance, to remove the job named uguide.xml given the printer queue state shown above, issue lprm 476.
Process Control
A running program is called a process. You can list running processes, impose or change scheduling limits and of course shut down processes.
- ps
- Display the processor state. Here is an example list returned by ps:
PID TTY STAT TIME COMMAND 209 1 S 0:00 -tcsh 237 1 S 0:00 sh /usr/X11/bin/startx 238 1 S 0:00 xinit /usr/X11R6/lib/X11/xinit/xinitrc -- 242 1 S 0:01 fvwm95-2 -s 2245 p0 S 0:00 -tcsh 9020 p0 R 0:00 ps -x
The first column shows the "process id" (the unique, identifying number of a process). The process is attached to the "terminal" (historically called a tty (or teletype terminal)). The status of the process is shown by the STAT column. (In this example we see that ps is running, and that all the other processes that the user owns are sleeping. The TIME column shows how much CPU time has been spent by each process. The final column shows the name of the process. (Notice how command-line arguments become included in the name.) A more comprehensive output can be obtained with the -aux flags There are two versions of ps in widespread use. The default Solaris version will not recognise the arguments -aux use -ef instead. (Under Solaris, the standard version of ps is /usr/bin/ps, but we refer to /usr/ucb/ps (the BSD version) in this document. , which will show all processes, x --- including those not attached to a terminal (for instance system processes). The u option sorts the entries into decreasing order of CPU usage. For instance:csio% ps -aux USER PID %CPU %MEM SZ RSS TT STAT START TIME COMMAND cshl 3323 23.7 4.5 212 460 p8 R 16:00 0:00 ps -aux cshl 3316 0.5 3.5 56 364 p7 S 15:59 0:00 -csh (csh) cshl 3318 0.5 3.5 56 364 p7 S 15:59 0:00 -csh (csh) root 107 0.0 0.2 16 24 ? S Aug 25 0:20 screenblank root 51 0.0 0.0 68 0 ? IW Aug 25 0:22 portmap bin 56 0.0 0.0 36 0 ? IW Aug 25 0:00 ypbind
- kill
- Sends a signal to a process. (Usually we send the "kill" or shutdown signal -9.) For instance, to shut down the first csh process in the ps listing above, we would issue kill -9 3316. (The number 3316 is the process-id of the csh process to be killed.)
- nice
- Be nice to other users. This is of little use today. Historically, it has been used to reduce the scheduling priority of long jobs, so that interactive work isn't too slow. In the UNIX lab, you will rarely run long batch jobs, and will most of the time be the only user logged in on a specific machine. (Typical modern use of nice would be to limit the cpu time consumed by user queries executed by a web-server.) \item[kill] Send a signal to a process. (Usually the "kill" signal.) Signals send specific messages to a process. By default certain signals are ignored, and others cause program termination. Examples of signals that terminate the process are KILL (terminate) and FPE (floating-point exception). Some signals will cause a process to voluntarily terminate, but kill -9 is the signal which forces a process to terminate.
> forever & [1] 581 > ps -x PID TTY STAT TIME COMMAND 236 ? S 0:00 sh /usr/X11/bin/startx 237 ? S 0:00 xinit /usr/X11R6/lib/X11/xinit/xinitrc -- 241 ? S 0:00 fvwm95-2 -s 351 p0 S 0:00 -tcsh 392 2 S 0:00 -tcsh 543 p0 S 0:00 vi uguide.xml 577 2 S 0:00 script 578 2 S 0:00 script 579 p1 S 0:00 -csh -i 581 p1 R 0:03 forever 582 p1 R 0:00 ps -x > ps -x PID TTY STAT TIME COMMAND 236 ? S 0:00 sh /usr/X11/bin/startx 237 ? S 0:00 xinit /usr/X11R6/lib/X11/xinit/xinitrc -- 241 ? S 0:00 fvwm95-2 -s 351 p0 S 0:00 -tcsh 392 2 S 0:00 -tcsh 543 p0 S 0:00 vi uguide.xml 577 2 S 0:00 script 578 2 S 0:00 script 579 p1 S 0:00 -csh -i 581 p1 R 0:07 forever 583 p1 R 0:00 ps -x > kill -9 581 > ps -x PID TTY STAT TIME COMMAND 236 ? S 0:00 sh /usr/X11/bin/startx 237 ? S 0:00 xinit /usr/X11R6/lib/X11/xinit/xinitrc -- 241 ? S 0:00 fvwm95-2 -s 351 p0 S 0:00 -tcsh 392 2 S 0:00 -tcsh 543 p0 S 0:00 vi uguide.xml 577 2 S 0:00 script 578 2 S 0:00 script 579 p1 S 0:00 -csh -i 584 p1 R 0:00 ps -x [1] + Killed forever > exit
In the example above we see the process forever is commenced as a batch-job (this is the significance of & after the command-line). We find the process id, 581 using ps and then issue a kill. Some programs choose to "handle signals" such as INT (interrupt) so they can perform shutdown work (flushing and closing files for instance). The -9 signal (KILL) indicates that kill sends a signal which can't be "handled" by the process --- the termination is unavoidable.
You may run a program "in the background" by placing an ampersand & after the command line arguments. This will run the job as normal, dumping any messages on the screen, but the shell will continue running, and you may continue issuing other commands.
Unix normally sends a "hang-up" signal to processes running when you log out. If you wish to start a process which is to survive after logging out, you should run the process from the nohup command.)
The C-Shell has it's own job control features:
- Listing jobs
- To list current jobs, issue jobs -l. This will print a list of job names and their job numbers.
- Suspending a job
- The current process can be suspended by issuing CTRL-z. You can also use stop %n to suspend the job with job number n.
- Resuming a job
- A suspended job can be resumed by issuing the command line fg %n where n is the C-shell job number.
- Resuming a job in the background
- A suspended job can be resumed "in the background" by issuing bg %n where n is the job number.
- Interrupting a job
- To "interrupt" the foreground job, issue CTRL-c. (This usually quits the process, but unlike kill -9 allows the process to perform "clean-up operations first.
Editing Text
(This section should be considered optional for casual Unix/Linux users that are expected to work completely within X Windows.)
Choosing an Editor
Which editor should I use? A number of different text editors are available under UNIX and Linux. The two most popular editors are vi ("visual editor") and emacs (sometimes also called "xemacs".) You may use any editor, but if you are going to spend large amounts of time in the Unix/Linux world, you will need to be familiar with either vi or emacs. In short, vi is very widely installed but minimalist, whereas emacs is not always available but has a huge number of powerful features and an inbuilt LISP interpreter for customisation(!)
Useful books We will cover vi briefly in this guide. Both vi and emacs have extensive on-line manuals, but here are some friendly starting points:
Lamb and Robbins, Learning the vi Editor
Cameron, Rosenblatt and Raymond, Learning GNU Emacs
Why we cover vi here It is worth knowing how to use vi since it is installed as standard on practically all Unix and Linux machines. The vi editor also requires only a text-based terminal. Compared to graphical editors vi is difficult to learn. But it does have some advantages:
- Since vi uses only a text display it is possible to edit text remotely over a very low speed dial-up line using a simple "remote terminal" program such as telnet rlogin or ssh. (Running X windows remotely over a slow dial-up line would involve sending too many bitmaps to be usable in most cases.)
- Some people find it easier to learn one editor well and use it everywhere, rather than continuing to install new editors on every machine they encounter. When visiting other Unix/Linux users or borrowing a machine, knowing vi means you won't have to dig around for a familiar editor.
- The author once worked in an environment in which the X Window System had to be reconfigured. Colleagues relying on graphical editors could not reconfigure their machines because they needed a working graphics system to edit the file that booted X Windows. The author simply used vi from the terminal to reconfigure his file.
Using vi
Note that vi is entirely keyboard driven and does not respond to mouse operations.
The vi editor has two operation modes, insertion mode and command mode. You use may invoke vi by typing vi name_of_file. Once loaded, you are placed in command mode. In command-mode, most keys will trigger a command. You may use the arrow keys to move up, down left and right by one character. Certain commands will put vi in insertion mode. You press escape to leave insertion mode. Here are some of the most basic commands:
- i
- Insert text before the cursor. The editor is then placed in insertion mode. To leave insertion mode, press escape.
- a
- Insert text after the cursor. The editor is then placed in insertion mode as with command i above.
- Ctrl-d
- Scroll down half a screenful.
- Ctrl-u
- Scroll up half a screenful.
- w
- Move right one word.
- b
- Move back one word.
- H
- Move to the top line on the screen.
- M
- Move to the middle line on the screen.
- L
- Move to the last line on the screen.
- $
- Move to the end of line.
- ^
- Move to the start of line.
- u
- Undo last operation.
- x
- Delete one character.(The character is stored in the buffer.)
- dw
- (Press d, then w.) Delete to end of current word.(The deleted text is stored in the buffer.)
- dd
- (Press d then d again.) Delete the current line. The deleted line is placed in the buffer.
- yy
- "Yank" the line into the buffer (without deleting it.)
- p
- Paste the contents of the buffer after the cursor.
- 5 G
- Go to line number 5. (You may use n G to jump to line number n jump to line number.)
- /
- The / key followed by some search-pattern, will find the next occcurence of the search-pattern in the text after the cursor position. If you wish to find the next occurence of the pattern, press / once more.
- ?
- The ? key followed by some search-pattern, will find the last occcurence of the search-pattern in the text before the cursor position.
- ZZ
- Saves the file and exits.
- ~
- Changes the case of the character under the cursor.
- %
- If the cursor is placed on a bracket, vi will attempt to match the bracket with a corresponding opening or closing bracket, and move the cursor to that position.
You can use combinations of the delete operations x,dw and dd) with the paste operation (p) to move text. For instance in command mode the sequence ddp swaps the line containing the cursor and the line immediately below the cursor. Note that if you perform two delete operations, the buffer contains only the text deleted by the most recent delete operation.
Repeating commands Typing a number and then a command letter, will result in executing the command the specified number of times. (Try typing 7, then "i", then type "I must attend meetings" followed by enter, then escape.)
Command lines You can also type vi command lines. Enter ":" to display the command line. To discover the current line number, issue # (followed by enter). To jump to line number simply type the number at the : prompt and press enter. You can issue batch mode ex editor editing commands at this prompt. The ex editor is a "line-at-a-time" editor, similar to edlin under DOS. (Many Unix programs interpret or produce the ex commands --- sed (the "stream-editor"), and diff which computes the differences between text files are examples. (For instance, to delete all the lines from the first to the line before the current line you may issue :1,.-1 d . In these command lines, the period symbol denotes the current line number.) To erase all lines containing the string TRACE, you could issue :g/TRACE/d. (The form :g/pat/cmd issues the command cmd on every line containing the pattern pat.) To turn on line numbering, issue :set number. (To remove the line numbers, issue :set nonumber.)
Search-and-replace To substitute the first occurence of some text string on the current line, say colour for color, you can issue :s/color/colour/. That is the simplest form of substitution. You can use the form :\%s/color/colour/cg to substitute on all the lines with interactive confirmations, globally (that is, find all occurrences). (The % symbol denotes application to all lines in the file.)
File operations
- Save to file
- To save a file to a different name, issue :w newname. (To overwrite an existing file, issue :w! existingname.)
- Quit without saving
- To quit without saving issue :x.
- Quit and save
- Type ZZ in command mode.
- Insert a file
- To insert a file f at the cursor position, issue :r f.
File Transfer and Networking
Since Unix and Linux are multiuser systems many people can use the same machine. You can discover who is logged on to your system, their (real) names, and what others are running on the system.
- ftp
- (File transfer protocol). UNIX is usually equipped with client and server ftp software. To ftp a file from another machine, the command is ftp. If you ftp to a UNIX machine from PCs or Macs you can transfer files from your account, by logging in with your username and password. On the PC's there is usually a command-line ftp application. Some Novell systems provide the WYSIWYG application "Rapid Filer". Some Macs have an ftp client called "Fetch".
- who
- Shows how is logged on. Where available, the command rusers shows users on an entire network.
- w
- Shows what each user is running.
- finger
- Running finger username@host will show the login-details of username on the machine host. For example finger csfred@csbox.somelab.someuni. (Issuing finger @host will sometimes list all the users presently logged on at host. This particular variation of use is often disabled though, because it's an easy way for hackers to learn about local account names.)
- talk
- Allows you to interactively talk to users.
- telnet
- This allows you to connect to other UNIX machines, anywhere on the Internet. (You might telnet to another Unix machine and try out who.)
- rlogin
- There is an alternative to using telnet called rlogin. Sometimes rlogin can be more convenient than telnet. (rlogin is one of the "r" utilities: rcp, rsh and rlogin. These utilities allow users of one Unix machine to work on another. Since these utilities involve authentication between two separate machines, careful configuration is needed to ensure security.)
- ssh
- Secure Shell - a program that allows you to interactively log in to other machines. Unlike telnet and rlogin, this program encrypts the input and output steams between both ends. (This program is also available for many implementations of Windows and other operating systems.)
Health Warning: Don't use telnet, rlogin and ftp - unless you're prepared to run software that sends your passwords over the network in unencrypted form. (These days even script kiddies know how to do packet sniffing.) We recommend using ssh instead.
Going Further
Everyday Unix/Linux use
Guidebooks I recommend "Harley Hahn's Student Guide to Unix" as an introduction:
H. Hahn, Harley Hahn's Student Guide to Unix
Quick References For many years, I've used little other than my copy of "Unix in a Nutshell". It's a short book, but contains most of the information I need to customise vi and the shells. (It also has a section on emacs.) "Linux in a Nutshell" is also published by O'Reilly.
Arnold Robbins and Daniel Gilly, Unix in a Nutshell
Siever, Hekman, Figgins, Spainhour, Linux in a Nutshell
Programming with Unix/Linux
It's amazing how much more productive you can be when you've customised your environment to suit your work. Here are the first steps towards being a productive Unix/Linux developer:
- Learn to use a shell. When you've learned to use it, try to automate repetitive little programming jobs with it. (These shells are popular: csh, bash, ksh.)
- Practice combining programs using pipes and filters.
- Learn either vi or emacs. You will need a good text editor regardless of the language or IDE that you will use.
- Practice using the C compiler in the Unix/Linux environment. (The traditional command-line compiler is called cc, but the GNU C compiler is called gcc. Under Linux, most people use gcc.) Although C++ is popular today, the Unix/Linux system API is built around C rather than C++.
Learning the Unix/Linux/Posix API You can look up API documentation in the on-line manual using man --- just issue man functionname. Richard Stevens has an excellent book for learning Unix API concepts:
Richard Stevens, Advanced Programming In the UNIX Environment
Unix/Linux for Computer Science Students
Those interested what exactly Unix/Linux is and how it works might like the case studies on Unix and Linux in Taenenbaum's book:
A. Taenenbaum, Modern Operating Systems
Marice Bach's Unix book is a little dated, but does provide extensive detail:
M. Bach, The Design of the Unix Operating System
(Originally published in September 1998, updated 30th December 2002.)