Unix Command Summary

update Sept. 4, 2016


This page is only intended as a quick reference summary, rather than an exhaustive description of Unix.



All Unix commands take the following form:

command options filenames

Options begin with a dash. Usually, several options can be combined.

Examples:

ls -l *.txt
cp -p file1 file1.bak
rm file1.old

Return to Using Unix


STARTING AND FINISHING YOUR SESSION

Logging in and logging out

login: userid
password: password

logout
ends session

To change password

 
passwd

Hints: 1. The passsword should be a mixture of letters (upper and lowercase), numbers and symbols. It should not be a word that would occur in a dictionary.
2. Write down the password and keep it in a safe place!

ENVIRONMENT VARIABLES

Environment variables hold values such as the paths to files or directories, or parameters required by a program. The names of environment variables begin with a dollar sign ($). Environment variable names may be either upper or lowercase.

$HOME
$PWD
$PRINTER
$DISPLAY

BIRCH=/home/psgendb/BIRCHDEV
export BIRCH
echo $BIRCH
cd $BIRCH


EDITOR=nedit
$EDITOR
User's home directory
Current working directory
User's default printer
the screen to which a window should be dislpayed
sets BIRCH to '/home/psgendb/BIRCHDEV'
prints the value of $BIRCH
changes current working directory to
'/home/psgendb/BIRCHDEV'. Export causes BIRCH to be used by all child processes of the current shell.

sets EDITOR to 'nedit'
launches the 'nedit' program

Hints:



FILE OPERATIONS

To list files in a directory

