Вы находитесь на странице: 1из 88

GNU/Linux lOl

March 8, 2007

University of Tehran Linux Workshop


ACM Student Chapter

Steps Install Linux! Go into the Linux and walk around!


How can I change this or move that? I need HELP! Where is my pen? How can I change my key to this home? Search and find and view ..... It takes a lot of space! Any compressor? Who are working on this system? Who uses the CPU? What's the time? I want to use my USB, CD?

I want to write my own program!


2

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

Graphical User Interface (GUI)


CDE KDE GNOME XFCE

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

Managing and Displaying Windows X Window System


Started at MIT Basic windowing services
drawing moving windows on the screen interacting with input devices such as mouse, keyboard, touchpad

Follows a client/server architecture


protocol X11

No specification for the User Interface

User Interfaces Many Window Manager and UI


KDE; the most popular GNOME Xfce twm

They use X services to create a suitable GUI for user

File System One of most important part in an OS


Responsible for organizing, storing and accessing data on/from the storage media Directories to associate files with file names

File systems in Linux


XFS Ext2, Ext3 ReiserFS GFS, GFS2 NILFS, ...

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

/lib/ : Libraries essential for the binaries in /bin/ and /sbin/

10

... Filesystem Structure

/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

Programs & Commands Commands are programs installed


except shell specific command

Command interpreter
take a command and its arguments from input process the command line find the program execute the program

13

Moving Around cd destination_dir_path : change directory to destination_dir_path


shell specific command '.' represents current directory '..' represents parent directory destination_dir_path
relative : respect to current location in the file system absolute : a path starts with '/' /usr/local/Adobe-7.0

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

Working in Bash Typical bash Prompt


[username@machine-name:current-dir]$

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

Command Overview Syntax


command-name [OPTIONS] [FIELDS]

Case sensitive executing multiple command


command1; command2; ...

18

Basic Linux Commands whoami


Display current user name

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

List of Files and Directories ls


List contents of directory (non-dot files and directories)

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

... ls ls [OPTIONS] [FILES]* Combining options


ls -Al

Colored output
ls --color=auto

Other useful options


-S : sort by file size -r : reverse sort result -R : list recursively -1 : one file per line -d : directory entries instead of content

21

... Basic Linux Commands clear : clear the terminal screen passwd : change user password

22

Files and Directories Manipulation mkdir dir_path


create directory dir_path dir_path can be absolute or relative

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

Copying touch FILES


change the modification time of each file to current time or create a file if not exists

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

Removing rm [OPTIONS] FILES


remove files and directories

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

Moving mv [OPTIONS] SOURCE DEST [OPTIONS] SOURCE... DIRECTORY


rename or move files or directories

-f : force overwrite, no prompt -i : interactive -v : verbos output

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

less myprogram.cpp less -N myprogram.cpp


number lines

Or you can use more


more file_name
use space to scroll down

27

Standard I/O Keyboard is the standard input


file descriptor number is 0

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

You can change the standard input by using <


cat < file

Changing the standard output


ls -al > file_list.txt
redirect the standard output of command `ls -al' to file `file_list.txt'

28

... Standard I/O Changing the standard error


ls -al 2> error_messages.txt

Redirecting output and error separately


command 1> stdout.txt 2> stderr.txt

Redirecting both standard output and error


command &> output-error.txt

Appending
command >> output.txt

Feeding the whole output of ne program to another program


command1 | command2 ls -al | less
29

Getting Help man commandname


show the manual for commandname

man systemcall_function man library_function man special_file_name manual sections


1 2 3 5 : Executable programs or shell commands : System calls : Library functions : Special files

30

... Getting Help man [section] page


look for manual with name page in section

apropos KEYWORDS
search the manual page names and descriptions

man -k KEYWORDS
equivalent to apropos

commandname --help commandname -h

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

... Wildcards [a-z]


This matches exactly one character with any character between a and z

[^...]
This matches exactly one character other than any character enclosed in brackets (excluding "^")

33

Environment Variables $PATH $HOME echo $ENV_VAR_NAME


show the value

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'

Unalias: remove an alias or all of them


unalias cls unalias -a
removes all the aliases from the current session

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

... Permissions Why permission?


control access of users and groups to the files and directories

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

... Permissions Three levels of permission


user (u) group (g) other (o)

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

/etc/group adduser username groupname


add user to group

deluser username groupname


remove user from group

39

Specific Permissions For file


read (r): to read contents of the file write (w): to modify the file execute (x): to run the file as a command

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

... Specific Permissions

user

group

other

Access Mode

41

File Type regular file (-) directory (d) block special file (b)
block devices

character special file (c)


character devices

symbolic link (l)


reference to another file

named pipe (p) domain socket (s)

42

file type file permission file size modification date

owner of file

group of file

43

Changing Owner chown new_owner file_dir_name


change the owner of file_dir_name to new_owner new_owner is the name of a user in system

chown -R new_owner dir_name


recursively change the owner of all files and directories in dir_name to new_owner

44

Changing Group chgrp GROUP FILES


change the group of FILES to GROUP GROUP is the name of a group in system

chgrp -R GROUP FILES


