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

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'
  • 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 5:06
4

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

  • 7
    Exactly; but that precisely is solution #1. – January Sep 24 '12 at 15:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.