Instruments & Support

While the ILL's neutron source has remained essentially unchanged during the lifetime of the Institute, the ILL's instruments and their components have been continually developed and improved to increase their effectiveness.

Back to ILL Homepage
www > Instruments & Support > Useful tools > Introduction to unix > Commands 210A

Useful tools

Commands 210A - unix you don't need

This section includes some commands that are useful, but only in limited situations. Many commands are for text formatting from the command line. Other commands are just not commonly used or too complicated to use on a regular basis.

find [path] [options]

Searches for files meeting some criteria. You'd think that this command would be one of the most useful and important ones. You'd be wrong. The command is so complicated and UNIX directory trees are so complex that 'find' is not used very much. Still, in some cases, it can be useful.

The [path] indicates the starting directory. 'find' will search it and all of it's subdirectories. It is not recommended to start searches with the root directory (/) since many subdirectories are not publicly accessible and it's a very long and tortuous process to search all of the SACL system. Searching from the current directory (.) or your home directory (~/) is probably the most effective. Some options are used to specify the file to search for, but there are lots of options. I will include just the most basic ones here. See the man page for more details.

-print

displays the name of the matching files and their full paths. This option is almost always used.

-name [pattern]

specifies the name of the file to find. Wildcard symbols are allowed, but must be preceded with a '\'.

-newer [file]

specifies files that are newer than [file]

-perm [nnn]

specifies files with the file permission [nnn](e.g. 744)

-user [username]

specifies files that belong to [username]


Thus, to find and display the file foo.bar starting in my home directory, I would enter "find ~/ -name foo.bar -print".

tar (-cfrtvxzZ) [file-list]

Creates a tape archive file. This command is like 'zip', but without any compression. It creates a single file that contains all the files in [file-list]. By default, the 'tar' file is created on a tape device, unless the '-f' option is used.

-c creates a new 'tar' file, overwriting any existing file by the same name
-f [filename] specifies a [filename] for the 'tar' file
-r appends [file-list] to an existing 'tar' file
-t lists the contents of the 'tar' file
-v sets verbose mode which shows each file as it's archived or extracted
-x extracts [file-list] from a 'tar' file
-z compresses or expands the 'tar' file with 'gzip' and 'gunzip'
-Z compresses or expands the 'tar' file with 'compress' and 'uncompress'

If I wanted to tar all of the files in my account into one 'gzip'ped archive, I would enter "tar -cvzf jcjai ~/" and it would create a file named 'jcjai.tar.gz'.

su [username]

Temporarily logs you in as [username]. 'su' essentially opens a new shell as [username]. You will be prompted for the password of [username].

rsh [host] [command]

