Terminal, add text to the end of all text files in a directory.

Discussion in 'Mac Basics and Help' started by Jesse Smith, Feb 21, 2011.

  1. Jesse Smith macrumors member

    Joined:
    Apr 19, 2003
    Location:
    Planet Zeekois
    #1
    Is there any way to use Terminal to add text to the end of all Files in a directory? TexFinder X is close, but it only searches and replaces existing text. If there's not the same text at the end of every file, it can't do it.
     
  2. dhracer88 macrumors regular

    Joined:
    Feb 21, 2005
    #2
    Sure, this should do it:

    Code:
    cd ~/path/to/files
    for f in *; do echo 'text to be appended' >> "$f"; done
    
    If you'd rather not type the path (in the cd part), you can type "cd " and then drag the folder onto the Terminal.

    Also, if you have a lot of text to be appending, you could try this:
    1. Create append.txt file with your content. Save this somewhere outside your directory of files.
    2. Change the "do echo" bit to
      Code:
      do cat ~/path/to/append.txt >> "$f"; done
     
  3. Jesse Smith thread starter macrumors member

    Joined:
    Apr 19, 2003
    Location:
    Planet Zeekois
    #3
    Thanks. That worked. I had been trying to do this...

    Code:
    for f in ~/html/*.html; do echo "Processing $f file.." && cat ~/close.html >> $f; done
    
    Put your closing tags in a file... well call it close.html and we'll jsut put it in your home directory /Users/youruser/close.html. Well assume your docs are in /Users/youruser/html
    
    Open Terminal.app and do the following command:
    
    find ~/html -type f -name "*.html" -exec cat ~/close.html >> {} \;
    but it just kept saying the directory didn't exist.
     

Share This Page