Find the answer to your Linux question:
Results 1 to 3 of 3
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 ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined! SQuIDers's Avatar
    Join Date
    May 2006
    Posts
    4

    [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

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


    Ok i solved it

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

  2. #2
    Linux Guru Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,252
    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:
    Code:
    echo 'Hello, world!' | tee file1 file2 file3

  3. #3
    Just Joined! SQuIDers's Avatar
    Join Date
    May 2006
    Posts
    4
    This is actually the first time i heard of command tee.

    Thank you very much!

  4. $spacer_open
    $spacer_close

Posting Permissions

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