How to use find on dirs with white spaces?

前端 未结 3 1493
情话喂你
情话喂你 2020-12-19 21:25

Here is something strange.

mkdir -p \"1/2 3/4\"
touch 1/2\\ 3/4/file.jpg
for f in $(find . -type f -name \\*jpg); do echo \"${f}\"; done

Th

相关标签:
3条回答
  • 2020-12-19 21:42

    You can use the -print0 option along with xargs -0. For instance:

    find . -type f -name \*.jpg -print0 | xargs -0 echo
    

    This would work no matter the content of the file names (even newlines would be handled correctly).

    0 讨论(0)
  • 2020-12-19 21:50

    See the difference between:

    find . -type f -name \*.jpg -exec echo {} \;
    

    and:

    find . -type f -name \*.jpg -exec echo \"{}\" \;
    
    0 讨论(0)
  • It can be done in several ways, but I find it much better to do it this way:

    find . -type f -name \*.jpg | while read i ; do echo "Procesing $i..." ; done
    
    0 讨论(0)
提交回复
热议问题