Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to search for a particular word in only .csv files using Linux command line?

Tags:

linux

I have a directory with multiple levels of sub-directories. These sub-directories contain a combination of .avi, .txt and .csv files.

I want to be able to search through all the .csv files for a particular word. Right now I am using the following command: grep -r "word" *

This also searches through the .avi and .txt files. How do I limit the search to just .csv files?

Apologies if this question has been asked before, I was not able to find it.

like image 660
DriedNuts Avatar asked Jan 20 '26 03:01

DriedNuts


2 Answers

The solution which worked for me was: find . -type f -iname "*.csv" -print0 | xargs -0 grep "word"

Reference: https://unix.stackexchange.com/questions/42407/pipe-find-into-grep-v

Further, if you want to count the number of "words" in each .csv file, use: find . -type f -iname "*.csv" -print0 | xargs -0 grep -c "word"

If you want to count the total number of times "word" appears in all .csv files in a directory, use: find . -type f -iname "*.csv" -print0 | xargs -0 grep -o "word" * | wc -l

like image 134
DriedNuts Avatar answered Jan 22 '26 21:01

DriedNuts


you can find all csv file and then grep only in founded files:

find <your directory path> -name "*.csv" -type f | xargs grep "word"
like image 36
Alex Avatar answered Jan 22 '26 21:01

Alex