Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to perform shell "for" command over files with spaces in names?

I find myself frequently doing the following:

for f in `find -foo -bar -baz`; do
  process "$f"
done

This of course doesn't work for file names with spaces. How can I handle such cases?

like image 865
AdSR Avatar asked Feb 02 '26 14:02

AdSR


1 Answers

Find and xargs work well together. find can print the names of the files with a \0-delimiter (option print0) and xargs can read them in that format (option -0):

find . -type f -print0 | xargs -0 echo
like image 86
soulmerge Avatar answered Feb 04 '26 05:02

soulmerge



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!