looping through files with spaces in the filenames in bash

I sometimes have to transfer files with spaces in the names, I like using a for loop, but the usual way doesn't work.

Usual way:

for file in `find . -type f|grep .ext$` do /do/something/to $file done

To get around this I use a while loop with a read instead. Using the read will read to the end of the line, enclosing within quotes escapes the spaces.

Unusual way:

find . -type f|grep .ext$ |while read file do /do/something/to "$file" done 

Or if you don't like using more than one line...and enjoy the unreadable (I'm transferring all my avi files to another computer in this example)

find . -name \*.avi |while read file; do scp "$file"; done

The above could be done using find -exec as well:

find . -name \*.avi -exec scp "{}" \;



Add new comment

This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.

About the Author...

I published a Thing on @thingiverse! https://t.co/xK6XzN7Cbh #thingalert Fri Jun 12 17:08:46 +0000 2020

Slides from LISA 2019 Linux systems troubleshooting #LISA2019 https://t.co/D4dMKflK6R Tue Oct 29 05:59:30 +0000 2019

https://t.co/AGeihMALAv configuring grub2 with EFI Fri Sep 13 05:20:01 +0000 2019

I published a Thing on @thingiverse! https://t.co/IYpRyEb7Hz #thingalert Tue Jul 23 19:27:57 +0000 2019

Nokogiri install on MacOSX https://t.co/v3An0miW9L Fri Jul 12 15:06:49 +0000 2019