Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I would like a Linux command to find files changed in last n seconds.

Is there a shell script or other tool that I can run from command line interface or gui?

share|improve this question

migrated from stackoverflow.com Jun 21 '11 at 17:47

This question came from our site for professional and enthusiast programmers.

5 Answers 5

Use find command like this:

find . -name "*.txt" -mtime -60s

To find all *.txt files modified in last 60 seconds.

share|improve this answer
    
Explicitly, "files modified between 0 and 60 seconds ago." –  dannysauer Jun 21 '11 at 19:27
3  
In linux, using find (from GNU findutils 4.4.2), I get an error with this command: find: missing argument to `-mtime'. However, I can get the desired behavior using -mmin and a decimal argument. I could not find any reference in the manpage for find for using s as an argument. –  dr jimbob Jun 4 '13 at 15:59

If you're monitoring a directory for changes to files, you probably want to use inotify-tools instead of some infinite polling loop.

share|improve this answer

Similar to what glenn suggested, if you want to find everything modified, say, in the time during which an installer process was running, it might be easier to do something like:

touch /tmp/checkpoint
<do installer stuff>
find / -newer /tmp/checkpoint

Then you don't have to do the time calculation; you just find things changed after the checkpoint file.

share|improve this answer

If you have a version of find that doesn't support -mtime -60s then a better solution is

touch -d '-60 seconds' /tmp/newerthan
find . -name "*.txt" -newer /tmp/newerthan
share|improve this answer

The solution with mtime specifying seconds doesn't work on my linux systems that use find --version == find (GNU findutils) 4.4.2.

I get the following error:

mycomputer:~/new$ find . -mtime -60s
find: missing argument to `-mtime'
mycomputer:~/new$ find . -mtime -60seconds
find: missing argument to `-mtime'

However, I can use -mmin (for modified in the last m minutes), and can it can take in a decimal argument; e.g., the following finds files modified in the last 30 seconds.

find . -mmin 0.5

So for example; creating files last modified 1s, 6s, 11s, ... ago for the past 120 seconds, this command finds:

mycomputer:~/new$ for i in $(seq 1 5 120); do touch -d "-$i seconds" last_modified_${i}_seconds_ago ; done
mycomputer:~/new$ find . -mmin 0.5
.
./last_modified_1_seconds_ago
./last_modified_26_seconds_ago
./last_modified_11_seconds_ago
./last_modified_16_seconds_ago
./last_modified_21_seconds_ago
./last_modified_6_seconds_ago

So if you really need it in seconds you can do something like:

localhost:~/new$ for i in $(seq 1 1 120); do touch -d "-$i seconds" last_modified_${i}_seconds_ago ; done
localhost:~/new$ N=18; find . -mmin $(echo "$N/60"|bc -l)
./last_modified_1_seconds_ago
./last_modified_9_seconds_ago
./last_modified_14_seconds_ago
./last_modified_4_seconds_ago
./last_modified_12_seconds_ago
./last_modified_13_seconds_ago
./last_modified_8_seconds_ago
./last_modified_3_seconds_ago
./last_modified_5_seconds_ago
./last_modified_11_seconds_ago
./last_modified_17_seconds_ago
./last_modified_16_seconds_ago
./last_modified_7_seconds_ago
./last_modified_15_seconds_ago
./last_modified_10_seconds_ago
./last_modified_6_seconds_ago
./last_modified_2_seconds_ago
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.