Monday, 3 December 2007

Notes for Unix Course


  • ls -R: to recursively descend subdirectory tress

    ls -i: to get inode number for each file.

    ls -l: can list 'link count'. This shows how many files are linked to one file ( same inode number)
  • find / -inum XXXXX -ls: here ls is similar to -print; using this way we can find what files are linked to same file (for hard link).

    $touch -m -t 200712030900 timewanted # generate a file with specified time stamp.
    $find / -newer timewanted -ls # show the files modified after the specified time.

    find / -mount -name "XXXXXXX" -ls: here -mount means NOT scan mounted file system. Especially useful for NOT scan mounted iso files, mounted Windows file system and some network or mirrors storage.
  • 2> /dev/null: this always follows shell command to redirect standard error output. while "1>" for standard output redirection. /dev/null doesn't generate any file, just get rid of error messages. For example: $ls -il /bin/ls # we will get the inode number XXXand link count, if the ls has been hard linked to other files, use the following command find what they are; $find / -mount -inum XXX 2> /dev/null. # using this command we will not see the permission error messages.
  • ps ax | head -123 # head is used to show first 123 lines.
  • tail +123 # to show lines from 123 lines
    tail -123 # to show the last 123 liens
    tail -f /var/log/messages # to monitor the messages file in real time.
  • cp *.[ch] ~ # use regular expression to specify what kind of file you want to copy. Here means copy .c and .h files to home folder.
  • umask: displays or changes the permissions that are taken away, i.e., masked out, from the defaults requested at file creation.
  • ps:
    ps -e: processes of everybody
    ps e: command arguments in detail
    ps l: in long format, similar as ls -l.
    ps -o pid,state,args: to assign what information to output, here -o means options.
    ps -O pri: preloaded -o, pri is added to show basing on the default output.
    state: S=sleeping,R=running,T=stop(because S has assigned to sleeping, so use the second letter T for STOP)
    ppid: parent pid. from ps -l , the ppid can be got. it means all the process with same ppid are the child process of the process, whose pid is ppid. If kill this parent process, all the child process will be killed as well.
  • kill -SIG pid: totally33 kinds of Signal for pid.
  • job control:
    prog & # run a program in the background.
    jobs -l # list current background jobs. -l can return pids as well as job numbers.
    : move the current foreground job into the background and STOP it.
    bg %2: RESUME job number 2 in the background.
    fg %2: Resume job number 2 in the foreground.
    wait %2: wait job number 2 to complete.
    kill %2: terminate job number 2.
  • tar -tvf /the/tar/file.tar: list the contents in the tar file while not decompress it.
  • vi: ":file": return the some file information, including total lines.
  • awk: Command line text tool (especially edit file or texts with columns)
    $awk -F':' '{ printf "%-10s%5d\n", $1, $3 }' /etc/passwd
    Here, printf "%-10s%5d\n", $1, $3 -- the action, it's the format string of two arguments to printf, in the case,$1 is the username column in the password file, '-' means left justified. ':' is the separator.
    $fs=$(mount | awk ' $5 =="ext3" { print $3 }')
    Here, shows mounted file systems and we are interested in those whose file system typ is ext3, in which case we print the mount point ($3).

Regular Expression:

symbols meaning example matches
. any 1 char pean.t peanut
[] 1 character set waln[aeiou]t walnet
[-] 1 character range pec[a-l]n pecen
[^] not in set alm[^Oo] almend
* 0 or more instances penut peeeeenut
^ begin line ^begin beginning of line
$ end line end$ line end
\ quote next n\-\[\$ n-[$

.* any string n.*s newts;ns
[]* string from set ba[na]* banana
[ - - ] multiple ranges nu[m-os-u]s nuns

USING WITH "grep","sed"
\{m\} m occurrences [m-os-u]\{4\} stun
\{m,\} m or more occurrences x[0-9]\{3,\}y x121345y
\{m,n\} m to n occurrences x[0-9]\{3,7\}y x1213458y

USING WITH "egrep","awk"
? 0 or 1 instance chest?nut chesnut;chestnut
+ 1 or more instance p[ae]+nut paenut;peeenut
| alternate RE pattern Wal|Pea Wal or Pea
( ) RE group (Wal|Pea|Coco)nut walnut;Peanut;Coconut


\( \) save match \(..\)\(.*\)
\n nth saved match s/\(..\)/\2--\1/

$echo TUNUTS | sed 's/\(..\)/\2--\1/'

$sed -e 's/abc/def/g' -e 's/hij/klm/g' ./where/the/file # In the file, use def to replace abc, and use klm to replace hij.

Shell Programming:

  • '${varible}' is good practice
  • Judge statement:
    [[ ]] for string compare, including "abc" and numeric string "12", here x=9 and x="9" are same.
    (( )) for numerical compare.
    • Be careful of space in the judge statement. [[ $x = 9 ]]. there are " space " around [ ] =, same with (( )).
    • but when you assign x=9, there can not be space!!!!
    • [[ $x -eq 9 ]] , here -eq just for [[]].
  • $x=9
    $[[ $x == 9 ]]
    $echo $? # here $? shows correct or not. or the command runs successfully or not.
  • shell scripts: exit can be put at the last line. if 'exit 9' at the last line. In the shell, $echo $? will return 9.
  • A shell script called 'test', after it runs, $./run , you can not get any variables generated in the script. Because it runs in the sub shell. $. ./run will give you the variable, because, '.' means run the script in the current shell.
  • Subshell. in scripts
    for f in $(ls | grep -v 'gz$')
    ls -l ${f)
    ###this script just shows the files except .gz files. Here sub shell similar to pipe.

No comments:

My photo
London, United Kingdom

Facebook & Twitter