7

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

Kathryn
  • 71
  • check this http://askubuntu.com/questions/55325/how-to-use-grep-command-to-find-text-including-subdirectories – devav2 Sep 24 '12 at 14:55

2 Answers2

7

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'
January
  • 35,952
  • You should add -type f so that only files will be displayed... in your example folder with name "bla.txt" will be listed too. But in real life you don't have folder with names like that :P – Wolfy Sep 24 '12 at 20:53
  • Naturally. But I never do that. – January Sep 25 '12 at 05:06
4

I actually solved it by using find . type -f instead of ls

Anwar
  • 76,649
Kathryn
  • 41