Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

find: invalid predicate when using -newermt

Tags:

linux

command

I am using a find command to get files between a certain time and tar it with the following command,

  find /lag/cnn -max-depth 3 -newermt "2013-12-19 00:00" -o -type f -newermt "2013-12-16 00:00" -print0 | 
   xargs -0 tar acf out.tar.gz

but when i run this command am getting: find: invalid predicate `-newermt' . what is the problem? and how i can work around this.

UPDATES: What i am actually trying to do is, path is (used ls -lrt /lag/cnn/*/*):

        /lag/cnn/Example1/one/a.tar.gz
        /lag/cnn/Example1/two/a.tar.gz
        /lag/cnn/Example1/three/a.tar.gz
         /lag/cnn/Example2/one/a.tar.gz

I am using grep for Example1 and got a list in sample.txt as follows,

        /lag/cnn/Example1/one/a.tar.gz
       /lag/cnn/Example1/two/a.tar.gz
       /lag/cnn/Example1/three/a.tar.gz

from this sample.txt i want to tar files based on time.since touch doesn't work on a file. i opted find command.thing is i need to do this from root directory.

          touch /lag/cnn/*/* start -d "2013-12-19 00:00"

will not work for sure. So is there any way to read files between a certain time and tar it or how to use this touch to use -newer and find files between certain time.

like image 886
user2572985 Avatar asked Dec 21 '13 06:12

user2572985


2 Answers

Your version of find doesn't support the -newermt predicate, so you cannot use it. As a workaround, you could use the -newer predicate. That predicate needs a file as a reference: instead of an absolute modification date, it will use the modification date of the file. You can create appropriate "marker files" for this purpose, for example:

touch /tmp/mark.start -d "2013-12-19 00:00"
touch /tmp/mark.end -d "2013-12-16 00:00"

And then rewrite using -newer predicate:

find /some/path -newer /tmp/mark.start

Btw it looks like your conditions were wrong: you have -newermt twice with different dates, which in your example would get all files newer then the older date, ignoring the newer date. Maybe you wanted to do something like this instead:

find /some/path -newer /tmp/mark.start ! -newer /tmp/mark.end

Finally, your tar won't work if the argument list is too long and xargs splits to multiple executions, because all executions will recreate the tar file. You need to use the -T flag of tar instead of xargs:

find /some/path -print0 | tar acf out.tar.gz --null -T-
like image 155
janos Avatar answered Sep 18 '22 21:09

janos


Find files newer than "start" and older than "end"

touch /tmp/mark.start -d "2016-02-16 00:00"

touch a -d "2016-02-15 00:01"

touch b -d "2016-02-16 00:01"

touch c -d "2016-02-17 00:00"

touch d -d "2016-02-18 00:00"

touch e -d "2016-02-19 00:01"

touch /tmp/mark.end -d "2016-02-19 00:00"

Command: find . -type f -newer /tmp/mark.start ! -newer /tmp/mark.end

========================================================================

Output:

-bash-3.2$ find . -type f -newer /tmp/mark.start ! -newer /tmp/mark.end

./d

./b

./c

-bash-3.2$
like image 35
Star202 Avatar answered Sep 21 '22 21:09

Star202