Find the answer to your Linux question:
Results 1 to 5 of 5
Hello, I was wondering how to do the following script. while not done open file write to end of file "hello" save / close file next file I would like ...
  1. #1
    Just Joined!
    Join Date
    Sep 2008
    Posts
    5

    recursive file append

    Hello,

    I was wondering how to do the following script.

    while not done

    open file

    write to end of file "hello"

    save / close file

    next file

    I would like the script to edit every file *.* in a directory I choose, as well as every child directory.

    ./run_script /test/hello/test/script/

    Thank you for your time

    DL

  2. #2
    Linux Newbie
    Join Date
    Jul 2008
    Posts
    181
    You can use "file" to print a list of all files matching certain criteria in and below the given starting directory and pass this list to "sed" (or "awk", or something similar) in order to append text to those files.

  3. #3
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    Where are you having difficulty with this? Or are you just looking for general tools to get you started?

    The 'find' program will allow you to find files in a subtree starting at some directory that match a given pattern. 'cat' will print out a file, and 'echo' will allow you to print whatever string of text you want. You can redirect output of a command to append to a file by using '>>'. For example:
    Code:
    cat file1 >> file2 # append the contents of file1 to file 2
    Does that help you at all?
    DISTRO=Arch
    Registered Linux User #388732

  4. #4
    Just Joined!
    Join Date
    Sep 2008
    Posts
    5
    I was having trouble in the sense that I have no clue where to start or how to do it.


    "cat file1 >> file2 # append the contents of file1 to file 2"

    is what I wanted but I want to do it recursivly through a directory and its sub directories.

    cat "Hello" >> *.*

  5. #5
    Linux Newbie Sangal-Arun's Avatar
    Join Date
    May 2006
    Location
    Gurgaon, India + Denver Colorado USA
    Posts
    101

    Thumbs up

    Code:
    [user@build]$ find temp1 -type f
    temp1/a/a
    temp1/b/b
    temp1/c/c
    temp1/d/d
    [user@build]$
    [user@build]$
    [user@build]$
    [user@build]$ find temp1 -type f
    temp1/a/a
    temp1/b/b
    temp1/c/c
    temp1/d/d
    [user@build]$ find temp1 -type f|xargs cat
    [user@build]$
    
    See carefully...no output for "cat" as no "data" was in the file.
    
    [user@build]$ for f in `find temp1 -type f`; do echo Hello >> $f; done
    [user@build]$
    
    Now see the content at the end of each 4 files...
    
    [user@build]$ find temp1 -type f|xargs cat
    Hello
    Hello
    Hello
    Hello
    [user@build]$
    [user@build]$ for f in `find temp1 -type f`; do echo "Hello SANGAL" >> $f; done
    [user@build]$
    
    Now we added more data...at the end of each file...so cat will show 2 lines now i.e. 1st one is Hello and 2nd one is Hello SANGAL
    
    [user@build]$ find temp1 -type f|xargs cat
    Hello
    Hello SANGAL
    Hello
    Hello SANGAL
    Hello
    Hello SANGAL
    Hello
    Hello SANGAL
    [user@build]$ echo "gareebi"
    gareebi
    Brgds,

    ARUN SANGAL
    SCM: 1- 720 251 9962
    Email: sangal.ak04@gmail.com
    Email: sangal_ak04@yahoo.com

Posting Permissions

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