UNIX by Examples


Emner:

Listing and viewing files
Using directories
Manipulating files and directories
Editing files and controlling your terminal
Remote logins, file transfer and shells
Passwords, permissions and ownership
Using pipes, redirection and subshells
Printing
Finding strings and files
Compiling, executing and analysing programs
Job control
Disk usage
Filtering files
Finding out about files and commands
Finding out about yourself and other users

Listing and viewing files

Command Description
ls
ls -l
ls -a
ls *.f
ls data?.out
ls [a-h]*
ls ~
ls /usr/man/man1
cat myfile
more myfile
head myfile
tail -20 myfile
list the files in the current directory
list protection, ownership, data and size
list all files, including hidden files (i.e. .login)
list all files ending with .f
list all files with any character matching ?
list all files beginning with letter a through h
list the content of the home directory
list the content of the directory /usr/man/man1
list the content (catenate) of the file myfile
page the content of myfile, 'space' for next page, do 'man page'
list the first 10 lines of myfile
list the last 20 pages of myfile

Using directories

Command Description
pwd
mkdir work
cd work
cd work/quasar
cd
cd ..
rmdir work
list the name of the current working directory
make a directory called work in the current directory
move to work in the current directory
move to the subdirectory work/quasar
go back to the home directory (the top directory)
move to the directory above the current directory
remove the diectory work

Manipulating files and directories

Command Description
cp file1 file2
cp /pub/recipes/src/rkqc.f .
cp file ~
cp file mydir
cp -r dir mydir
cat file1 file2 > file3
rm myfile
rm *.o
rm -r mydir
rm -f myfile
mv file1 file2
mv file1 mydir
copy the file named file1 to the file named file2
copy file rkqc.f in /pub/recipes/src to the current directory
copy file named file to home directory
copy file to the directory mydir
copy the directory dir and everything in it to directory mydir
concatenate (copy together) file1 and file2 on file3
remove file myfile (must be a file)
remove all files ending with *.o
remove directory mydir and its content
remove file myfile, even when it is write-protected
move (rename) file named file1 to file2
move file named file1 into directory mydir

Editing files and controlling the terminal window

Command Description
vi myfile
vi -r myfile
emacs myfile
setenv TERM vt100
stty erase ^H
stty kill ^C
stty all
setenv DISPLAY neptun:0.0
edit the file myfile using the vi editor (find a vi manual)
edit the file myfile after an abnormal termination using vi
edit a file using the editor emacs (use the help option)
set terminal emulation to vt100. Other options hp, xterm
set backspace character to CTRL H (press this key)
set kill character to CTRL C
display all terminal settings
set output screen to the display in front of you (see label)

Remote logins, file transfer and shells

Command Description
telnet origo
rlogin origo



rlogin origo -l vp3
hostname
whoami
logout or CTRL D
exit
ftp da601.ifa.au.dk
rcp origo:mydir/myfile .

rcp myfile origo:mydir
rsh origo uptime
log into the host origo
remote login to origo. Terminal information passed and display.
A line should be present in the .rhosts file on the host giving your
user name and the host you come from to avoid the
need for a password.
login under the name vp3 on origo
display the name of the current host
display the current username
logout from the current host
exit from your current shell (command session)
perform file transfer between your curent host and da601
remote copy of the file file in mydir on origo to
current directory. .rhost file must contain proper permisions.
remote copy the opposite direction
run a remote shell (session) on origo. .rhost must be correct

Passwords, permissions and ownership

Command Description
passwd
chmod u+x my.exe
chmod ugo+r myfile
chmod u+rwx myfile
chmod go-rwx myfil
chmod ugo-w myfile
chmod -R go-rwx mydir
chown vp6 myfile
chown -R vp3 mydir
change your password
change the mode of file my.exe to make it executable
allow myfile to be read by user, group and others
make myfile readable, writeable and executable
make myfile accessible only to myself
prevent any writing to or deletion of myfile
make mydir and its content accessible only to me
change owner for file myfile to vp6
change owner of the whole directory mydir to vp3

