Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. 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

I want to add some text to over 200,000 files I am trying this

find . -name *.txt -print | xargs -I % echo "hello world" >> %

But nothing is happening. When i run find . -name *.txt it work by itself as does echo "hello world" >> myfile.txt

share|improve this question
The shell does the >> % part, not xargs. Did it create a file called %? – Mikel May 3 '12 at 3:37
Yes I now have a % and {} file =). Mikel solution works – Daveo May 3 '12 at 3:45
up vote 14 down vote accepted

The shell is expanding the >> % part before xargs sees it.

If you need to do shell redirections, you'll have to try something like this:

find . -name "*.txt" -exec sh -c '
    echo "hello world" >> "$0"
    ' {} \;

How it works:

  1. find replaces {} with each file that it matches
  2. bash -c "some command" arg0... sets $0... inside the "some command" script

Alternatively, you could use a command such as sed that doesn't rely on >> e.g.

find . -name "*.txt" -exec sed -i -e '$a\
hello world' {} \;


share|improve this answer
I wouldn't use so many newlines, but yes. – Ignacio Vazquez-Abrams May 3 '12 at 3:41
That sed command will append 'hello world' after each line. You need sed -i '$ahello world' to put it only at the end of the file (my testing with GNU sed did not need the backslash-newline) – camh May 3 '12 at 3:57
Gah! Thanks! I meant that, but forgot the $. :-) – Mikel May 3 '12 at 3:59

With GNU Parallel you can do:

find . -name *.txt -print | parallel 'echo "hello world" >> {}'

You can install GNU Parallel simply by:

wget http://git.savannah.gnu.org/cgit/parallel.git/plain/src/parallel
chmod 755 parallel
cp parallel sem

Watch the intro videos for GNU Parallel to learn more: https://www.youtube.com/playlist?list=PL284C9FF2488BC6D1

share|improve this answer

The original command you tried ....

find . -name *.txt -print | xargs -I % echo "hello world" >> %

needs to be changed to

find . -name "*.txt" | xargs -I {} sh -c "echo 'hello world' >> '{}' "
share|improve this answer
Why ​​​​​​​​​​? – don_crissti Mar 9 at 17:44
or you can use ..... find . -name "*.txt" | xargs -i sh -c "echo 'hello world' >> '{}' " – Raj Velayudhan Mar 9 at 17:48
The above commands will add "hello world" to the end of each file found by the find command. – Raj Velayudhan Mar 9 at 17:48

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.