This section is intended to give you a manageable overview of a small number of the most useful commands available on the average Linux system. Neither the list nor the descriptions of the commands are at all comprehensive, but they should give you an idea where to start. For a complete description, you should turn to the man pages on your system, or to a reference book (see bibliography).
Looking at the contents of files
There are various ways to look at the contents of text files. The simplest are cat, more, less and tail:
|cat||cat file||Outputs the contents of file to the screen.|
|more||more file||Outputs the contents of file to the screen, a page at a time|
|less||less file|| A more sophisticated version of more. However, the advantage of more is that the text generally
remains on the screen after more exits, whereas with less it does not.
|tail||tail file|| Displays the last ten lines of file to the screen. Can be made to display more or less using
the -n number option. There is also a corresponding command called head (no prizes for guessing
Comparing two text files
The key command here is the imaginatively-named diff, which takes the names of two files as arguments, and outputs any differences it finds. This is most useful when the two files can be expected to differ only slightly, or not at all.
Compressing and archiving files
Given the potential size of some data files, it can be useful to compress them, where possible. If you only have a single file you want to compress, you can use gzip:
[user@machine ~]$ gzip my_file
This will produce a compressed file called my_file.gz, and remove the original, uncompressed file. To restore the file to uncompressed state, simply use gunzip:
[user@machine ~]$ gunzip my_file.gz
With that, your original file is restored. Using the -v option with either command results in information being output to the screen about the compression/decompression process.
If you have more than one file that needs archiving/compressing, the tar command is your friend. The syntax is a bit obscure (tar is a venerable command that dates back to the days when data was routinely archived on tape), but entirely manageable when you get used to it. The first argument after the command is a series of letters that specify the operation to be performed:
|c||create an archive|
|t||list files in an archive|
|v||be verbose (i.e. print lots of output)|
|z||use file compression|
|f||the following argument is the name of the archive file|
Obviously, it doesn't make sense to specify more than one of x, c and t at a time. So, to archive the contents of my_dir to a compressed archive, this is the command you would typically use:
[user@machine ~]$ tar czvf my_dir.tar.gz my_dir
The suffix .tar.gz is conventional for the resulting archive files. To uncompress the directory, the procedure is very similar:
[user@machine ~]$ tar xzvf my_dir.tar.gz
Care must be taken to make sure this operation doesn't overwrite any pre-existing files located target directory
As mentioned previously, emacs is a popular and very versatile editor, available on almost all Linux systems. One of its great advantages is that it has many built-in highlighting and indentation modes for use with different programming languages. For example, Fortran code is automatically coloured to reflect its syntax, and the TAB key can be used to auto-complete do-loops and if-blocks.
Emacs is a sophisticated program, and has a built-in version of the LISP programming language, in addition to many useful text-editing tools. If you want to learn how to use it effectively, consulting one of the many books or websites devoted to the subject is essential.
For the more adventurous, and especially when in a situation where emacs is unavailable, the standard Linux editor is called vi. Explaining how to use vi is definitely beyond the scope of this document!
Printing is one of those things that varies somewhat between Linux systems; over the years, there have been many ways of handling printing in UNIX and Linux, and so there is perhaps more variation between systems than in other matters. So, here we will cover one of the most common printing frameworks, the Common Unix Print System (CUPS). Most of these commands should work on most Linux systems.
First, you need to find out what printers are attached to your system, and what they're called. The main command for discovering information about printers is lpstat:
[user@machine ~]$ lpstat -p -d
This combination of options returns a list of currently available printers and also gives the name of the default printer. On my system, I get this output:
printer GlaciologyMono is idle. enabled since Jan 01 00:00 system default destination: GlaciologyMono
So, I have only one printer available (called GlaciologyMono), which is the default.
The basic file format for printing in the UNIX/Linux is Postscript (files generally end in .ps). Postscript was devised by Adobe, and is in some respects similar to their popular Portable Document Format (PDF). Postscript files may be printed directly to a printer using the lpr command:
[user@machine ~]$ lpr -P GlaciologyMono my_file.ps
The -P option is used to specify the destination printer: if it is omitted, the default printer is used.
Without any options, lpstat can be used to discover information about what print jobs are being processed:
[user@machine ~]$ lpstat GlaciologyMono-270 irutt 20480 Thu 04 Oct 2007 12:00:26 BST
Only the user's own print jobs are displayed. To cancel a job, the cancel command is used, along with the job ID:
[user@machine ~]$ cancel GlaciologyMono-270
One very useful command that can be used to print plain text files is called a2ps (the name is shorthand for ASCII-to-Postscript). This command will format text in a sensible way, and convert to Postscript format. The number of columns, page orientation, font size, etc, can be specified using options. Usually, a2ps is configured to send its output direct to the default printer, but this can also be changed: see the relevant manual pages for details.
Accessing remote systems
It is often useful to be able to access other computer systems from the Linux command-line. Two basic methods exist: ssh allows you to log onto another computer and give it commands through the shell as though you were physically sitting in front of it, while sftp allows you to transfer files to and from another computer. In both cases, the initial `s' stands for `secure' --- the connections are encrypted.
Using ssh (Secure Shell) to access another computer is simple:
[user@machine ~]$ ssh -X email@example.com
Here, remotemachine.somewhere.ac.uk is the computer you want to log into, and username is your username on that machine. The -X option allows software running on the remote machine to display windows on your display. You'll be prompted to enter your password, of course.
Transferring files is done using sftp (Secure File Transfer Protocol), and the syntax is very similar:
[user@machine ~]$ sftp firstname.lastname@example.org
After entering your password, you're presented with a prompt that accepts a number of simple commands:
|put||copy a file from local to remote machine|
|get||copy a file from remote to local machine|
|cd||change current directory on remote machine|
|lcd||change current directory on local machine|
|ls||list files in current directory on remote machine|
|lls||list files in current directory on local machine|
|rm||delete file on remote machine|
|rename||rename a file on the remote machine|
|help||display help text|
One thing to note is that the put and get commands only act on one file at a time, so if you want to transfer large numbers of files, it is more efficient to use tar to make an archive of the files and transfer that.
Another useful command that can be used for file transfer is scp (Secure Copy). See its manual pages for more information.
One thing almost all regular users of Linux do is to customize the way Linux, and especially bash, behaves when they log in. There are several hidden files located in your home directory (they're hidden because they begin with a dot, so don't show up unless you do ls -a). The most useful is probably .bashrc, which consists of a list of commands to be executed when bash is started.
Among the tasks you can perform by adding lines to .bashrc are these:
- Customize the prompt --- for instance, to display the whole path, of just part of it, or not at all.
- Set extra search directories in the PATH.
- Set rm, cp and mv to prompt for deletions by default.
The last of these is accomplished by adding the following three lines to .bashrc:
alias rm='rm -i' alias mv='mv -i' alias cp='cp -i'