Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

grep files with whitespaces in filename

Tags:

regex

grep

bash

I have a list contains filenames. Some of filenames contain whitespaces:

./folder/folder/some file name.ext

I need to grep each of these files:

cat filelist | while read i; do grep "pattern" $i; done

Obviously grep fails because of whitespaces:

grep ./folder/folder/some: No such file or directory
grep file: No such file or directory
grep name: No such file or directory

I've tried to escape whitespaces like:

:%s/some file name/some\ file\ name/g

but no luck. How can I perform my operation? Thanks!

like image 442
hdf Avatar asked Jan 29 '26 06:01

hdf


1 Answers

You can use this loop:

while read -r i; do grep "pattern" "$i"; done < filelist

Using a pipe with cat is error prone and BASH will treat strings with space as separate arguments.

like image 136
anubhava Avatar answered Jan 31 '26 21:01

anubhava



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!