In a folder I have files with nanoseconds timestamps.:


I want to keep only file that are create 10 secondes from the lastest file in that directory (FileLastestFile.ext).

I try to use the command find .. -newermt, but I don't know which way to specify the FileLastestFile.ext timestamp as origin.

  • 1
    Does the latest file always have name FileLastestFile.ext, or you just assumed it? – Prvt_Yadav Feb 18 at 8:31
  • We don't know that the last file is FileLastestFile.ext but we want to use it as reference – user3262532 Feb 18 at 8:48

I think this shows how the time from FileLatestFile.ext could be used, whereby %x represents last access, %y reprecents last modification and %z represents last status change.

$ find /folder/ -mindepth 1 -newerct "$(stat --printf=%y /folder/FileLatestFile.ext)" 2>/dev/null

Now the trick is to subtract 10 sec. from that.

| improve this answer | |

With zsh:

#! /bin/zsh -
zmodload zsh/stat

zstat -F%s.%N -A mtime_of_newest +mtime -- $newest || exit
(( cutoff = mtime_of_newest - 10 ))
older_than_cutoff() {
  local mtime
  zstat -F%s.%N -A mtime +mtime -- ${1-$REPLY} && (( mtime < cutoff ))
rm -rf -- *.ext(+older_than_cutoff)

(note that for symlinks, it's the mtime of the file after symlink resolution that is considered. If you want the mtime of the symlink itself to be considered instead, remove the - in -om and add the -L option to both zstat invocations).

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.