Saturday, November 17, 2007

find, An Interview Question

A colleague of mine was one of the job interviewers and he posted the following question to the interviewee: "Can you tell me how you can remove files that have not been modified for more than 30 days?"

Sad to say that the interviewee was not able to provide the exact command. Sometime it is just not possible to remember those cryptic syntax of UNIX commands. What if the interviewee is able to answer the question, does it mean that he/she know 'find' command well enough ? Also, is the interviewee capable of doing other system administrative jobs? If I were the interviewer, I would follow up with more questions:

  1. Can you tell me how you can remove files that have not been modified for more than 30 days? (original question)
  2. Can you tell me how to find those files that was modified between the last 30 and 60 days?
  3. Can you locate those files that are more than 200 days old, but last modified on the weekends?
  4. Can you locate those files that was last modified on 4 July 2007 ?

Let's create tonnes of files (365 of them) for our experiment using this Tcl script

set now [clock seconds]
for { set i 1 } { $i <= 365 } { incr i } {
    set time [clock format [expr {$now-86400*$i}] -format {%Y%m%d%H%M}]
    exec touch -t $time day-$i
}

    My answers to the above questions:
  1. find . -type f -mtime +30 -exec rm -i {} \; In this case, I include -i in 'rm' to let the system to prompt me before I delete the files. If you are dealing with a lot of files, you may want to use 'rm -f'. Make sure you are deleting the files that you are suppose to delete.
  2. find . -type f \( -mtime +30 -a -mtime -60 \) -ls
  3. find . -type f -mtime +200 | ./wk 'wk' is a simple Tcl script to determine whether the file is last modified on weekend
    while { [gets stdin line] >= 0 } {
          file stat $line fstat
          set wk [clock format $fstat(mtime) -format %a]
          if { [string equal $wk Sun] || [string equal $wk Sat] } {
                  puts $line
          }
    }
    
  4. You may think that 'ls -lR | grep "4 July"' is the solution. However, this may not be fool proof 'cos if the file is more than 180 days old, 'ls -l' will show the "4 July 2007" time format. If not, it will show "4 July 17:11" time format. That means, you may have more than one occurrence of "4 July" in the 'ls -lR'. Worse still, you can pick up files modified on "14 July" or "24 July"

    What you need is a program to calculate the number of days old and use the output as the input to find. A sample Tcl program (dayold) like this will do the job

    set t [clock scan [lindex $argv 0]]
    set now [clock seconds]
    set days [expr ($now-$t)/86400]
    puts $days
    
    Below shows you how it can be combined with find
    $ ./etime 2007-07-04
    136
    
    $ find . -mtime `./etime 2007-07-04`
    ./day-136
    
    $ ls -l ./day-136
    -rw-------   1 chihung  gdz            0 Jul  4 17:11 ./day-136
    

If I were the interviewer, I would expect the interviewee (if he/she is apply for a system admin job) to be able to program in scripting language. It really does not matter what language as long as he/she can master that language well.

Labels: ,

0 Comments:

Post a Comment

<< Home