CMT3321 (02/03) (Linux study guide 2 - basic commands) For Lab. 2, 3 Lecturer : Mr. Lee Bo-sing (Joe) Basic commands (1) Basic commands (2) Basic commands (3) - find ? Function : search for set of files with specified characteristic in a directory hierarchy, starting from specified directory. ? Syntax : find starting_dir_pathname expression_for_file_characteristic ? Expression includes : – -name "x" (by filename, x is wildcard for filename) – -type x (by file type, x = b,c,d,f,l) – -user "x" (by file owner name x) – -group "x" (by group name x) – -size +nc (file size greater than n byte) – -size -nc (file size less than n byte) – -size nc (file size equals to n byte) Basic commands (4) - find – -mtime +x (modified more than x days ago) – -mtime -x (modified less than x days ago) – -mtime x (modified exactly at x days ago) – -printf "%m\t%u\t%g\t%s\t%t\t%p\n" (print searched files in format of ls -ld) ? Components of expression can be combined in logical NOT, AND, OR : – NOT : !expr – AND : expr1 -a expr2 – expr1 expr2 – OR : expr1 -o expr2 ? Demo with file find_demo.ksh Basic commands (5) - cat ? Function : – Display content of file. – Drawback :if size of file is more than one page, you have to scroll up to view content of first page – Eg. cat filename – Can it work for wildcard ? – Concatenate content of 2 or more files – Eg. cat file1 file2 > file3 – Content of file3 will be content of file1 + file2 Basic commands (6) – less ? Function : read content of file page by page, with fast movement and pattern search function ? Syntax : less filename ? Move to beginning of file : press g ? Move to beginning of file : press G ? Forward one page : press ctrl-f ? Backward one page : press ctrl-b ? Search pattern : /pattern (n for next downward search, N for next upward search) ? Quit : press q ? Actually, its function key definition is similar to that of vi ? Note that "more filename" can also read content of file page by page. Its function is much less than "less filename" Basic commands (7) – head, tail ? head filename : view first 10 lines of file ? head -30 filename : view first 30 lines of file ? tail filename : view last 10 lines of file ? tail -25 filename : view last 25 lines of file Basic commands (8) - ls ? Function : list name of filename/subdir. in a directory ? Syntax : ls -options wildcard_for_filename_or_subdirname ? Options : – -a (show all files, including hidden files, wildcard must match if you want to show hidden files) – -l (display long format) – -d (does not list recursively) – -i (list inode number) Basic commands (9) - wc ? Function of wc : Count number of lines, words and characters in a file ? Syntax : wc -option filename ? Option : – -l (show number of lines only) – -w (show number of words only) – -c (show number of characters only) ? Output of "wc filename" – Num_of_line num_of_word num_of_char filename Basic commands (10) - file ? Function : a quick way to find out classification of files for a specified set of files ? Classification includes : ascii text, executable binary, link, character special, block special, empty, image, directory ? How to use : file wildcard | more Basic commands (11) – diff, cmp ? diff : – Function : compare 2 ascii text file – Syntax : diff file1 file2 – Difference will be shown line by line. ? cmp : – Function : compare 2 binary files – Syntax : cmp -s file1 file2 ? -s suppress output and return status : – 0 ( 2 files are identical) – 1 (2 files are different) – 2 (some error occurs) ? Often used in script Basic commands (12) - grep ? Function : search for some pattern specified in regular expression format in a file or standard input. – Working on file ? grep "pattern" filename ? Eg. grep bslee /etc/passwd ? Very useful in working with pipeline ? Eg. file * | grep dir ? Eg. ps –aux | grep root Basic commands (13) – sort ? Function : sort lines of ascii text file or stdin by some specified criteria. The ascii text file can be a database file. ? Result is output to stdout => can be piped ? The default sorting order is lexical order. ? With appropriate option, it assumes each line has several fields separated by specified separator. ? Default field separator is white space (i.e. space, tab) Basic commands (14) - sort ? Syntax : sort -option text_filename ? Option : – -n (numeric order) – -d (consider only blank & alphanumeric characters) – -f (ignore case) – -r (reverse output order) – -t? (specify field separator as ? ) – +n (specify field n as starting sorting key) – -m (specify field m as ending sorting key) – (n, m is numbered starting at 0) ? Demo with file sort_demo.ksh Basic commands (15) - uniq ? Function : remove duplicate lines from a sorted ascii text file or stdin ? Assume field separator is white space ? Syntax : uniq -option text_filename ? Option : ? -c (prefix lines by number of occurrence) ? -i (ignore case) ? -d (display duplicated lines only) ? -u (display unique lines only) ? -n (ignore first n fields) ? +n (ignore first n characters) ? Drawback : cannot specify field separator, particular field(s) to ignore ? How to solve it ? ? Demo with file uniq_demo.ksh , uniq_target Basic commands (16) – cut & paste ? Cut : – Function : split a text file or stdin vertically – Syntax : cut -options text_filename – Options : ? -d"?" (specify field separator as ?) ? -fn1,n2 (select field n1, n2. Field number starts as 1) ? -fn1-n3 (select from field n1 to n3) ? -f-n1 (select from first field to field n1) ? -fn1- (select from field n1 to last field) Basic commands (17) – cut & paste ? Paste : ? Function : merge 2 or more files vertically ? Syntax : paste -options file1 file2 ….. ? Options : ? -d"?" (specify field separator as ?) ? Think about 2 methods of changing the field separator of a file ? Demo with file cut_demo.ksh Basic commands (18) – split ? Function : cut a text file horizontally into several files ? Syntax : split -option original_file prefix_of_result_file ? Option : -ln (each output file contains n line(s) of original file) ? Several files will be output and named as prefixaa, prefixab, prefixac, ….. ? Demo with file split_demo.ksh, split_target ? How to paste 2 or more files horizontally? Basic commands (19) - gzip ? Function : losslessly compress or decompress one or more files (ordinary or directory only). ? Keep access mode, ownership, time-stamp (create, access & modify time) on compression and decompression ? Each target file will be compressed and "filename" changes to "filename.gz" ? Syntax : gzip -option wildcard_for_target_files ? Option : – -r (recursively down a directory) – -d (decompress) ? bzip2 is getting popular because of better compression ratio Basic commands (20) - tar ? Function : archive one or more file into another tar file either for transfer or tape archive ? Ordinary, directory, link file can be tared. For link file, it only tar the link file but the target point to by link file ? Extension of tar file is usually .tar , for human recognition ? Demo with file tar_demo.ksh Basic commands (21) - tar ? Syntax : tar -options tar_filename wildcard_for_target_files ? Options : – -c (create new archive or tar file) – -f (specify name of tar file) – -r (append file to end of tar file) – -v (verbosely) – -x (extract a tar file) – -t (list content of a tar file, can only work on uncompressed tar file) – -z (compress after tar) Basic commands (22) – at ? at : schedule a job/script/program to run at a later time/date ? Syntax : at -options time – -l (list scheduled jobs at queue) – -d job_id (remove job of job_id from at queue) – -f pathname_of_script_or_command_to_run ? Time can now+1minute, now+2hour, now+3day, now+1month, now+1year, 1300, 2330, etc… ? Demo with file at_demo.ksh Basic commands (23) – at ? Daemon /etc/rc.d/init.d/atd must be started ? Not everyone can run above commands. Controlled by 2 configuration file : /etc/at.deny, /etc/at.allow – If the file /etc/at.allow exists, only usernames mentioned in it are allowed to use at. – If /etc/at.allow does not exist, /etc/at.deny is checked, every username not mentioned in it is then allowed to use at. – If neither exists, only the superuser is allowed use of at. – An empty /etc/at.deny means that every user is allowed use these commands, this is the default configuration. Basic commands (24) – crontab ? Jobs scheduled by "at" only run once. To periodically run jobs at scheduled time, you can use crontab. ? You only need to configure one time. ? crontab –e – Content to input : refer to file cron_example ? To edit : crontab –e ? To list : crontab –l ? To remove : crontab –r ? Demo with file cron_example, incre.ksh Basic commands (25) – crontab ? Daemon /etc/rc.d/init.d/crond must be started ? Not everyone can run above commands. Controlled by 2 configuration file : /etc/cron.deny, /etc/cron.allow – If the cron.allow file exists, then you must be listed there in order to be allowed to use this command. – If the cron.allow file does not exist but the deny file does exist, then you must not be listed in the cron.deny file in order to use this command. – If neither of these files exists, then depending on site-dependent configuration parameters Basic commands (26) - tr ? Function : translate ascii characters into different ascii characters of same length. ? Works on stdin ? Work with pipeline ? Syntax : tr 'src_string' 'dest_string' ? Eg. cat file1 | tr 'abc' 'ABC' > file2 ? Eg. cat file1 | tr 'abc' '123' > file3 ? application is limited as length of src_string and dest_string must be the same Basic commands (27) ? which ? uname –a ? hostname ? date ? date +%Y%m%d%H%M%S ? df –k ? du –sk dir_pathname ? who ? whoami ? su Open Discussions Exercise (1) ? a. Name the command to find out linux kernel version? ? b. How to find out used % of your root partition. Please name the command and options used. ? c. login as root, find out & list, with format similar to "ls –ld ", all the files under dir. /etc with following criteria : – Filename starting with ta – Filename starting with ta and filesize smaller than 100byte – Filename starting with a and file type is link – Filename starting with a and file owner is "news" Exercise (2) -1 ? a. Find out the detailed file type (more than the 4 types) of following files : – /bin/vi, /bin/view, /etc/passwd, /dev/fd0, /dev/had, /dev/isdn1, /bin/, /etc/rc.d/init.d/crond, /boot/vmlinuz-2.4.18-3, /boot/initrd-2.4.18-3.img ? b. Create a subdirectory , named "lab2_3", under your home directory. Ftp download script $HOME/student_cp.ksh & $HOME/at_cron.ksh from 158.182.111.18 into $HOME/lab2_3/. Username & password is anonymous and any respectively. Run script student_cp.ksh. This script will copy all files (except /bin/, /dev/fd0, /dev/had, /dev/isdn1) in part a to dir. "lab2_3" ? c. after copying, how to use one command to know sum of filesize of all files in $HOME/lab2_3/ ? Exercise (2) - 2 ? $HOME/lab2_3/passwd is a file containing information of each user account with following format : Username:password:usid:gid:comment:home-dir:shell-type ? d. How to check for existence of username of "root", "bin", "daemon" and find out their usid and home-dir? ? e. What command can easily find out total number of accounts in file $HOME/lab2_3/passwd ? ? f. copy $HOME/lab2_3/passwd to $HOME/lab2_3/passwd.new and compare these 2 files ? g. modify first and second lines of file $HOME/lab2_3/passwd.new with any editor and compare these 2 files again. ? h. how to generate a new file $HOME/lab2_3/passwd_2 which contains only username, usid, home-dir fields, separated by "|", for each account. ? i. how to generate another file $HOME/lab2_3/passwd_3 which is sorted by value of usid of file in part g. Exercise (3) ? a. archive all files in dir. $HOME/lab2_3/ into a single file, named "tarfile.tar", without compression. ? b. how to list what files are archived in "tarfile.tar" ? c. create a dir. $HOME/lab2_3/ extract/ and extract all files in "tarfile.tar" into this new dir. ? d. compress all files $HOME/lab2_3/ extract/ Exercise (4) ? What is done by script at_cron.ksh : – create a new file $HOME/lab2_3/filesize if this file does not exist. – Then append "Hello….\n" to this file. ? a. schedule script $HOME/lab2_3/at_cron.ksh to run at one minute later once. What is the result? ? b. redo part a but schedule to run at 9:10 p.m. What is the value of job id? ? c. schedule script $HOME/lab2_3/at_cron.ksh to run at every 1 minute & observe the result 2 minutes later. ? d. un-schedule what you done in part c.