ls
ls -l
ls -la
ls -l filename
ls -l *.fsa
ls -l $HOME
ls -l $HOME/*.gen
list files in current directory
list files in long format
list all files, including hidden files
list a specific file
list all files with the .fsa extension
list all files in home directory
list all files in home directory with the .gen extension.

Hints: It is best to make a habit of using 'ls  -l', which gives more complete information.


To change to a new working directory

cd directoryname 
cd
cd ~
cd ..
cd $BIRCH/doc
move to a new directory
move to home directory
move to home directory
move up one directory
move to the directory named 'doc' within the directory
pointed to by $BIRCH

Hints: The chacter '~' refers to the $HOME directory. '.' refers to the current directory. '..' refers to the parent directory.


To send output to the screen or to a file

echo "Just some words" 
echo "Just some words" > file1.txt
prints message to the terminal
writes the message to a new file called file1.txt

Hints: The chacter '>' redirects output of a command to a file.


To show the contents of a file

cat file1.txt

prints contents of file1.txt to the terminal
writes the message to a new file called file1.txt


File permissions

  permissions
dir group #links size date/time name
user all owner
-rw------- 1 frist 3865 Oct 28 12:12 soycab3.gen
drwx------ 2 frist 512 Nov 3 1992 sv40

Hints: Fields are produced by the ls -l command are described below:

Directory: d if it is a directory, - if it is a file

Permissions, read (r) write (w) and execute (x). The set of three permissions refers to the owner. The second refer to the group. The third refer to the world (ie. all users on the system). In the example above, all files are readable and writeable by the owner, but group and world have no permissions. Note that directory 'sv40' is executable. To search (eg. list files in) a directory, you must have execute permissions for that directory.

Owner: owner of the file or directory

Size: size of file or directory in bytes

Date/time: data and time file or directory was created

Name: name of file or directory


To change permissions for a file

chmod a-w filename 
chmod u+w filename
chmod a+r filename
chmod a+r *

chmod a-r *


chmod a+rx directory

chmod -R a+r directory
write protect, world
unprotect, user
make the file world-readable
make all files in current directory world-readable

make all files in current directory not readable by
other users on the system.

make a directory world readable and searchable

recursively descend a directory structure, makeing all
files and directories world readable (Note: this will not
make subdirectories world-searchable!)

Hints: Permissions can also be set using three-bit code to specify read, write and execute permissions for all users. Type 'man chmod' for a complete description.


To copy a file

cp originalfilename newfilename
cp -p file1 file2

cp -pR olddir newdir

cp dir1/* dir2
copy a single file
copy a file, preserving its properties
(eg. date of creation, owner)
recursively copy a directory structure,
preserving file properties
copy all files from directory dir1 to
directory dir2 

Hints:


To remove a file

rm filename
rm -rf directory
remove a file
recursively remove all files and directories
within directory (DANGEROUS!)

Hints: Remove can not be undone


To rename or move a file

mv oldfile newfile
mv file1 directory2
rename a file
moves the file into directory2

Hints:



PRINTING

To print a file at the lineprinter

 lpr filename
lpr -Pmh114_p2 filename
prints at your default printer
prints at lineprinter mh114_p2

Hints: Only ASCII text files and PostScript files (.ps) can be printed directly to a printer using lpr. Other files, such as spreadsheets and wordprocessor documents must be printed from within the application.


VIEWING AND EDITING DATA

To edit an ASCII text file

 gedit filename 
nedit filename
pico filename
vi filename
GNOME graphic text editor
powerful graphic text editor
EASY text-based editor
SOPHISTICATED text-based editor

Hints: A text editor is essentially a word processor specialized for viewing and modifying ASCII text. ASCII text files contain nothing but charcters. There is no special formatting, such as underlining, bold type, pagination, embedded figures and tables etc.


To view a file on the screen

less filename 




cat filename

head -n filename
tail -n filename
view a screenful at a time. <spacebar> moves down one page;
ctrl-F moves forward a page at a time; ctrl-B moves
backward a page at a time; q exits the program. Type 'h'
for a help screen.

writes the whole file out.

prints the first n lines of a file to the terminal
prints the last n lines of a file to the terminal

Hints: head and tail are the fastest way to find out what is in a very large file, such as a file containing hundreds of thousands or millions of sequencing reads. Many text editors with will not be able to load such large files, or will take a long time doing so.



RUNNING PROGRAMS

To run any program

If the program is found in one of the directories specified in $PATH, simply type the name of the program. If the executable is not in the $PATH, you must specify the path to the program.

numseq

/usr/local/bin/xv


firefox &

ctrl-C
run a program called 'numseq'. 

run a program called 'xv', found in the
/usr/local/bin directory

run firefox in the background

Terminate the current program, if it was not
launched in the background.

Hints:


Redirection of input and output

Many programs take input from the standard input, and send output to the standard output. Input and output can be redirected to and from files, and piped from one program to the next

> redirect output to a file

< take input from a file

>> apend output to the end of an existing file

<< take input until ctrl-D is encountered

| pipe output from one program to another program

echo "Are we having fun yet?" 



echo "Are we having fun yet?" > junk


cat junk
cat < junk


Given the a file called 'names'
containing the lines:
Mary
Jane
Anna
Susan

cat names | sort

produced the output:
Anna
Jane
Mary
Susan
writes message to the standard output (ie. screen)
creates a file called junk, and writes the message
 to that file.

writes the contents of junk to standard output,


In each case the output would be

“Are we having fun yet?”








This command pipes the output from the cat
command to the sort command, which writes
to the standard output.


Listing  and terminating jobs running on the host to which you are logged in


ps
ps -u userid

kill pid
kill -9 pid

top
list all jobs running in the current shell
list all jobs belonging to 'userid'

kill the job whose process id is 'pid'
-9 switch sometimes needed to kill a job

opens a screen listing the jobs using the most
resources. The screen is updated every few
 seconds in real time.

Hints: These commands only work for a single login host. For example, if you have jobs running on two hosts eg. antares and deneb, and you are logged into deneb, ps or top will only show jobs running on deneb. To see jobs running on antares, you must log into antares.



ONLINE DOCUMENTATION

Online manual pages

man command


man -k topic
view the manual pages for a command,
using less

list all commands related to 'topic'

Hints: All Unix commands have man pages. Many, but not all other applications have man pages.



USING THE NETWORK


To transfer files across the network

ftp hostname



ftp ftp.ncbi.nih.gov


sftp userid@hostname

gftp
opens an interactive session to another host. This protocol
should only be used when security is not important
eg. when loging in as user 'anonymous'.

Opens a session to NIH anonymous FTP server.


Opens an FTP session using the SSH encryption protocol.

graphic FTP program

Hints: Type ? to see a list of commands in ftp and sftp.


Login to a remote host

ssh hostname
ssh -l userid hostname
ssh -l frist jupiter.cc.umanitoba.ca

ssh ccl.cc.umanitoba.ca
logs into host under your current userid
logs in as user 'userid'
logs into Univ. of Manitoba host jupiter as user
frist
logs into a randomly-chosen login host at
Univ. of Manitoba.