1. Directories and moving § mkdir dir1 § mkdir -p dir1/dir2 § cd § cd . § cd .. § cd ~user § cd into symlinks o cd .. (depends on the shell/command interpreter)
2. Listing files § ls o ls is often an alias in shell/command interpreter o cd o alias § /bin/ls § the real unix command § unalias ls § ls -l § ls –d § ls -s § ls -s | more –c (note different output - ls use isatty()) § ls -a § ls -a .??* § ls -lad .??* § ls –lt § ls –lu § ls -lS § ls -rtl § ls -rlS § ls -rlS -h § ls -rlS –si § man ls
3. Links § Hard link - gives another name to a file o ln file4 file4.hlink o File is removed when it has no more link o Hard link limited to filesystem § Symbolic links o ln -s file4 file4.hlink o Symbolic links can cross filesystems
4. Redirection and IPCs § proc1 > output1 § proc1 2> error1 § proc1 2>error1 > output1 § proc1 | proc2 § proc1 | tee output2 § proc1 2>&1 | tee output2 § named pipes § mkfifo /tmp/bar § proc1 > /tmp/bar § proc2 < /tmp/bar § cat beers > /tmp/bar & sort /tmp/bar
5. File times (and status) § file times o access time read data o modified time write data o change time inode change (e.g. permission) § ls -l --time=use § ls -l --time=,ctime § stat
6. Moving/Copying file § mv file1 file2 § mv file1 dir1 § cp file1 file2 § cp file1 dir1 o cp –p (preserve perm/time - useful for config backup) o cp -r o cp –i § Ask confirmation before overwriting
7. User and group ownership § /etc/passwd, /etc/group o uid, gid, gecos § /etc/shadow, /etc/gshadow § useradd, groupadd § chown § chgrp
8. File types and protection § file types o directory o regular o symbolic link o named pipes o ... § file protection modes § special bits o setuid for executables o setgid for directories o sticky bit for files o sticky bit for directories § special cases o rwx for directories
9. Devices files § ls -l /dev directory --> Special files for devices § Real devices o /dev/audio, /dev/dsp, /dev/sdax, /dev/hdax, ... § Pseudo devices o /dev/null, /dev/zero, /dev/random, ...
10. Disk space § Files are taking space on disk § Space occupied by a directory o du o du * o du -s o du -s * o du -s -k o du -s -h * o du -s --si * o What about hard links ? § counted once o What about Symbolic links § du -L § Disk space used by a user o du may not be suitable o quota § Disk space used on a partition o df o df –k o df –h o df --si
11. Finding files § find (pathname expression) o type f, d, l, p, c, b, s § In ~home • find . -type f • find * -type f -ls • find * -type d -ls § In /dev • find . -type d • find . -type p -ls • find . -type s -ls o perm mode, -mode, +mode with negation (!) o In ~home § find . -perm 0600 -ls § find . \! -type d -perm 644 -ls § find . \! -type d -perm 111 -ls § find . \! -type d -perm -111 -ls § find . \! -type d -perm +111 -ls o mtime/atime/ctime § o In ~home § find . -type f -mtime -2 § find . -type f -atime -2 § find . -type f -mtime +1200 o size o In ~home § find . -type f -size +120000k -ls o name o path o In ~home § find . -type f -name \*dir\* § find . -type f -path \*dir\* o user (nouser) o In ~psp § find . -nouser -ls o group (nogroup) § locate o run periodically o on sync with delete (on good UNIX implementation). § Advanced find o find -name \*.gz -mtime +7 -size +500M -size -1G –ls o find . -name \*.gz -mtime +7 -size +500M -size -1G -exec rm -f {} \; o find . -name \*.gz -mtime +7 -size +500M -size -1G –delete o find . -name \*.gz -mtime +7 -size +500M -size -1G -execdir gzip {} \; 12. Files comparison § Diff § diff –C § patch
13. Files archives / compression § gzip § gzip file § gzid -d file.gz § gzip -v9 file § gzip -v1 file § gzip -dc file.gz | proc1 § gzip -dcf message* | proc1 § xz § tar o tar cvf archive.tar ./data o tar tvf archive.tar o tar xvf archive.tar o gzip -v9 arctive.tar o gzip integrated into tar with z option
14. Process management § finding processes o ps o ps u o ps xu § (x = proc without terminals) o ps aux o ps auxw o ps auxww o ps alx § On a system V UNIX o ps -ef o PATH=/usr/ucb:$PATH § top o nProc & MultiThread (1) § States & Priorities o R, S, W, D(U), N, < § nice, renice § /proc o environ o fd o cpuinfo o sys o meminfo § lsof 15. Elementary filters § From the tarball airnav.tar.gz § grep o grep Belgium lands o grep belgium lands o grep -i belgium lands o grep -i niger o grep -i -w niger o grep -i -c niger o grep -i -v belgium o grep -i -w london */* o grep -i -w -l london */* § dirname, basename o dirname CA/apt.txt o dirname /usr/bin/ls o basename CA/apt.txt o basename CA/apt.txt .txt § awk o awk -F: '{print $2}' apt.txt o awk -F: '{print "ICAO", $1, $2}' apt.txt o awk -F: '{printf "ICAO %4s %20s\n", $1, $2}' apt.txt o awk -F: '{ if( ! $1 ) printf "%20s\n", $2}' apt.txt o awk -F: '{ printf "%20s : %2d\n", $2, length($2);}' apt.txt o awk -F: '{ printf "%-20s: %2d\n", $2, length($2);}' apt.txt § tr o see man tr ... ! § sort o sort apt.txt o sort -t: -k3,3 apt.txt o sort -t: -k 3,3n apt.txt § head, tail o head -n o tail -n o tail -f (= -tail--follow=descriptor) o tail -F (= -tail--follow=name) § xargs o xargs –nn o xargs –ln § echo a b c d e f g h I j | xargs –n2 echo | xargs –l1 echo o xargs –I replace-str o file hosts-expe.txt (with host names) o cat hosts-expe.txt | xargs -l1 -iH xargs \ wget -O results-H.txt http://H/results.txt
16. Advanced filters § grep -l -i london */apt.txt | awk -F/ '{print $1;}' \| xargs -I {} -l 1 grep {} lands § cat lands.txt | tr "[:lower:]" "[:upper:]" | sort -n § cat */apt.txt | sort -t: -k7,7n | tail -5 § grep -l -i london */apt.txt \| xargs -l1 -i{} sh -c "mail -v -s {} [email protected] < {}"
17. More filters § Editors o Discussion about editors § dd, ex, vi, emacs, nemacs, pico o Stream editors § sed -e '/brolle/d' < file_in > file_out § sed -e 's/regex1/exptr2/' < file_in > file_out § sed -e 's/regex1/exptr2/g' < file_in > file_out § cat vs tac (on modern linux system) o file0 § 11 mai § 12 mai o file1 § 5 mai § 10 mai o cat file0 file1 o cat file1 file0 o cat file* o cat file[01] o cat file[10] o tac file* o tac file* | tac
18. Shells § shell est l'interpréteur de commande § shell est aussi le nom générique d'un command file § Il n'y a pas un seul interpreteur de commande: o sh - csh - ksh - bash - tcsh - zsh o explication de quelques differences entre les shells § programming syntax § history § rappel: shell par defaut dernier champs de /etc/passwd o valid shells: /etc/shells
19. Bash programming § Programming --> variables § Exemple mysh01.sh o suffix is irrelevant o file type determined by magic characters (/usr/share/file/magic) o #! perl, sh, bash, ... could be print § Variables can be local or global (environment) § Variables can be predefined: o $$ (pid) o $* o $? o $0 o $# o $1 $2 $3 $4 ... o $BASH_VERSION § Demo global variable avec mysh01.sh et prenv o /proc/pid/environment is the startup environment o cat /proc/pid(mysh01.sh)/environ | tr '\0' '\n'| grep GLOVAR --> nothing o cat /proc/pid(prenv)/environ | tr '\0' '\n'| grep GLOVAR --> OK
20. Wild characters (expansion by the shell) § ls a* § ls *.* § ls *.sh § ls ? § ls ????.* § ls *.[08] § ls *.[0-9] § ls *.{pl,sh} § advanced: (shopt - shopt -s extglob) § ls *([a-z0-9]).? a comparer avec ls *[a-z0-9].? § ls *[0-9]+([0-9]).sh § ls *.?
21. Shell escaping § \ § simple quotes § double quotes § demo prargs o ./prargs 0 o ./prargs \0 o ./prargs \\0 o ./prargs "\0" o ./prargs '\0' o ./prargs "$0" o ./prargs '$0'
§ var=3232 o ./prargs $var o ./prargs \$var o ./prargs "$var" o ./prargs '$var'
§ ./prargs * § ./prargs \*
22. Wild characters (advanced) § If nothing match a wild character expansion, the argument is passed as is to the program § demo from Annexes-11 o ./prargs *.{awk,pl} o ./prargs /usr/bin/*.{awk,pl} o ./prargs *.doc *.c
23. Shell constructs § for loop o based on file list: § mysh02.sh • introduction of basename (and dirname) • introduction of md5sum o based on string words § mysh03.sh § expr for arithmetic expression o cnt=1 o cnt=$(expr $cnt + 1) § while loop o mysh04.sh o Warning test syntax (man test) § simple tests o Example 1 - test number of arguments (mysh05a.sh) o Example 2 - test file existence o Example 3 - test if root (mysh05b.sh) o Example 4 - test exit value (mysh05c.sh)
24. Subshells § cmd1 & § cmd1 & cmd2 & cmd3 & § prodd 10 & ls § prodd 10 && ls § prodd 11 & & ls § cmd1 ; cmd2 ; cmd3 § (cmd1 ; cmd2) o (date ; ls) | mail -v -s testfile [email protected]