Using pipes, redirection and subshells

Command Description
ls -l | more
ls -l | cat -n > ls_out
prog.exe < prog.in > prog.out >& prog.err

prog.exe >> prog.out
cat prog.in -| prog.exe

(date; prog.exe >& prog.out; date) > prog.log
list files in a directory, one page at a time. '|' is called a pipe
list the files, number each line and write to file ls_out
execute prog.exe. Take input from prog.in, write
output to prog.out and error messages to prog.err
append output to prog.out
copy prog.in to the standard input of prog.exe.
Then read the keyboard (-)
write start/stop time to prog.log, output/errors to prog.out

Printing (at this site)

Command Description
lpr -Phpnew myfile
lpr -Phplj4 myfile
lpr -Phpcol myfile
lpq -Phpnew
lprm -Phpnew 23
a2ps -Phplj4 myfile
print file myfile (postscript) on both side of paper
print file (postscript) on one page only
print file (postscript) on color printer
list print queue
remove job 23 (see lpq output) from print queue
print ASCII file (text file) single sided.

Finding strings and files

Command Description
grep FUNCTION *.f
find . -name "*.tex" -print

find . -mtime -2 -print
find . -name "*.f" -exec grep EXP {} \;

look for the string FUNCTION in all files ending with *.f
look for all files with names ending with .tex in current directory and below
find all files that have been modified within the last two days
find all occurences of the string EXP in *.f files here ('.')
and below

Compiling, executing and analysing

Command Description
cc -o prog.exe prog.c
cc -c prog.c
cc -lX11 -c prog.c *.o mylib.a
cc -g -o prog.exe prog.c
prog.exe
time prog.exe
nice prog.exe
cvd prog.exe core

ar cr mylib.a p1.o p2.o
compile and link prog.c. The executable is prog.exe
compile prog.c and place the object code in prog.o
compile prog.c and link with object files and library
compile with debug option -g
run the executable prog.exe
time the program prog.exe
run prog.exe with low priority
run the debugger (SGI only) cvd on prog.exe and
the core dump
create the archive (library) mylib.a with two object files

Job control

Command Description
prog.exe &
ps
ps -ef
kill -TERM 1342
kill -QUIT 1342
kill KILL 1342
renice -20 1342
^Z
bg
fg
start prog.exe in background
list your processes
display full list of all processes
tell process 1342 to terminate
tell process 1342 to terminate and create a core file for debugging
tell process to stop immediately (last possibility)
lower priority of job 1342
stop the current job
start the job that was stopped as a background process
start the job stopped as a foreground job

Disk usage

Command Description
du
du -s *
df -k
disk usage of current directory and subdirectories in kbytes
total disk usage of files and directories
display free disk space on all disks in kbytes

Filtering files

Command Description
paste file1 file2
cat file | sed 's/E012//g'

cat file | awk '{print $1+$2}'

cat file | awk '{if ($1>1.5) print $0}'
join corresponding lines from file1 and file2
remove every occurence of the string E012 from the file
using the stream line editor sed
print the sum of column 1 and 2 for the file
using the program awk
print all lines, where the first column is larger than 1.5

Finding out about commands

Command Description
man cc
man 5 passwd
man 3c intro
whatis ls
file ls
whereis ls
which ls
display the manual page for the command cc
give the manual page for passwd from section 5 (file formats)
give the introductory page from section 3c (C library routines)
give a brief introduction to all commands starting with ls
display the file type of ls
give the location of all files connected with ls
display the executable started by the ls command

Finding out about yourself and other users

Command Description
who
grep `whoami` /etc/passwd
finger hans@origo
finger @origo
find out who is logged on
display your entry in the passwd table
display information on users matching hans on the host origo
find out who is logged onto the host origo