Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

share|improve this question
check this… – devav2 Sep 24 '12 at 14:55

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'
share|improve this answer
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

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

share|improve this answer
Exactly; but that precisely is solution #1. – January Sep 24 '12 at 15:00

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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