Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find files older than FILE without including FILE

Tags:

find

shell

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...

like image 904
dubberx Avatar asked Jan 21 '26 11:01

dubberx


2 Answers

You can try this:

find . ! -newer $COMP ! -name $COMP
like image 93
sleepsort Avatar answered Jan 22 '26 23:01

sleepsort


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.

like image 26
schamp Avatar answered Jan 23 '26 00:01

schamp



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!