Consider a directory with typical Microsoft Windows filenames:
New Document.txt Foo.doc Foo - Copy.doc
I'd like to do something on each file such as:
for sendfile in $(find ${POLLDIR} -type f -mmin +1 -print0) do echo \"${sendfile}\" ls -l "${sendfile}" and-so-on if success_above then mv "${sendfile}" "${donedir}/." fi done
Please note that I don't want to just run 1 command with "${sendfile}" as the argument. I need the loop to do error checking and other things (like moving "${sendfile}" on success and logging on failure).
What is a "correct" construct to escape/quote the filenames from find so I can use them in a for look like in the ls
command above? If possible, I'd like to avoid storing the filenames in a temp file one by one.
I don't think that find -printf '"%p"\n'
as suggested by triplee in the comments to question [ How do I use find when the filename contains spaces? ] would work in a for foo in $(...) do
construct.
I think replacing "illegal" chars with ?
would work for me in this case, but it would be very ugly. The for loop ultimately processes files in ${POLLDIR} and then moves them when done, so the chance of "Foo bar.txt" colliding with "Foo-bar.txt" is 0 (-ish).
My best attempt so far is:
for sendfile in $(find ${POLLDIR} -type f -mmin +1 -print | tr ' ' '?') do ... done
Any cleaner suggestions?
for loop
andfind
construction together. I think you want something likefind "$POLLDIR" -type f -mmin +1 -exec sh -c 'mv -- "$@" ./target' _ {} +
You can use-name
flag to specify different extensions etc. No need for the loop at all. – Valentin Bajrami Oct 09 '14 at 14:46-exec
arguments. – MattBianco Oct 09 '14 at 14:51-exec
IMHO. – MattBianco Oct 09 '14 at 14:58