Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
877 views
in Technique[技术] by (71.8m points)

linux - How to find files modified in last x minutes (find -mmin does not work as expected)

I'm trying to find files modified in last x minutes, for example in the last hour. Many forums and tutorials on the net suggest to use the find command with the -mmin option, like this:

find . -mmin -60 |xargs ls -l

However, this command did not work for me as expected. As you can see from the following listing, it also shows files modified earlier than 1 hour ago:

-rw------- 1 user user   9065 Oct 28 23:13 1446070435.V902I67a5567M283852.harvester
-rw------- 1 user user   1331 Oct 29 01:10 1446077402.V902I67a5b34M538793.harvester
-rw------- 1 user user   1615 Oct 29 01:36 1446078983.V902I67a5b35M267251.harvester
-rw------- 1 user user  72365 Oct 29 02:27 1446082022.V902I67a5b36M873811.harvester
-rw------- 1 user user  69102 Oct 29 02:27 1446082024.V902I67a5b37M142247.harvester
-rw------- 1 user user   2611 Oct 29 02:34 1446082482.V902I67a5b38M258101.harvester
-rw------- 1 user user   2612 Oct 29 02:34 1446082485.V902I67a5b39M607107.harvester
-rw------- 1 user user   2600 Oct 29 02:34 1446082488.V902I67a5b3aM465574.harvester
-rw------- 1 user user  10779 Oct 29 03:27 1446085622.V902I67a5b3bM110329.harvester
-rw------- 1 user user   5836 Oct 29 03:27 1446085623.V902I67a5b3cM254104.harvester
-rw------- 1 user user   8970 Oct 29 04:27 1446089232.V902I67a5b3dM936339.harvester
-rw------- 1 user user 165393 Oct 29 06:10 1446095400.V902I67a5b3eM290158.harvester
-rw------- 1 user user 105054 Oct 29 06:10 1446095430.V902I67a5b3fM265065.harvester
-rw------- 1 user user   1615 Oct 29 06:24 1446096244.V902I67a5b40M55701.harvester
-rw------- 1 user user   1620 Oct 29 06:24 1446096292.V902I67a5b41M337769.harvester
-rw------- 1 user user  10436 Oct 29 06:36 1446096973.V902I67a5b42M707215.harvester
-rw------- 1 user user   7150 Oct 29 06:36 1446097019.V902I67a5b43M415731.harvester
-rw------- 1 user user   4357 Oct 29 06:39 1446097194.V902I67a5b56M446687.harvester
-rw------- 1 user user   4283 Oct 29 06:39 1446097195.V902I67a5b57M957052.harvester
-rw------- 1 user user   4393 Oct 29 06:39 1446097197.V902I67a5b58M774506.harvester
-rw------- 1 user user   4264 Oct 29 06:39 1446097198.V902I67a5b59M532213.harvester
-rw------- 1 user user   4272 Oct 29 06:40 1446097201.V902I67a5b5aM534679.harvester
-rw------- 1 user user   4274 Oct 29 06:40 1446097228.V902I67a5b5dM363553.harvester
-rw------- 1 user user  20905 Oct 29 06:44 1446097455.V902I67a5b5eM918314.harvester

Actually, it just listed all files in the current directory. We can take one of these files as an example and check if its modification time is really as displayed by the ls command:

stat 1446070435.V902I67a5567M283852.harvester

  File: ‘1446070435.V902I67a5567M283852.harvester’
  Size: 9065        Blocks: 24         IO Block: 4096   regular file
Device: 902h/2306d  Inode: 108680551   Links: 1
Access: (0600/-rw-------)  Uid: ( 1001/   user)   Gid: ( 1027/   user)
Access: 2015-10-28 23:13:55.281515368 +0100
Modify: 2015-10-28 23:13:55.281515368 +0100
Change: 2015-10-28 23:13:55.313515539 +0100

As we can see, this file was definitely last modified earlier than 1 hour ago! I also tried find -mmin 60 or find -mmin +60, but it did not work either.

Why is this happening and how to use the find command correctly?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I can reproduce your problem if there are no files in the directory that were modified in the last hour. In that case, find . -mmin -60 returns nothing. The command find . -mmin -60 |xargs ls -l, however, returns every file in the directory which is consistent with what happens when ls -l is run without an argument.

To make sure that ls -l is only run when a file is found, try:

find . -mmin -60 -type f -exec ls -l {} +

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...