3
find /log/ -mtime -31 -type f -name ""*data.txt"" -printf ""cp -p %p /Backup/%Td/\n"" | sh

I am trying to set up this command in a cron job and it's complaining about unknown predicate -p. Not really a cron issue, looks like a shell-scripting problem.

How can I fix this? Quoting -p doesn't help.

jesse_b
  • 37,005

1 Answers1

2

Since the quoted strings are double...double quoted they are actually not quoted at all.

You have:

find /log/ -mtime -31 -type f -name ""*data.txt"" -printf ""cp -p %p /Backup/%Td/\n"" | sh

This should be:

find /log/ -mtime -31 -type f -name "*data.txt" -printf "cp -p %p /Backup/%Td/\n" | sh

You probably shouldn't do this though. You should use find -exec to copy these files.

jesse_b
  • 37,005
  • 1
    @steeldriver - exactly! I need the file modification times in order to copy them into appropriate directories and I could not find a way to combine -exec with what I am trying to do. That's why I am trying to use -printf. Let me try your suggestion and report back. – GA_train Jul 12 '19 at 22:15
  • 2
    so x-y problem. – jesse_b Jul 12 '19 at 22:30
  • @steeldriver what if someone creates a file named /log/path/to/." -r foo; echo rm -fr ~; : "data.txt? how would your quoting help? –  Jul 12 '19 at 23:17
  • @mosvy fair point – steeldriver Jul 12 '19 at 23:37
  • 1
    @GA_train A better idea would probably be: find /log/ -mtime -31 -type f -name '*data.txt' -printf '%Td %p\0' | xargs -0 sh -c 'for a; do printf "{%s} " cp -p "${a#* }" "/Backup/${a%% *}/"; echo; done' sh (untested; remove the printf "{%s} " and the echo; if right). –  Jul 12 '19 at 23:42