Gnu/Linux Lol: University of Tehran
Gnu/Linux Lol: University of Tehran
March 8, 2007
Linux Architecture Shell: a software that provides a kind of interface to the end-user
two kinds of shell in OS
Command Line Interface (CLI)
command.com (DOS) cmd.exe (Windows) sh, bash, csh, ksh (Linux & Unix)
User Apps Shells System Calls Kernel Hardware
Shells CLI
faster that GUI on the same operations simpler in some cases good for administration (esp. remote) tasks and batch processing
GUI
user friendly more suitable for end-user and desktop
Filesystem Structure Files are organized into directories Files and directories are arranged in one big tree rooted at '/' A Hirarachy (Tree) of directories
Filesystem Hirarchy Standard
specifies the usage of major directories such as /bin, /lib, /usr, /etc, ...
... Filesystem Structure /bin/ : Essential command binaries for all users (e.g., cat, ls, cp) /boot/ : Boot loader files (e.g., kernels, initrd) /dev/ : Essential devices (e.g., /dev/null, /dev/modem, /dev/mouse) /etc/ : Host-specific system-wide configuration files /home/ : Users' home directories
10
/mnt/ : Temporarily mounted filesystems /proc/ : Virtual filesystem documenting kernel and process status, mostly text files (e.g., uptime, network) /root/ : Home directory for the root user /usr/ : Secondary hierarchy for user shareable, read-only data /var/ : Variable files, such as logs, databases, websites, and temporary e-mail files
11
Bourne Again Shell (Bash) A command interpreter Developed in GNU Project based on Bourne Shell, sh, in Unix Incorporate many useful features from Korn and C Shell (ksh, csh) Simple with many capabilities
12
Command interpreter
take a command and its arguments from input process the command line find the program execute the program
13
14
... Moving Around pwd : print name of current/working directory '~' : represent Home directory of user cd without any parameter : change to user home dir cd - : change to the last visited directory
15
Ctrl-U: Erase line before cursor Ctrl-H: Erase a character before cursor Ctrl-D: Exit shell Ctrl-C: Terminate a running program Ctrl-Z: Temporarily stop program, EOF character Ctrl-S: Start incremental command history search
16
... Working in Bash Ctrl-R: Start reverse incremental command history search up-down-arrow: Moving in command history TAB: Complete input of the filename to the command line Ctrl-V TAB: Input TAB without expansion to the command line Ctrl-Alt-Del: Reboot/halt the system
17
18
file file_name
Display a type of file for the file file_name
type commandname
Display information on command commandname
whatis commandname
Display one line explanation on command commandname
19
ls -a
List contents of directory (all files and directories)
ls -A
List contents of directory (almost all files and directories, i.e., skip ".." and ".")
ls -l
List contents of directory in long format
20
Colored output
ls --color=auto
21
... Basic Linux Commands clear : clear the terminal screen passwd : change user password
22
mkdir -p dir_path
create directory dir_path and all of its parents
rmdir dir_path
remove empty directory dir_path
rmdir -p dir_path
remove parents
23
cp [OPTIONS] SOURCE DEST [OPTIONS] SOURCE... DIRECTORY cp -r : recursive copy cp -v : verbos output cp -f : force overwrite cp -i : interactive mode, prompt before overwrite
24
rm -r : remove dirs and their content recursively rm -f : never prompt rm -i : prompt before removal rm -v : explain what is being done Attention: use this command with care when you are logged in as root
25
26
Viewing Content cat: concatenate files and print on the standard output
cat myprogram.cpp myprogram.h cat -n myprogram.cpp myprogram.h
number all output lines
27
Screen is the standard output for both output messages and error messages
standard output file descriptor number is 1 standard error file descriptor number is 2
28
Appending
command >> output.txt
30
apropos KEYWORDS
search the manual page names and descriptions
man -k KEYWORDS
equivalent to apropos
31
Wildcards Working with group of files and directories without typing all of them
*
This matches any group of 0 or more characters This does not match a filename started with "."
?
This matches exactly one character
[...]
This matches exactly one character with any character enclosed in brackets
32
[^...]
This matches exactly one character other than any character enclosed in brackets (excluding "^")
33
env
show all the environment variables
export ENV_VAR_NAME=value
create an environment variable with name ENV_VAR_NAME
34
Aliasing For commonly used commands you can set a shorter command_name using alias alias new_cmd_name='command [options] [args]' Example:
alias ls='ls -h --color=auto' alias la='ls -al' cls='clear'
35
Permissions Groups Owner and group of a file Access mode Changing the owner of files Changing the group of files Changing the access mode of files Adding user to a group
36
File attributes
Owner and group owner of the file File type (regular, directory, ...) Permissions Date and time of creation, last read and change File size Address of file on the disk
37
In every level there are some specific permission, read , write and execute Every file is owned by a specific user, owner of file group is a set of users and groups Every file belongs to a group
38
Groups There is a group associated with each user with the same name as user Groups in system
www-data, dip, lp, audio
39
For directory
read (r): to list contents of the directory write (w): to add or remove files in the directory execute (x): to access files in the directory
40
user
group
other
Access Mode
41
File Type regular file (-) directory (d) block special file (b)
block devices
42
owner of file
group of file
43
44
45
46
chmod -R
recursive ...
47
Programming
48
GUI editors
Kate Kwrite Emacs
49
vi
up k move around using arrow keys or backward h l Pressing the Esc key whenever put you in the normal mode. j Then you can issue commands down To exit go to normal mode and type ':q' To exit and discard changes, normal mode, then type ':q!' In normal mode if you type x it will delete a character To insert and edit, type i To undo changes Esc and then type u
forward
50
... vi To write changes to disk press Esc and then type ':w' To write changes to disk and exit press Esc and then type ':wq' To delete a line press Esc and then type 'dd' To go to the end of file press Esc and then 'G' To go to the beginning of file press Esc and then 'gg' To search a word place vi in normal mode and type '/' and then type your word to search and press enter.
to find the next occurrence of word press 'n' or 'N' for previous occurrence
51
Run
./myprogram
52
53
54
file1
Hard link
refer to the specific location of physical data
file1 data
file2 data
file2
55
... Linking In case of soft links if reference to data be removed, there would soft be no access to data link to file1 Not true for hard links
file1
file1 data
file2 data
file2
56
57
ls -L
show information for the file the link references rather than for the link itself
ls -H
follow symbolic links listed on the command line
cp -H
follow symbolic links
58
59
Extended information
specify -ls option find DIR -name PATTERN -ls
60
find DIR COND The COND part is a boolean expression expr1 -a expr2 expr1 and expr2 expr1 -o expr2 expr1 or expr2 ! expr not expr
61
62
grep -r string_to_find .
search content of all the files in the current directory recursively for string_to_find
-H -h -L -l
63
Regular Expression lOl regular expression, r, is nothing or a character concatenated with another regular expression Quantification
r?
zero or one occurrence of r
r*
0, 1 or any number
r+
1 or more
64
grouping
(r)
65
tail filename
output the last part of files
66
unzip file.zip
decompress file.zip
unzip -t file.zip
just test the file, no decompression
67
68
69
70
Processes A process is a running instance of a program, including all variables and other state Each Process has a Process ID, PID Each process is created by a parent process, Parent Process, PPID There is a process called init that is parent of all other processes This relationship between processes in system form a tree of processes
71
ps
show processes running
ps -A
show all the processes running in the system
ps -f
show full information
72
Signals A signal is an asynchronous event transmitted between one process and another SIGKILL : 9 SIGINT : 2 SIGTERM : 15
73
killall process_name
kill processes by name
74
... Processes A process that is running in background Lower priority In order to run a process in background
command &
top
another process manager
ksysguard
75
System and Hardware Information Proc Filesystem File Disk Usage System Disk Usage Date and Time
76
Proc Filesystem The /proc filesystem is a pseudo-filesystem and contains information about the system and running processes There is a directory associated with each process running Other information about system organized in files and directories
/proc/meminfo /proc/cpuinfo
77
du -h FILES
Summarize disk usage of each FILE, recursively for directories
du -s FILES
display only a total for each argument
du -h FILES --max-depth=<n>
show information for at most n level
78
df -h
report in a human readable format
79
You can't eject cdrom while you are using it First un-mount
81
82
USB Your memory stick will be considered as a USB mass storage device posing as a removable SCSI disk /dev/sda, /dev/sdb mount -t vfat /dev/sda1 /mnt/myflash
for a FAT32 flash memory
83
84
ssh user@host_name ssh [email protected] use `-v' option for verbos output Secure file trasfer
sftp user@host_name sftp [email protected]
85
Downloading wget : The non-interactive network downloader wget URL wget -r URL
download recursively
wget -nH -r URL Disable generation of host-prefixed directories invoking Wget with -r http://somwhere.com/ will create a structure of directories beginning with somwhere.com/
86
wget --convert-links -r -L
http://www.oreilly.com/catalog/debian/chapter/book/index.html
Retrieve only one HTML page, but make sure that all the elements needed for the page to be displayed, such as inline images and external style sheets, are also downloaded.
87
88