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.

2
  • 1
    Why is everything double double quoted?
    – jesse_b
    Commented Jul 12, 2019 at 21:45
  • 2
    It looks like you're trying to nest double-quotes inside double-quotes; that doesn't work. In general, trying to construct command strings and then execute them isn't safe. See this Q&A for a much better approach. Commented Jul 12, 2019 at 21:46

1 Answer 1

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.

5
  • 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
    Commented Jul 12, 2019 at 22:15
  • 2
    so x-y problem.
    – jesse_b
    Commented Jul 12, 2019 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?
    – user313992
    Commented Jul 12, 2019 at 23:17
  • @mosvy fair point Commented Jul 12, 2019 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).
    – user313992
    Commented Jul 12, 2019 at 23:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .