Ubuntu Linux: find files between specific times?

06
2014-04
  • digitaltoast

    I found an SO called using Find/Grep to search files between specific time of day

    Based on that and a Unix SE called Grep command to find files containing text string and move them I ended up with:

    find . -type f -mtime -20 | grep -v -e " \(0[012345]\|18\|19\|2[0123]\)" | xargs mv -t daytime/
    

    But it's moving ALL the files. Does it make a difference that I'm using Ubuntu?

    All I want to do is move all files between 6am and 6pm to another directory. Any suggestions would be appreciated.

  • Answers
  • mdsl

    Actually, find already has this functionality:

    find . -newermt "2013-01-01 00:00:00" ! -newermt "2013-01-02 00:00:00"
    

    From the manpage:

    -newerXY reference
           Compares  the timestamp of the current file with reference.  The
           reference argument is normally the name of a file  (and  one  of
           its  timestamps is used for the comparison) but it may also be a
           string describing an absolute time.  X and  Y  are  placeholders
           for other letters, and these letters select which time belonging
           to how reference is used for the comparison.
    
           a   The access time of the file reference
           B   The birth time of the file reference
           c   The inode status change time of reference
           m   The modification time of the file reference
           t   reference is interpreted directly as a time
    
           Some combinations are invalid; for example, it is invalid for  X
           to  be t.  Some combinations are not implemented on all systems;
           for example B is not supported on all systems.  If an invalid or
           unsupported  combination  of  XY  is  specified,  a  fatal error
           results.  Time specifications are interpreted as for  the  argu‐
           ment  to the -d option of GNU date.  If you try to use the birth
           time of a reference file, and the birth time  cannot  be  deter‐
           mined,  a  fatal  error  message results.  If you specify a test
           which refers to the birth time of  files  being  examined,  this
           test will fail for any files where the birth time is unknown.
    

  • Related Question

    linux - Find words in many files
  • ant2009

    I am looking for this struct messages_sdd_t and I need to search through a lot of *.c files to find it. However, I can't seen to find a match as I want to exclude all the words 'struct' and 'messages_sdd_t'. As I want to search on this only 'struct messages_sdd_t' The reason for this is, as struct is used many times and I keep getting pages or search results.

    I have been doing this without success:

    find . -type f -name '*.c' | xargs grep 'struct messages_sdd_t'
    

    and this

    find . -type f -name '*.c' | xargs egrep -w 'struct|messages_sdd_t'
    

    Many thanks for any suggestions,


  • Related Answers
  • Ivan Petrushev

    Use ack:

    ack "struct messages_sdd_t" *.c

    It is recoursive by default and it is a lot faster than grep (according to my observation) when searching trough a directory containing tons of source code.

    If you don't want to use ack, grep should be fine too:

    grep -r "struct messages_sdd_t" *.c

  • Ignacio Vazquez-Abrams
    find . -type f -name '*.c' -exec grep 'struct\s\+messages_sdd_t' {} \;
    

    Or if you only care about the filename:

    find . -type f -name '*.c' -exec grep -q 'struct\s\+messages_sdd_t' {} \; -print
    
  • Daniel Andersson
    grep -R --include='*.c' 'struct messages_sdd_t' .
    

    This will search recursively and use grep itself to pick only .c files.

    Consider adding -H to always write the file name with the line match.