Introduction to UNIX

Download Report

Transcript Introduction to UNIX

Introduction to Linux
©Colin Jamison 2004
Colin Jamison
Help on Unix Commands
©Colin Jamison 2004
• List all options available for a command
and also some example usage e.g.
• man command-name
• As for ‘man’ but the ‘info’ command has
more information and is up to date e.g.
• info command-name
UNIX Command Format
command <options> <arguments>
©Colin Jamison 2004
•
•
•
•
The first word is the command
Options follow commands
Commands/options/arguments are case sensitive
A command may or may not have options
$ ls
$ ls -al
• Arguments are normally files/directories
$ ls -al /etc
©Colin Jamison 2004
Directory and File Names
• Case sensitive
• Any character
• Avoid “ / * > space ! ”
– they are legal but their use is problematic with shells
• Must be unique inside its directory
• Wildcard
– *
any number of characters
– ?
any single character
– []
Any one of the characters inserted
between the brackets
Path Names
©Colin Jamison 2004
• Path name - is an address that uniquely
identifies a file or directory in the UNIX file
system.
• Absolute path name - always starts from the
root directory e.g. /usr/local/bin
• Relative path name - path relative to the
present working directory e.g. if pwd is /usr
relative pathname local/bin is equivalent to
the absolute pathname /usr/local/bin
Directory Navigation
• Changing directory
$ cd /usr
$ cd local/bin
$ cd ..
©Colin Jamison 2004
• Present directory
$ pwd
/home/cjamison/
• Creating/removing directories
$ mkdir directoryname
$ rmdir directoryname
Directory Contents
©Colin Jamison 2004
• File listing
$ ls
$ ls -a
$ ls -al
$ ls -F
- abbreviated list
- abbreviated list + hidden files
- long list + hidden files
- abbreviated list + file type
File listing
©Colin Jamison 2004
access permission
name
bash2.04$ ls -l
total
drwxr-xr-x
1 cjamison
-rwx--x--x
1 cjamison
-rwxr--r-1 cjamison
-rwxr--r-1 cjamison
-rwxr-x--x
1 cjamison
-rwx-----1 cjamison
-rwxr-x--x
1 cjamison
drwxr-x--x
1 cjamison
drwx-----1 cjamison
drwx-----1 cjamison
-rwxr-x--1 cjamison
-rw------1 cjamison
bash2.04$
date and
time
type
last modified
staff
staff
staff
staff
staff
staff
staff
staff
staff
staff
staff
staff
123 Dec 12 2003
514
Dec 24 2003
owner
2345 Jan 02 2004
group
231 Aug 12
2003
126 Dec 24 1990
732 Oct 23 2003
9470 Aug 26 2003
512 Dec 10 2003
512 Jan 16 2004
512 Sep 01 2003
8972 Aug 21 2003
42 Dec 24 2003
Aproject
Ascript
afile
checksum
dos2unix
dev.txt
excalibur
home
mp3
payroll
swallow
swift.txt
size
©Colin Jamison 2004
Access Permissions
• Access permissions on a directory
determine whether a file in the directory can
be renamed or removed
• File permissions determine what can be
done to the file contents
• To allow access to a directory, access
permissions should be set for all of its
parent directories all the way up to the root
directory
Access Permissions
• The first character indicates the type of file:
– directory (d)
– link (l)
-rwxrwxr-x
– plain file (-)

The rest, specify three types of users
 owner
 group
 others
©Colin Jamison 2004

who are allowed to
 (r)
read
 (w) write
 (x)
execute
©Colin Jamison 2004
Numerical Access Permissions
Character
-|rwx|r-x|r--|
Binary
|111 |101|100|
Octal
| 7 | 5 | 4 |
Protecting Your Files
©Colin Jamison 2004
•
•
•
•
•
-r-- --- ---rw- --- ---rw- r-- r--rw- rw- rwdr-x r-x r-x
(400)
(600)
(644)
(666)
(555)
• drwx --- --- (700)
• drwx r-x r-x (755)
• drwx rwx rwx (777)
protect it from accidental editing
owner can edit/read the file
owner can edit, others may read
everyone can edit/read
everyone can list but can’t
create delete/rename files
owner can do anything
owner can do anything, others can
read
anyone can edit/read/run
Changing Access Permissions
chmod permission files
©Colin Jamison 2004
• Character Method
$ chmod a=r-x filename
$ chmod u=rwx filename
$ chmod go+r filename
$ chmod ugo-w filename
• Numerical Method
$ chmod 755 filename
$ chmod 600 filename
Viewing and Editing Files
• Listing files
– cat
– tail
e.g. cat filename
e.g. tail -f filename
• Listing files one screen at a time
– more
©Colin Jamison 2004
• Editing files
– vi
– emacs
– GUI tools
e.g. cat filename | more
File Manipulation
• Copy
– cp filename1 filename2
• move (rename)
– mv filename directory
• remove (delete) - be careful!
©Colin Jamison 2004
– rm filename
Listing Processes
©Colin Jamison 2004
• List processes e.g.
• ps
• ps -ef
©Colin Jamison 2004
Processes - Foreground v
Background
• To run a process as a background process:
• process-name &
• To examine foreground/background
processes use the jobs command
• fg
• bg
• Each process has a unique process id (PID)
Disposing of Rogue Processes
©Colin Jamison 2004
•
•
•
•
The kill command
kill parameter PID
parameters ‘-1’ to ‘-9’
PID available from one of the process
commands
Redirection (1)
©Colin Jamison 2004
•
•
•
•
•
•
ls -al > lsoutput1.txt
ls -al >> lsoutput2.txt
file descriptors
standard input 0
standard output 1
standard error
2
©Colin Jamison 2004
Redirection (2)
• Redirect standard error to a file
• ls -al > lsout.txt 2>lserror.txt
• redirect standard output and standard error to the
same file
• ls -al > lsout.txt 2>&1
• Redirecting input
• more < lsout.txt
• To discard standard output and standard error
• ls -al > /dev/null 2>&1
Pipes
• Simplify redirection e.g.
• ps | sort | more
©Colin Jamison 2004
• ps | grep ‘sh’ > listshells.txt
‘C’ Tools (gcc)
• gcc
- GNU C/C++ compiler
• gcc filename1 … filenameX -o programfilename
• most commonly used options
©Colin Jamison 2004
•
•
•
•
[-Idir...] search dir for header files
[-Ldir…] include dir to search for library ( -llibrary )
[-o outfile]
[-ggdb] include gdb debugging information
‘C’ Tools (gdb)
• gdb - GNU debugger
• gdb programfilename
• most commonly used options
©Colin Jamison 2004
•
•
•
•
run
break [file:]function
next
step c
edit [file:]function
list
help command
quit
print expr
©Colin Jamison 2004
Questions ?