Find the answer to your Linux question:
Results 1 to 3 of 3
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1

    [SOLVED] redirect to multiple files?

    Hello everyone. I feel kind of embarrassed posting here, but this is technically a scripting sub-forum.

    Here is the problem
    I have a folder with various files which include .txt files as well

    How can i redirect same content to each of the .txt files in the folder?

    I have tried

    $ echo "hello" > *.txt
    -bash: !": event not found
    also cat ~/otherdir/test.txt > *.txt
    -bash: *.txt: ambiguous redirect
    Can anyone help me with this?

    Ok i solved it

    #! /bin/bash
    for file in *.txt
       echo "Text that needs to be written" > $file
    Last edited by SQuIDers; 11-12-2010 at 12:59 PM.

  2. #2
    Linux Guru Cabhan's Avatar
    Join Date
    Jan 2005
    Seattle, WA, USA
    This is one way to do it. But it won't always work. For instance, what if you were running a real command that actually did something (like add a user). You don't want to run the command multiple times, but you want the output in multiple files.

    The solution is to use the "tee" program. "tee" takes all of its input and writes it to multiple files, as well as to standard output:
    echo 'Hello, world!' | tee file1 file2 file3

  3. #3
    This is actually the first time i heard of command tee.

    Thank you very much!

  4. $spacer_open

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts