How to use find when there are spaces in the directory names?
I need to find all .nfo
files in my media directory so I can use sed
to change some parts. The problem is my folders have spaces in the names.
find /media/media1/HDTV -name \*.nfo -type f
media/media1/HDTV/Band of Brothers/Season 1/
…
The output file won't found by a sed
command to change the strings I want.
find
doesn't care about special characters in file names, but the program that's parsing the output of find
might. If you're using xargs
, use the -print0
option to find
, and the -0
option to xargs
. This tells find
and xargs
to use null characters (which cannot appear in file names) as a separator between file names, and xargs
not to do any other parsing which would mangle file names containing spaces.
find /media/media1/HDTV -name '*.nfo' -type f -print0 |
xargs -0 sed -i 's/pattern/replacement/'
Another way to invoke a command on many files is to use find
only.
find /media/media1/HDTV -name '*.nfo' -type f -exec sed -i 's/pattern/replacement/' {} +