Unix Notes

A Unix shell guide can be found here

by Pierre Fromager

This page is for the novice or intermediate UNIX user and gives a brief description of each command and some examples to clarify its use. For more detailed information, refer to the man pages.

A intermediate user of UNIX should know all of the things mentioned in the index below. UNIX has many more commands and the commands listed have many more options which have been ignored for simplicity.


Hidden Files | syntax | Redirection | a2ps | cal | cat | CC | cd | chmod | cp | date | diff | du | elm | finger | ghostview | grep | head | ln | logout | lp | lpstat | ls | man | mesg | mkdir | more | mv | od | passwd | pr | ps | pwd | quota | rm | rmdir | scripts | setenv | sort | tail | talk | tin | who | wc

For corrections, please contact ScienceIT


 

 

Hidden Files.

Files starting with a dot (“.”) are hidden files. Doing ls will not show these files. You must type ls -a. Here are some examples.

.cshrc C-Shell personalised command set.
Edit or create this file as a normal text file. The commands entered will take effect whenever a new session (Window) is started. Type source .cshrc to effect the changes immediately.
.login Same as .cshrc but only for the first process. Type source .login to effect the changes immediately.
.signature If it exists, its contents will be included at the bottom of every outgoing mail message (before you edit it).
.plan If it exists, its contents will be displayed after the usual “finger” information when another user issues the ‘finger’ command on your user_id. Make sure the permission on the file is readable by all.
(ie. chmod 644 .plan).

 


Some UNIX command-line syntax:

; A semicolon separates two commands on the same line. eg. cd mydir; ls -al
& Used after a command, an ampersand tells the system to run the command in the background and return immediately for a new command. eg. xclock & (opens a clock window)
| Used between two commands, the pipe sends the output of one command into the input of another. eg. cat myfile1 myfile2 | lp (not to be confused with redirection)
* Means “any string of 0 or more characters”. eg. cp * Backup Copy all files in current directory to Backup directory.
? Means “any string of exactly 1 character” eg. cp prog?.c Backup Copy all files in current directory, such as prog1.c, prog2.c, etc, to Backup directory.
[character_list
or range]
Matches 1 character to character_list or range
eg. a[0123].cpp Matches all files a0.cpp, a1.cpp, a2.cpp or a3.cpp
eg. a[0-3].cpp Same as above

Redirection:

  • command > file_name Send output to file_name instead of stdout
  • command < file_name Take input from file_name instead of stdin
  • command >> file_name Append output to file_name instead of stdout

Example:
fred < input.txt > output.txt
Run fred using input.txt instead of the keyboard and send output to output.txt instead of the screen.

 


Alphabetic list of Common UNIX commands

^c

Stops program execution

 

^z

Pauses program execution. Type bg to continue in background or fg to resume in foreground. See “ps” command to keep track of background processes.

 

^s

Stop display of long output. See ^q.

 

^q

Restart display after a ^s.

 

&

Run command in background.

Example: xclock &
Run xclock program but return a prompt.

 

 

a2ps

a2ps <options> file_name | lp

Convert text file to postscript format.

Options: 

-1
One page per sheet.
-2
Two pages per sheet.
-p
Portrait (prints landscape if 2 pages per sheet is requested)
-n
Print line numbers.
-ns
Do not print page borders.
-Fn
Set the font size. (n is normally 8 but use 10 for a little bigger)
-H’text’
Print ‘text’ as header on every page.

Example: a2ps -2 -F8 -H'Assigment 1' myfile | lp

 

 

cal

cal [month] [year]

Display a monthly calendar.

month:
1 to 12 (January to December)
year:
1 to 9999 (0001 to 9999) – do not abbreviate.

Example:

pompeii:~:1 % cal
   April 1997
 S  M Tu  W Th  F  S
       1  2  3  4  5
 6  7  8  9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30

pompeii:~:2 % 

 

 

cat

cat <options> file_list

Join files together (conCATenate)

Options: 

-n
Display lines’ numbers at left of each line
-s
Compress multiple empty lines into one empty line.

Example:
cat A/a B/b > C/c Copy file “a” in directory “A” into file “c” in directory “C”. Then, append file “b” in directory “B” to the end of file “c” in directory “C”.

 

 

CC

C++ compiler. (Use lower case ‘cc’ for straight ‘C’ code.)

Examples:CC fred.c
Compile program fred.c. Executable will be in a.out.

CC -o fred fred.c
Compile program fred.c but let executable be fred instead of a.out.

CC -o fred.o fred.c
Compile fred.c but do not link to executable stage. Only create object file fred.o.

CC fred.c >& output_file_name
Compile fred.c and send compilation error messages to output_file_name.

 

 

cd

Change directory.

Examples:

cd / Root directory

cd fred Change to directory fred from current directory.

cd .. Parent directory

cd Your home directory

 

 

chmod

chmod mode file_list

Change the mode or access permissions of a file.

To see the current permissions, use ls -l. You will see something like this:

drwxr-xr-x   2 s9999999 3ug          512 Mar  3 13:58 fred.c