Runs a single [command] on [host]. The [command] can be enclosed in single quotes (') if it is longer than one word. This can be used to run a command that only works on SunOS machines or Solaris machines when you are sitting at the wrong type of terminal. It can also be used to check the system resources of a remote machine, say ply, by entering "rsh ply top".

action (-hp) (number)
msgs (-hp) (number)

These are message boards that contain information that relates to system administration. Often they also include important items about updates and software bugs. They are a good place to check first if something isn't working. Check them first, then contact a system administrator. Specifying a (number) will start reading from that message.

-h displays just the headers of the messages
-[number] starts displaying [number] back from the last message
-p pipes the output through 'more', such as for for long messages

touch [file]

Updates the access and modification times of [file] to the current time. If [file] doesn't exist, then an empty file is created with the name [file].

FOR THE REALLY LAZY: This command can be use for creating dummy files. These dummy files can then be filled with "echo [text] >> [file]" without opening a text editor.

-

This is the symbol for standard input. It is used more commonly with pipes (|). Some commands, like 'tar', need to have its input specified. So if the input is piped from another command, then '-' needs to be used to let 'tar' know that the input is from the pipe. I'm sure that you can think of some creative uses for this.

[command1]`[command2]`

This is NOT the apostrophe ('). It takes the output of [command2] and uses it as the arguments for [command1].

[command1] ; [command2]

Strings two commands together, running [command2] after [command1] is completed. More than two commands can be strung together this way.

[command1] && [command2]

Runs [command2] after [command1], but only if [command1] is successful. More than two commands can be strung together this way.

[command1] || [command2]

Runs [command2] after [command1], but only if [command1] fails. More than two commands can be strung together this way.

( [commands] )

Spawns a sub-shell, which doesn't hurt as much as it sounds like it should. Any commands between parentheses are run in their own shell. An example would be '(a ; b) & c' which would run 'a', then 'b' in a separate shell in the background, while running 'c' in the current shell in the foreground. Another application would be to use the output from all the commands in the sub-shell as input of another command.

nl (-bfhnp) [file]

Adds line numbers to a text file and sends the output to the screen unless it is redirected. Line numbers are added to each line and is reset to 1 at the beginning of each page. A page is defined by three parts, a header, a body, and a footer. The header starts with a line containing '\:\:\:'. The body starts with a line containing '\:\:' and the footer starts with a line containing '\:'. There are many more options than listed here, please see the man page for details.

-b [type]
-f [type]
-h [type]

determines how to number the body (b), header (h), and footer (f). The types are: a (all lines), n (no numbers), p [string] (only number lines containing [string]), and t (the default, numbers all lines with printable text).

-n [format]

formats the line numbers with ln (left justified with no leading zero), rn (the default, right justified with no leading zero), and rz (right justified with a leading zero).

-p

prevents reset of line numbers at the beginning of each page

wc (-cClw) [file-list]

Counts the number of character, words, and lines in [file-list].

-c counts just the number bytes
-C counts just the number of character
-l counts just the number of lines
-w counts just the number of words

POWER TIP: 'wc' can be used with 'ls' to see how many files are in a directory. Just enter "ls | wc -l".

join [file1] [file2]

Extracts and joins common lines from two files.

cut (-cdf) [file-list]

Extracts columns or fields from the files in [file-list]. Columns are specified with either the '-c' or '-f' options.

-c [list]

specifies a [list] of columns to cut at. For example, 'cut -c4,5,20 foo' cuts foo at columns 4, 5, and 20.

-d [character]

specifies [character] as the field delimiter for the '-f' option. The default is TAB. If SPACE is the delimiter, be sure to put it in quotes (-d " ").

-f [list]

specifies a [list] of fields to cut. For example, 'cut -f4-7 foo' cuts fields 4, 5, 6, and 7 from foo. 'cut -f5- foo' would cut from field 5 to the end of the line from foo.

paste (-ds) [file-list]

Attaches the lines of the files in [file-list] as the columns of the output. Thus, the first lines of the files in [file-list] are output as columns, separated by TABs, followed by the second lines and so on.

-d [character] specifies [character] as the delimiter for the columns
-s joins all the lines of one file into one long line. The [file-list] must have only one file.

uniq (-cdfsu) [infile] [outfile]

Removes and reports adjacent duplicate lines in [infile] and saves the unique lines and one copy of each duplicated line in [outfile].

-c shows the number of times a line is repeated
-d saves only one copy of each duplicated line
-f [number] ignores the first [number] fields of each line
-s [number] ignores the first [number] characters and any leading spaces of each line
-u saves only unique lines

Note that this only checks adjacent lines, so it is often useful to 'sort' the [infile] first.

cmp (-s) [file1] [file2]
zcmp (-s) [file1] [file2]

Compares two file, character by character. These are like 'diff' and 'zdiff', but compares byte by byte instead of line by line. 'zdiff' works on 'gzip'ped files by temporarily uncompressing them.

-s runs the command in silent mode

Normally, the output of 'cmp' and 'zcmp' lists where the differences are. In silent mode, these messages are suppressed. This is useful for connecting 'cmp' to another command.

One good use for these commands is to clean up your directory. You can compare two files and if they are the same, delete one. This can be done by entering "cmp -s [file1] [file2] && del [file2]". The '&&' will only delete a file only if 'cmp' finds no differences between the two files.

last (-hnt) (user)

List the last logins for (user) on the current machine. If no (user) is specified, then all logins are shown.

-h [host] specifies the host to show logins for
-n [number] shows the last [number] logins, instead of all logins since the last reboot are shown
-t [tty] shows the last logins at the terminal [tty]

tty

Displays the device name of the terminal that 'tty' is run in.

mesg [-ny]

Controls whether other users are allowed to send you 'write' messages. Note that turning off 'write' permission also disables 'talk' notification messages.

-n disables 'write' permission
-y enables 'write' permission

rusers (-ahilu)

Reports who is on each computer in the network. This is a lot like 'sacl' when used with the '-l' option, but is a UNIX command not a SACL system script.

-a reports on a machine even if no users are logged on
-h sorts the output alphabetically by host name
-i sorts the output by idle time
-l gives a more detailed output
-u sorts the output by number of users

xset (q)

Sets X window parameters. Use 'xset q' to see the current status of the variables. Settings which are changed only last for the current login session. Permanent changes should be made to the '.xinitrc' file.

tr (-d) [string1] (string2)

Translates [string1] to [string2] in the standard input. Thus, if I pass something to 'tr "[A-Z]" "[a-z]"', it will convert all uppercase letters into lowercase letters. Note that the input comes from standard input, so usually you will need to pipe something to 'tr'. Also, the output goes to standard output and will have to be redirected if it is to be saved.

-d deletes all characters in [string1] from the standard input, no (string2) needed

expand (-) [file]

Converts the TABs in [file] to SPACEs. Each TAB is replaced with 8 blank spaces unless otherwise specified.

-[number] replaces each TAB with [number]blank spaces

unexpand [file] {Solaris}

Replaces leading blanks in [file] into TABs.

gzexe (-d) [file]

Compresses an executable [file] and keeps it executable. There is a performance loss associated with this operation, but it saves a little disk space. When a compressed executable is run, it is uncompressed first.

-d expands the compressed executable [file]

fold (-w) [file]

Cuts long lines in [file] into shorter pieces. By default, lines are cut to be 80 characters long each. Note that a TAB is considered one character. Use 'expand' to convert TABs into SPACEs.

-w [length] cuts the [file] into lines of [length] characters each

comm (-) [file1] [file2]

Compares two sorted files line by line and displays the results in three columns. The first shows lines unique to [file1], the second shows lines unique to [file2], and the third columns shows lines common to both files.

-1 suppresses the first column (lines unique to [file1])
-2 suppresses the second column (lines unique to [file2])
-3 suppresses the third column (lines common to both files)
-12 displays only lines unique to [file1]
-13 displays only lines unique to [file2]
-23 displays only lines common to both files

newform (-abceilops) [file-list]

Manipulates text files by adding or removing spaces.

-a [number]

appends [number] spaces to the end of each line

-b [number]

deletes [number] characters from the beginning of each line

-c [character]

uses [character] to pad lines instead of spaces. This option is used in conjunction with the '-a' and '-p' options. It must come before them.

-e [number]

deletes [number] characters from the end of each line

-i [string]

expands TABs into [string]. If no [string] is specified, TABs are converted into eight spaces each.

-l [number]

extends each line to [number] characters long. It should appear before any other options that pad lines or delete characters.

-o [string]

converts [string] into TABs

-p [number]

adds [number] spaces to the beginning of each line

-s

strips leading characters of each line up to the first tab. If used, this option must be first.

 

pr (-adfFhlmnoprstw) [file]

Formats text files into a specified format. By default, [file] is formatted to 66-line pages with a five line header and a five line footer. The header contains the page number, the filename and the file's date and time.

+[number] begins the formatting with page [number]
-[number] format the output into [number] columns
-a outputs in two columns, truncating lines that are too long
-d double spaces the output
-f spaces out new pages with form feeds rather than blank lines
-F wraps lines to avoid truncation
-h [string] uses [string] as the header
-l[number] sets the page length to [number] lines. The default is 66.
-m merges up to eight files, printing each file in a separate column
-n[character][number] adds [number] digit line numbers separated from the text by [character]
-o[number] offsets each line by [number] spaces
-p pauses output to the screen after each page
-r suppresses error messages
-s[character] uses [character] as the column separator
-t suppresses headers and footers
-w[number] sets the line length to [number] characters. The default is 72.

umask [file-permission]

Sets the default file permission for new files. Without any arguments, 'umask' displays the current file creation mask. The system subtracts this mask from the system default permission to determine the file permission for new files created by you. By default, the system sets 666 for new files and 777 for new directories. If 'umask' is set to 002, then your new files would have 664 file permission (read and write for user and group, read only for everyone else.)

at (-mlr) [time] [commands]

Runs [commands] at [time]. [time] is specified by a mandatory hour, an optional minutes, an optional date, and an optional increment. The hour is specified with a two-digit number. Minutes can be added to the hour with or without a colon (e.g. '1320' or '13:20'). The time can also be either 'noon', 'midnight', or 'now'. 'am' and 'pm' can also be added to the time. The date is specified by a three-letter month name, followed by a date, and optionally a year (e.g. 'nov 13 1996' or 'nov 13' or 'nov 13, 1996'). The date can also be 'today' or 'tomorrow'. An increment to the specified time can also be added. The increment is of the form '+[number][unit]' where [unit] can be 'minutes', 'hours', 'days', 'weeks', 'months', or 'years'. The [number] can also be 'next'.

After entering "at [time]", hit ENTER, type in the commands to be executed, followed by "Ctrl-D". For example, I could enter "at 02:30 tomorrow +3minutes [ENTER] lpr foobar [Ctrl-D]" to print out 'foobar' at 2:33 am tomorrow.

-m sends mail to you after the job is complete
-l [jobs] reports on [jobs] or all jobs if [jobs] is omitted
-r [jobs] removes [jobs] or all jobs if [jobs] is omitted

The 'at' command is very useful for scheduling big jobs to run at times when the system is in low demand.

atq (-cn) [users]

Shows the jobs in the 'at' queue for [users] in the order in which they are scheduled to run. If no [users] are specified, all jobs are shown.

-c sorts the jobs in the order that they were submitted
-n displays the total number of jobs in the queue

atrm (-i) [jobs]

Removes your 'at' [jobs] from the queue. If [jobs] is not specified, all of your 'at' jobs are killed. The [job] number can be obtained from 'atq'.

-i prompts you for verification before removing [jobs]

sleep [time]

Pauses the system for [time] seconds before executing a command. This is usually used with '( )' and ';' to run something with a little delay. For example, '( sleep 120 ; more foobar )' would display 'foobar' after two minutes.

sed (-ef) [file-list]

Takes each line of the files from [file-list], performs some operations on it and changes the file itself. The editing commands to be performed are placed in a script. Each line of the script has the form '[line1](,line2) [function] (arguments)'. This performs [function] on the lines from [line1] to [line2]. The function can be any of the valid functions for 'awk' (see a reference book or the man pages). This is actually a very powerful command. Refer to the man pages for details.

-e applies to f
-f [script] applies the commands in [script] to [file]

awk (-f) ('script') (var=value) [file-list]

Applies the text manipulation commands in a script to the files in [file-list]. The script can contain variables defined when 'awk' is called and can also do some very fancy things. The script can include functions such as mathematical formulae and text manipulation functions. This command is very similar to 'sed', but doesn't change the original file; instead, the output is sent to the screen and should be redirected (see '>') if it is to be saved. 'awk' is far too complex to describe briefly here. See the man pages or a reference book for a detailed explanation of 'awk'.

-f [script] specifies the script to run if it isn't listed on the command line

There are also several other key UNIX programs which are almost standard UNIX that either are not used very often at SACL or require more explanation than I have space for. Some of these will be explained in other documents.

Compilers: 'cc', 'f77', 'gcc', and 'gdb'
Mail Handlers: 'elm', 'mail', 'mailx', 'mailtool', 'pine', and 'xmh'
Text Editors: 'ed', 'emacs', 'ex', 'textedit', and 'vi'
Text Formatters:
¡¡'checknr', 'eqn', 'nroff', 'tbl', and 'troff' are related
¡¡'bibtex', 'dvips', 'latex, and 'tex' are related
Others:
      'archie' - a search engine for files available by anonymous ftp
      'perl' - a text processing language related to 'awk' and 'sed' but even more powerful
      'tcl' and 'tk' - programming toolkits for graphical user interfaces (GUIs)
      'xrn' - an X-based newsgroup reader
      'xv' - an image viewing program