Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

06.linux basic-operations-2

445 views

Published on

  • Be the first to comment

06.linux basic-operations-2

  1. 1. Linux: Basic Operations - 2 Minsuk Lee Hansung University, Seoul, Korea minsuk@hansung.ac.krNEAOSS MC2.0 CC-BY 2.0 KR, © Korea OSS Promotion Forum, NIPA
  2. 2. Simple Shell usage • Invoking Terminal  • Type any command and [ENTER] • BIG TIPs !! – Use ‘←,↑→↓’, [INS], [DEL],[HOME],[END] keys to edit command line – Try [tab] - It completes filename or shows all available choices – When output stops, try [SPACE], mouse scroll, ‘q’ to continue or to quit – $ exit -- will end the terminalNEAOSS MC2.0 CC-BY 2.0 KR, © Korea OSS Promotion Forum, NIPA
  3. 3. man (1) • Shows manual page of Linux commands, libraries, utilities • Usage : $ man [options] [section] item – Item can be program, function, file, anything in /usr/share/man/*/ • Options : – Sections 1. Executable programs or shell commands 2. System calls (functions provided by the kernel) 3. Library calls (functions within program libraries) 4. Special files (usually found in /dev) 5. File formats and conventions e.g. /etc/passwd 6. Games 7. Miscellaneous (including macro packages and conventions), e.g. man(7), groff(7) 8. System administration commands (usually only for root) 9. Kernel routines [Non standard] ‘$ man printf ‘ show man page of shell command printf ‘$ man 3 printf ‘ show man page of printf() library ‘$ man -a printf ‘ show man page of both of the twoNEAOSS MC2.0 CC-BY 2.0 KR, © Korea OSS Promotion Forum, NIPA
  4. 4. man (2) • Options : -a : display all available manual pages of the item -k : list all man page items includes the given item name ($ apropos –r item) -f : shows very short descriptions -Tps : output as Postscript format (to be converted into pdf) • Example $ man man $ man -Tps 3 printf | ps2pdf - printf.pdf • Alternatives $ info item : shows detail information, if the item is available in /usr/share/info $ command –help : shows help messages (options) of the command • PLEASE READ MAN PAGES OF COMMANDS, FUNCTIONS YOU USE !!NEAOSS MC2.0 CC-BY 2.0 KR, © Korea OSS Promotion Forum, NIPA
  5. 5. Name pattern matching • Specifying multiple files(directories) – Work for ALL commands • ‘*’ means any string (multiple, any characters including null) • ‘?’ means any single character • [a-s] : means any single character between ‘a ’ and ‘s ’ – E.g., [1-7c-f], [acf2A-Z], … – ‘$ rm * ’ : means everything in current directory – ‘$ rm directory/* ‘ : means everything in directory – ‘$ rm s*s ‘ : means files or directories start and end with ‘s ’ – ‘$ rm 6[ab]x* ‘ : means files or directories start with ‘6ax ’ or ‘6bx ’ • See man page of bash ( $ man bash ) for more – Such as ?(patten-list), *(patten-list), +(pattern-list), …NEAOSS MC2.0 CC-BY 2.0 KR, © Korea OSS Promotion Forum, NIPA
  6. 6. Pipe and redirection • Standard Input and Output – In Shell, It’s Keyboard input and Screen Output, by default – Pipe feeds standard output into other command’s standard input – Redirection redirects the standard input/output from/to files ‘$ ls –l | more’ : feed the output of ‘ls –l’ to ‘more’ ‘$ ls –1 | sort –r | more’ : ‘ls -1’ then, ‘sort’ in reverse order, and ‘more’ ‘$ ls –l > file ’ : redirect the output of ‘ls –l’ to file (overwrite or create) ‘$ ls –l >> file ’ : redirect the output of ‘ls –l’ to file (append) ‘$ cat a b > c ’ : concatenate file ‘a ‘ and ‘b ‘ into file ‘c ‘ ‘$ sort < source > destination ’ : ‘sort’ source, into destination • Standard Error ‘$ command 2> file’ : redirect error message of command to fileNEAOSS MC2.0 CC-BY 2.0 KR, © Korea OSS Promotion Forum, NIPA
  7. 7. grep • Prints lines matching a pattern • Usage : $ grep [options] pattern file… • Options : – -i : ignore case – -n : print line number – -H : with file name – -r : read all files under directory recursivelyNEAOSS MC2.0 CC-BY 2.0 KR, © Korea OSS Promotion Forum, NIPA
  8. 8. find (1) • Searches for files in a directory hierarchy • Usage : $ find [options] [path...] [expression] • Options : -H, -L, -P : controls symbolic links (see, $man find) • Expression for [match test] [Action] : – Match test -name : ‘$ find / -name thisname’ finds files with thisname, thisname can be pattern (*, ?, …) -executable : executable files -user : ‘$ find / -name user1’ finds files belonging to user1 -atime : ‘$ find / -atime n’ finds files accessed within n days -ctime , -mtime : files created or modified within n days -newer : ‘$ find / -newer file’ finds files modified more recently than file -type c : ‘$ find / -type c’ finds files of type c, (c can be ‘d’ : directory, ‘f’ : regular file, ‘l’ : symbolic link, ‘b’,’c’: device file) -… – Action -delete : delete the found files -exec command : apply command to the found files in command ‘{}’ is used for found file command ends with ; ( is escape code) -…NEAOSS MC2.0 CC-BY 2.0 KR, © Korea OSS Promotion Forum, NIPA
  9. 9. find (2) • Example – Just find – find and grepNEAOSS MC2.0 CC-BY 2.0 KR, © Korea OSS Promotion Forum, NIPA
  10. 10. ps • Reports a snapshot of the current processes • Usage : ps [options] • Options : ax : all process in system, same as ‘–e’ r : only running process nnn : only process id nnn, same as ‘-pid pid_list list’ (e.g., ‘$ ps –pid 107,109’) l, s, u, v, x : format long, signal, user-oriented, virtual memory, register format m : shows threads after processNEAOSS MC2.0 CC-BY 2.0 KR, © Korea OSS Promotion Forum, NIPA
  11. 11. pstree • Display a tree of processes • Usages : $ pstree [options] [pid_or_user] – $ pstree myuser (Terminal related part) – $ pstreeNEAOSS MC2.0 CC-BY 2.0 KR, © Korea OSS Promotion Forum, NIPA
  12. 12. top (1) • Display Linux tasks • Usage : $ top [options] [pid…] [Commands to change ] See man page !NEAOSS MC2.0 CC-BY 2.0 KR, © Korea OSS Promotion Forum, NIPA
  13. 13. top (2) ‘;’ multiple commands ‘>’ redirection <type x.c> <endless loop code> x.c Ctrl-D to end edit Compile x.c to loop ‘&’ execute background execute ./loop Ctrl-Z to suspend ‘bg’ resume in backgroundNEAOSS MC2.0 CC-BY 2.0 KR, © Korea OSS Promotion Forum, NIPA
  14. 14. top (3) • top with a endless loop task • top with two endless loop tasksNEAOSS MC2.0 CC-BY 2.0 KR, © Korea OSS Promotion Forum, NIPA
  15. 15. kill • Send a signal to a process • Usage : $ kill –signal [pid] – $ kill -9 100 : kills process 100 – $ kill -9 -1 : kill all (you can kill) • Signals : -9 (KILL) : kill process -1 (HUB) : restart Others : see man page !NEAOSS MC2.0 CC-BY 2.0 KR, © Korea OSS Promotion Forum, NIPA
  16. 16. tar, bzip2, gzip (1) • Compresses files and directories • Usage : $ tar [options] path… – $ tar cvf name.tar directory : Archive (no compress) directory into name.tar – $ tar xvf name.tar : Extract name.tar • Options : – c : create, x : extrace, t : list – v : verbose (lists file names) – j : use bzip2, z : use gzip for compress / uncompress – f filename : specify archive filename • Bzip2 : compress a file into file.bz2 – ‘$ bzip2 file ’ to compress, ‘$ bzip2 –d file.bz2 ’ to uncompress • gzip : compress a file into file.gz – ‘$ gzip file ’ to compress, ‘$ gzip –d file.gz ’ to uncompress • Example – ‘$ tar cjf file.bz2 files-directories ‘ : tar & compress files-directories into file.bz2 – ‘$ tar xjvf file.bz2 ‘ : uncompress and untar file.bz2NEAOSS MC2.0 CC-BY 2.0 KR, © Korea OSS Promotion Forum, NIPA
  17. 17. tar, bzip2, gzip (2) tar & bzip2 list archive Remove temp directory un-bzip2 and untarNEAOSS MC2.0 CC-BY 2.0 KR, © Korea OSS Promotion Forum, NIPA
  18. 18. df, du • df report file system disk space usage • Usage : $ df [options] [file] • Options -h : human readable units ! • du estimates file space usage • Usage : $ du [options] [file] • Options -h : human readable units ! -s : display only totalNEAOSS MC2.0 CC-BY 2.0 KR, © Korea OSS Promotion Forum, NIPA
  19. 19. sudo • Execute a command as another user (such as super user) • Usage : $ sudo [–u username] [command] – $ sudo –u other-user /bin/bash : invoke shell with user other-user – $ sudo /bin/bash : invoke superuser’s shell – $ sudo command : run command in superuser permission • Only selected user can get superuser permission • (defined in /etc/sudoers) ? $ sudo rm -rf ./* vs. $ sudo rm –rf /*NEAOSS MC2.0 CC-BY 2.0 KR, © Korea OSS Promotion Forum, NIPA
  20. 20. chown, chmod • Changes owner, permission of files and direct • Usage : $ chown [owner][:group] file… $ chmod mode file… • Options -R : recursively change • Permission issue – $ chown can be done by superuser – $ chmod needs enough permission • Permission (read,write,execute) mRWXRWXRWX user others group Type (file, directory, device)NEAOSS MC2.0 CC-BY 2.0 KR, © Korea OSS Promotion Forum, NIPA
  21. 21. hostname, uname – $ hostname : name of this computer • Search domain, name server are defined in /etc/resolv.conf – $ uname : Operating System Name as compiledNEAOSS MC2.0 CC-BY 2.0 KR, © Korea OSS Promotion Forum, NIPA
  22. 22. ifconfig Use GUI • Configures a network interface To configure Network ! • Usage : $ ifconfig [interface or –a] $ ifconfig interface [aftype] options | address ... – interface is the name of network device • (e.g., eth0 : Ethernet, lo : loopback)NEAOSS MC2.0 CC-BY 2.0 KR, © Korea OSS Promotion Forum, NIPA
  23. 23. ping • Send ICMP ECHO_REQUEST to network hosts • Usage : $ ping [options] destination – destination is ip-address (a.b.c.d) or hostname • Options : -c count : stop after count -i interval : ping interval -n : display ip address rather than hostname -s packetsize : set packet sizeNEAOSS MC2.0 CC-BY 2.0 KR, © Korea OSS Promotion Forum, NIPA
  24. 24. netstat (1) • Prints network connections, routing tables, interface statistics, masquerade connections, and multicast memberships • Usage : $ netstat [options] • Options : -r : print routing table -i : print interface statistics -n : display ip address rather than hostname -p : print pid and program name -l : print only listening sockets -a : print both listening & non listening -t, -u : print socket using tcp, udp protocolNEAOSS MC2.0 CC-BY 2.0 KR, © Korea OSS Promotion Forum, NIPA
  25. 25. netstat (2)NEAOSS MC2.0 CC-BY 2.0 KR, © Korea OSS Promotion Forum, NIPA
  26. 26. Network files • /etc/hosts : stores hostname and ip-address • /etc/resolv.conf : ip-address of name server • /etc/services : name of network servicesNEAOSS MC2.0 CC-BY 2.0 KR, © Korea OSS Promotion Forum, NIPA
  27. 27. Let’s Practice • find & grep – Build up directories – Try $ find , $ grep, and mix it • Top – Edit ‘x.c’, compile into ‘loop’ – $ top – Run ‘loop’ background and $ top, – And once more, see how ‘loop’ processes share the CPU – Try ‘$ pstree’ to find ‘loop’ process – $ kill the ‘loop’ process • Tar, bzip2 – As in slide • Try $ df, $ du, $ chown, $ chmod, $ sudo • Try $ ping, $ netstat [options], $ ifconfigNEAOSS MC2.0 CC-BY 2.0 KR, © Korea OSS Promotion Forum, NIPA

×