The characters on the left hand side are the permissions. The first character is a ‘-‘ if it is a file, ‘d’ if it is a directory. The next 9 characters are the read (‘r’), write (‘w’) and execute (‘x’) permissions for the owner, the owner’s group and the world (everyone else).

drwxrwxrwx
1222333444
	1: Directory indicator: d for directory, - for ordinary file
	2: Owner permissions:	r for read-access, - otherwise
				w for write-access, - otherwise
				x for executability, - otherwise
	3: Group permissions: ditto
	4: Others' (world) permissions: ditto

The mode can be either a three character code or a three digit octal code. The three character code consists of:

  1. who        ==>u for user, g for group, o for others or a for all.
  2. operation	==>+ for adding permission, - for removing permission
  3. permission	==>r, w, x as above.

The three octal digit code sets all 9 permission bits at once, counting them as bits. That is, 700 sets the first 3 bits to 1 and all others to 0. In effect, this gives the owner of the file read, write and execution access and denies these to everyone else.

Examples:

chmod 711 html Set permissions to 111001001, ie rwx–x–x, ie Owner can do anything, including executing this file, and others can execute but not read or change this file. This is commonly used for html directories. Opening a directory is, in effect, executing that directory.

chmod 644 index.html Set permissions to rw-r–r–, ie Owner can read or write the file and all others can read it only. This is commonly used for html files.

chmod u+x index.html Add execution permission to “index.html” file.

 

 

cp

cp <options> source_file destination_file

Copy one file to another.

Options: 

-i
Interactive – asks for consent before proceeding with each file.
-r
Recursive. Allows copy of directories and their contents.

Examples:

cp file1 file2 Creates a copy of file1 in file2.

 

 

date

Display date and time. A possible option is “-u” giving the Universal Time (Greenwich Mean Time).

Examples:

  pompeii:~:1 % date
  Mon Jun  2 16:10:50 EST 1997
  pompeii:~:2 % date -u
  Mon Jun  2 06:12:56 GMT 1997
  pompeii:~:3 % 

 

 

diff

Checks for differences between two files.

“diff” outputs the differences:

<
First file lines
>
Second file lines
n1 a n3,n4
append lines n3 to n4 after line n1
n1,n2 d
delete lines n1 to n2
n1,n2 d n3,n4
replace lines n1 to n2 by lines n3 to n4

 

 

du

du <options> [dir_list] [file_list]

Displays disk usage in blocks of 1000 bytes (not 1kB). eg 129809 is shown as 129!

-a
Displays information for all files and directories.
-s
Display only a total for directory of files listed.

 

 

elm Electronic mail facility. (see also “pine”)

-f
use the file_name specified. (usually “Mail/received”)

eg. elm -f Mail/letters Opens up the folder “letters”.

in “elm”, each message has a status on the left hand side:
Status field = N (new), O (unread), D (to be deleted)

To read a message, press “Enter”. This displays the contents of the current message. If it does not fit on a page, press “space” to scroll a page at a time. When finished reading press “i” to return to the index. “d” deletes the current message, “q” exits elm.

To compose a new message, type “m” for mail.

If you receive a message and want to reply to the sender, just type “r”.

 

 

finger

finger [user_id]

Displays details of all users currently logged on. If a user id is specified, That user’s details are displayed, including whether they are logged on, whether they have unread mail and their plan file.
You can just enter the user’s first or last name and, if found, the user id will be returned.

 

ghostview

ghostview [file_name]

“Ghostview” allows the user to see a postscript file which is not normally viewable with a standard text editor.

 

 

grep

grep <options> pattern file_list

(Global Regular Expression Print) Display lines which contain pattern in file_list.

Options: 

