Find files older than FILE without including FILE

2019-07-07 04:36发布

I have a plan to use find to give me a list of files that are older than some FILE and then use xargs (or -exec) to move the files to somewhere else. Moving stuff is not a problem; | xargs mv {} ~/trash works fine.

Now, if I try to use ! -newer FILE, then FILE is included in the list, which I do not want!

The functionality of that command argument does indeed make sense logically, though, because 'not newer' could very well be interpreted as "same or older", like here:

$ find . ! -newer Selection_008.png -exec ls -l {} \;

includes the file from the compare argument:

-rw-r--r-- 1 and and 178058 2012-09-24 11:46 ./Selection_004.png
-rw-r--r-- 1 and and 16260 2012-09-21 11:25 ./Selection_003.png
-rw-r--r-- 1 and and 38329 2012-10-04 17:13 ./Selection_008.png
-rw-r--r-- 1 and and 177615 2012-09-24 11:53 ./Selection_005.png

(ls -l is only to show dates for illustrative purposes)

What I really need from find is an -older option, but none is listed in find(1)...

Of course, one could just pipe the output through grep -v, use sed, etc., or utilise an environment variable to reuse the filename (fx. for grep -v) so I can enjoy the DRY-principle, like

$ COMP=Selection_008.png find . ! -newer $COMP | grep -v $COMP | xargs ...

but it just seems to be a lot of writing for a oneliner and that is not what I am looking for.

Is there a shorter/simpler way than find or am I missing some option? I have checked the manpage, and searched Google and SO...

标签: shell find
3条回答
家丑人穷心不美
2楼-- · 2019-07-07 05:12

You can try this:

find . ! -newer $COMP ! -name $COMP
查看更多
在下西门庆
3楼-- · 2019-07-07 05:13

It is not exactly -older, but ! -samefile will exclude a specific file.

$ find . ! -newer Selection_008.png ! -samefile -exec ls -l {} \;

This will still catch a file exactly the same age of file, and because it is inode based, there may be caveats dealing with links, as well.

查看更多
家丑人穷心不美
4楼-- · 2019-07-07 05:29

Find doesn't seem to support this. You could delegate the test to test:

find . -exec test "{}" -ot "$COMP" \; -print

Not very efficient as it runs a fork for each file.

查看更多
登录 后发表回答