<<

1. Directories and moving § dir1 § mkdir -p dir1/dir2 § § cd . § cd .. § cd ~user § cd into symlinks o cd .. (depends on the shell/ interpreter)

2. Listing files § o ls is often an in shell/command interpreter o cd o alias § /bin/ls § the real command § unalias ls § ls -l § ls –d § ls -s § ls -s | –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 - gives another name to a o file4 file4.hlink o File is removed when it has no more link o limited to filesystem § Symbolic links o ln -s file4 file4.hlink o Symbolic links can cross filesystems

4. Redirection and § proc1 > output1 § proc1 2> error1 § proc1 2>error1 > output1 § proc1 | proc2 § proc1 | output2 § proc1 2>&1 | tee output2 § named pipes § mkfifo /tmp/bar § proc1 > /tmp/bar § proc2 < /tmp/bar § cat beers > /tmp/bar & /tmp/bar

5. File times (and status) § file times o access read data o modified time data o change time inode change (e.g. permission) § ls -l --time=use § ls -l --time=,ctime §

6. Moving/Copying file § file1 file2 § mv file1 dir1 § 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/, /etc/group o uid, gid, gecos § /etc/shadow, /etc/gshadow § useradd, groupadd § §

8. File types and protection § file types o 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 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 o df –k o df –h o df --si

11. Finding files § (pathname expression) o 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 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) § o run periodically o on 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 -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 –C §

13. Files archives / compression § gzip § gzip file § gzid -d file.gz § gzip -v9 file § gzip -v1 file § gzip - file.gz | proc1 § gzip -dcf message* | proc1 § xz § 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 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 § o nProc & MultiThread (1) § States & Priorities o R, S, , D(U), N, < § nice, renice § /proc o environ o fd o cpuinfo o sys o meminfo § 15. Elementary filters § From the tarball airnav.tar.gz § 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 */* § , o dirname CA/apt.txt o dirname /usr/bin/ls o basename CA/apt.txt o basename CA/apt.txt .txt § o awk -F: '{print $2}' apt.txt o awk -F: '{print "ICAO", $1, $2}' apt.txt o awk -F: '{ "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 § o see man tr ... ! § sort o sort apt.txt o sort -t: -k3,3 apt.txt o sort -t: -k 3,3n apt.txt § , o head -n o tail -n o tail -f (= -tail--follow=descriptor) o tail -F (= -tail--follow=name) § o xargs –nn o xargs –ln § 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 names) o 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 " -v -s {} [email protected] < {}"

17. More filters § Editors o Discussion about editors § , , , emacs, nemacs, pico o Stream editors § -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 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 § § rappel: shell 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 #! , 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 o based on string words § mysh03.sh § for arithmetic expression o cnt=1 o cnt=$(expr $cnt + 1) § while loop o mysh04.sh o Warning 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 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]