-i
ignore case
-n
precede each line by its line number
-v
reverse sense of test (display lines which do not satisfy pattern.
-c
count number of lines in each file that match the pattern.

Examples:

grep the *
Displays all lines in all files in the current directory which contain the three characters “the”.

grep "the dog" *
Use double quotes (“) for more complex expressions, especially if they contain a space.

 

 

head

head [-no_of_lines] file_name

Display specified number of lines (10 if omitted) from top of file_name. This would be used for very long files which might contain rubbish half way through the file. This means you can just look at the first 100 lines or so.

Examples:

head -30 fred.c
Displays the first 30 lines of “fred.c”.

 

 

ln

ln <options> directory_name pretend_path_name

Create a symbolic link. Allows renaming of long path names to a short name. It creates a file name in your pwd so the ‘ln’ command does not need to be in your .cshrc file. The link can be deleted just like any other file name.

Options: 

-s
Symbolic link eg. ln -s ~com224co/pracs c4p

 

 

logout

Logs the user out of the system. See also CONTROL_D (^d).

 

 

lp

lp <options> file_list

Send files listed in file_list to default printer.

Options: 

-dprinter_name
Print on the specified printer.

Examples:

pompeii:~:4 % lp test1
request id is e6a114-laser-18401 (1 file(s))
pompeii:~:5 %

pompeii:~:11 % lp -de6a119-laser test1
request id is e6a119-laser-18405 (1 file(s))
pompeii:~:12 %

 

 

lpstat

lpstat -o

Display print job queue.

Examples:

pompeii:a3:16 % lpstat -o
e6a114-laser-452 pompeii.ics.mq.edu.au!s3036050 2828 Jun 10 13:46 on e6a114-laser

Note the number (452) after the printer name (“e6a114-laser”). This is the Job Number and can be used to cancel the job.

 

 

ls

ls <options> [file|directory]

Display list of files in current directory.

Options: 

-l
Long – One line per file (gives more information)
-a
All – Display all files, including hidden files
-c
Create date – Sort by time of creation
-r
Reverse order
-t
Time modified – Sort by time of last modification

 

 

man

man [-sXX] command_name

Display system documentation relating to command_name. Some commands or functions are in different sections. Use the -sXX option to access the specific section, where XX is the section number. “man” will look for the first occurence of the command_name.

Examples:

man printf
Displays a description of the printf command from section 1.

man -s3S printf
Displays a description of the printf function in C. You can use “xman &” for a Xwindow version of “man” with a better index.

 

 

mesg

mesg <options>

Controls the “talk” command.

Options: 

blank
Displays terminal status (ie. yes or no).
y
Allow other users to “talk” to you.
n
Do not allow other users to “talk” to you.

 

 

mkdir

mkdir directory_name

Create a new directory.

 

 

more

more file_name

Display file on screen one page at a time.

<space>
Next page
<return>
Next line
<d>
>Next half page
<(n)b>
go back n pages
<(n)f>
go forward n pages
<q>
quit “more”

 

 

od

od <options> file_name

Display binary files in octal mode. This can be used to display the contents of a file which contains unprintable characters.

Options: 

blank
Display each byte’s octal value
-c
Display printable characters and use the C escape codes (eg. ‘n’ for 10) for non-printable characters.
-x
Display each byte’s hexadecimal value.

 

mv

mv <options> [old_path/]file_name [new_path/]/new_file_name

Move file_name file to new_file_name file. Can be used to rename a file or move a file from one directory to another.

Options: 

-i
Interactive – asks for consent before proceeding with each file.

 

 

passwd

Change your password

 

pr

Format a file to prepare it for printing.

Examples:
pr -2F in_file > out_file This creates out_file which is in_file in 2 columns. You can then send out_file to the printer. The number of columns can be changed.

pr -F file1 file2 > file1_file2_side_by_side This produces an output with file1 and file2 side by side. This makes it easier for comparing two similar files.

 

ps

Display list of current processes.

Examples:

pompeii:~:6 % ps
   PID TTY      TIME CMD
 17808 pts/135  0:00 ps
 11079 pts/135  0:00 tcsh-6.0
pompeii:~:7 % 

 

 

pwd

Present Working Directory (your current directory).

 

quota

quota -v

Display your current quota. You are normally allocated about 8MB of memory to work with. This command will show you how much you are currently using and how close you are to exceeding your quota.

Examples:

pompeii:~:1 % quota -v
Disk quotas for s9999999 (uid 6163):
Filesystem     usage  quota  limit    timeleft  files  quota  limit    
/disks/pompeii/home
                6667   8000   8500                829      0      0            
pompeii:~:2 % 

 

rm

Delete files in file_list.

 

rmdir

Remove empty directory

 

 

setenv

setenv PRINTER printer_name

Set printer_name as the default printer. “setenv” by itself will display all the environment variables which affect the behaviour of you terminal session.

 

 

sort

sort <options> [field_specifier_list] file_list

Sort files.

Options:(Fields are separated by blanks.)

field_specifier_list
+0 or missing: sort on first field
+1: sort on second field
-c
Check only if file is sorted or not
-d
Dictionary sort only alphanumeric characters (and blanks)
-f
Convert lowercase to uppercase.
-r
Reverse the order of the sort.

 

 

tail

tail [-no_of_lines] file_name

Display specified number of lines (10 if omitted) from bottom of file_name. This would be used for very long files which might contain rubbish half way through the file. This means you can just look at the last 100 lines or so.

Examples: tail -30 fred.c

 

 

talk

talk user_name

Visual communication with user_name. ^c by either party will terminate connection.

 

tin

News reader. Allows access to news-groups.

 

who

Displays information about who is using the system. (“w” works also).

 

 

wc

wc <options> file_list

Count number of words in the files in the list.

Options: 

-c
Characters only
-l
Lines only
-w
Words only

 

 

Shell Scripts

Edit a new text file and type commands as required. Then make this text file executable (see chmod). You can then type the file name by itself and you have thereby created a new command.

#
Tells the system to ignore text that follows (ie. a comment)
set variable = string_value
Set variable to a value
alias dummy_name real_command
Create new command from old.
alias dummy_name
Display real command corresponding to dummy_name
alias
Display aliases already set
unalias dummy_name
Remove alias dummy_name
echo message
Display message on screen (if message starts with dollar and it is not a variable, precede it with (slosh))
echo $variable
Display value of variable on screen
$HOME
Preset variable containing home directory.
$path
Contains path to be searched for commands
Rating: 5.0. From 1 vote.
Please wait...