I'm trying to loop through all the files in a directory which contain a certain word in filename using bash script.
The following script loops through all the files in the directory,
cd path/to/directory
for file in *
do
echo $file
done
ls | grep 'my_word' gives only the files which have the word 'my_word' in the filename. However I'm unsure how to replace the * with ls | grep 'my_word' in the script.
If i do like this,
for file in ls | grep 'my_word'
do
echo $file
done
It gives me an error "syntax error near unexpected token `|'". What is the correct way of doing this?
for file in $(ls | grep 'my_word'); do, but usingfindor a glob` is still the correct way to go.