Ubuntu – Grep through subdirectories

bashcommand linegrep

Add a string to a text file from terminal

I've been looking at this thread. The solution (number 2, with ls grep) works perfectly for files called .txt in the current directory. How about if I wanted to search through a directory and the subdirectories?

For example, I have to search through a directory that has many subdirectories, and they have many subdirectories etc.

I'm new to Linux sorry, so I'm not sure if this is the right place

Best Answer

Use the -R (equivalent to --recursive) option to grep.

EDIT: after reading the thread, in the combination ls /mydata | grep txt$ you do not need recursive grep, but recursive ls. You do not grep the files; you grep the output of ls, which happens to be a list of files.

"Recursive ls" is called find :-)

find /mydata -type f | grep txt$

or, better,

find /mydata -type f -name '*.txt'
Related Question