Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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

2 Answers 2

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' {} \;

References:

share|improve this answer
    
I wouldn't use so many newlines, but yes. –  Ignacio Vazquez-Abrams May 3 '12 at 3:41
1  
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

Your Answer

 
discard

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.