How to execute the “for” command for commands with files with spaces in names?

I often do the following:

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

This, of course, does not work for file names with spaces. How can I handle such cases?

+3
source share
5 answers
find . -type f | while read file; do 
     process "$f"
done;
+2
source

Find and xargs work together. find can print file names with \0-delimiter (option print0), and xargs can read them in this format (option -0):

find . -type f -print0 | xargs -0 echo
+4
source

find, exec

find -foo -bar -baz -exec process '{}' \;

IFS ( )

+1

bash 4

shopt -s globstar
for file in /path/**
do
  process "$file"
done
+1

for , .

Then inside the loop I will replace this particular line with a space.

Example:

list=`find -foo -bar -baz | tr ' ' 'µ'`
for fx in $list ; do
    f=`echo $fx | tr 'µ' ' '`
    process "$f"
done
0
source

Source: https://habr.com/ru/post/1733002/


All Articles