0

Recently have faced with an issue during bulk copying files that contains '$' symbol in the name. What I'm trying to achieve, I have bunch of sub-sub folders containing a lot of files inside, but I'm interesting only in some of them, e.g. *.html files. I want copy all of html files into new dir.

My first command for this: find . -name \*.html -exec sh -c "cp {} newDir/newFileName_<randomVal>.html" \; <- this return me proper number (seems to) of copied files. But also in console I'm getting errors like 'file not found' to those that containing '$' symbol..

Next I changed a 'cp {}' sub-command to ... -c "cp '{}' ..." in order to get filename as a string value. No errors appear now, but using this command I'm getting duplicates of the files, e.g. I know that inside folders 10 html files, but after executing a command getting 20 html files..

So, is there any another way to achieve desirable result, or what's wrong with this command?

10x

no_one
  • 21
  • 1
  • It's unclear without an example what's wrong exactly, but the `sh -c` argument should definitely be in single, not double, quotes. I suspect this also explains the symptoms you are describing. – tripleee Feb 04 '18 at 13:01
  • `sh -c "cp {} newDir/newFileName_.html"` is dangerous. It can lead to loss of data if someone with malicious intentions literally create a file, say, named `$(rm ~)`. Always do `out-of-band` parameter passing like `sh -c 'cp "$1" somestuff' _ {}` – sjsam Feb 04 '18 at 13:09
  • That said, the problem mentioned in the question too is stemming from the fact that `{}` is passed directly to `cp` Consider a file named `$delta.html` When `{}` is used as is inside copy, variable expansion would take place that replaces `$delta` with corresponding value, and nothing if such a variable is not defined. – sjsam Feb 04 '18 at 13:20
  • 1
    hm.. @sjsam thanks! really valuable remark will take it under consideration! – no_one Feb 04 '18 at 13:20
  • 1
    Thanks @tripleee and Sjsam seems the issue was indeed in single quotes. And of course insecure code :D Sorry, and up vote your comments, due to lack of rep.. – no_one Feb 04 '18 at 13:26

0 Answers0