Find files older than FILE without enabling FILE

I have a plan to use find to provide me with a list of files older than FILE, and then use xargs (or -exec ) to move the files to another location. Moving things is not a problem; | xargs mv {} ~/trash | xargs mv {} ~/trash working fine.

Now if I try to use ! -newer FILE ! -newer FILE , then FILE included in the list that I do not need!

The functionality of this command argument really makes sense logically, though, because β€œno newer” could well be interpreted as β€œthe same or older,” as here:

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

contains the file from the comparison 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 only shows dates for illustrative purposes)

What I really need from find is the -older parameter, but none of them are specified in find(1) ...

Of course, you can just pass the output through grep -v , use sed , etc. or use an environment variable to reuse the file name (fx. for grep -v ) so that I can enjoy the DRY principle, for example

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

but it seems like this is a lot of writing for oneliner, and that is not what I am looking for.

Is there a shorter / easier way than find or am I missing an option? I checked the man page and searched for Google and SO ...

+3
source share
3 answers

You can try the following:

 find . ! -newer $COMP ! -name $COMP 
+4
source

This is not exactly -older , but ! -samefile ! -samefile excludes a specific file.

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

This will still catch the file of exactly the same file age, and because it is based on inode, there may also be caveats regarding links.

+4
source

Find does not seem to support this. You can delegate the test to test :

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

Not very efficient as it launches fork for each file.

+1
source

Source: https://habr.com/ru/post/905915/


All Articles