recursively change the group of all files and directories in FILES to GROUP

45

Changing Access Mode chmod [OPTION]... OCTAL-MODE FILE...


OCTAL-MODE is mode in base 8
644 -> rw-r--r-753 -> rwxr-xrw-

chmod 753 myfile.sh

chmod [OPTION]... MODE FILE...


MODE
[ugoa...][+-=][rwxX...]

chmod u+w file


gives write permission to user

46

... Changing Access Mode chmod u+rx,go-wx file


give read and execure permissions to user and revoke write and execute permissions from group and other

chmod a+r file


give read permission to all (user, group and other) equivalent to chmod ugo+r file

chmod -R
recursive ...

47

Programming

48

Editors Text mode editors


vi nano

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

Compiling and Running Compile


g++ myprogram.cpp -o myprogram To get all warnings use -Wall g++ -Wall myprogram.cpp -o myprogram

Run
./myprogram

52

Make For faster build process There are some targets


target has dependencies target has some commands to be executed

53

Working with Files Linking


What is a link?
a reference to a file or directory in the filesystem more than one location refer to the same information

Two types of links


symbolic links or soft links hard links

54

Linking Symbolic link


refer to a symbolic path indicating the abstract location of another file
soft link to file1

file1

Hard link
refer to the specific location of physical data

file1 data

hard link to file2

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

hard link to file2

file2 data

file2

56

... Linking How to create soft links?


ln -s TARGET LINK_NAME
TARGET could be a directory or a file

How to create hard links


don't specify -s option ln TARGET LINK_NAME
create hard link LINK_NAME to TARGET

hard link to directories is not allowed

57

... Linking Behavior of other commands with links


Options to follow links or show information about where links refer to

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

Finding Files find


a program to find files find all the files matching a criteria under the specified directory

find . -name "my*"


find all the file start with my (case sensitive) under current directory

find /usr/share/apps/ -name "g[tn]*"


find all the file under /usr/share/apps start with gt or gn (case sensitive)

59

... Finding Files Find file only:


find DIR -name PATTERN -type f f : regular file c : character device file b : block device file d : directory l : symbolic link

Extended information
specify -ls option find DIR -name PATTERN -ls

60

... Finding Files Complex Criteria


find /usr/share/apps/ \( -name "k[n]*" -a -name "*.jpg" \) -type f

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

... Finding Files Executing a command on every file found


find DIR COND -exec command {} \;

62

Searching Content of Files grep <option> <regularexpression> <filename> grep string_to_find *


search content of all the files in the current directory for string_to_find

grep -r string_to_find .
search content of all the files in the current directory recursively for string_to_find

grep grep grep grep

-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

... Regular Expression lOl alteration


r1|r2
r1 or r2

grouping
(r)

65

Head and Tail head filename


output the first part of files

head --lines <n> filename


output first n lines

tail filename
output the last part of files

tail --lines <n> filename tail -f filename


output appended data as the file grows

66

Compressing zip -r file.zip FILES


compress all FILES into file.zip Files could contain wildcards

unzip file.zip
decompress file.zip

unzip -t file.zip
just test the file, no decompression

zip -R file.zip FILES


recursively add the files in directory structure

67

... Compressing zip -u file.zip FILES


update file.zip

unzip file.zip -d extdir

68

... Compressing tar cfv file.tar FILES


create a tar file archive with verbose output from FILES

tar cvfz file.tar.gz FILES


create a tar file archive with verbose output from FILES and then compress it using gzip algorithm

tar cvfj file.tar.bz2 FILES


bzip2 algorithm for compression

69

... Compressing tar xfv file.tar


extract tar archive

tar xvfz file.tar.gz


extract tar gz archive

tar xvfj file.tar.bz2


extract tar bzip2 archive, j!

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

... Processes pstree


shows a tree of processes

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

KILLING kill -9 PID


terminate a process

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

File Disk Usage du


estimate file space usage

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

System Disk Usage df


Report file system disk space usage

df -h
report in a human readable format

79

Date and Time date date +'%Y-%m-%d %H:%M:%S'


show date with the specified format %Y -> year %m -> month for detailed see manual for date

Setting the date


date --set='2006/11/20 12:34:23 IRST'
set date to November 20, 2006 12:34:23 also set the time zone to Iran Standard Time

To set the your hardware clock use hwclock


hwclock --set --date=newdate
80

Peripherals Mount: binding a device to a directory CDROM device


/dev/cdrom /dev/hdc

In order to use cdrom


mount /dev/cdrom /mnt/cdrom /mnt/cdrom is the content of CDROM

You can't eject cdrom while you are using it First un-mount

81

... Peripherals umount /dev/cdrom


or

umount /mnt/cdrom No process should use this device or directory!

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

Floppy /dev/fd0 mount /dev/fd0 /mnt/floppy umount /dev/fd0


unmounting the floppy device

84

Connecting to another computer Secure Shell


a secure encrypted connection between two host

ssh user@host_name ssh user@ip.add.re.ss use `-v' option for verbos output Secure file trasfer
sftp user@host_name sftp user@ip.add.re.ss

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

... Downloading wget -i <file>


read URLs from file

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

Вам также может понравиться