Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I'm trying to do quite a simple thing. I have 40 text files in which in need to delete everything and replace with the content of a template text-file.

What is the easiest/fastest way?

NB: I don't want to search and replace, but to delete everything and then copy inside. That would be like opening the file, ctrl + A >> del >> ctrl + v for each file

share|improve this question

2 Answers 2

up vote 3 down vote accepted

Unix-like

On a Unix-like command-line (on Linux, OSX, etc.) you can use cat to print the contents of a file to STDOUT, and > to redirect STDOUT to a file, over-writing its contents.

cat template.txt > file.txt

The easiest way is to put template.txt in a separate directory, then you can use a for loop to affect every *.txt in the working directory:

for f in ./*.txt; do cat /path/to/template.txt > "$f"; done

On systems with bash 4+ (any modern *nix is likely to have this; check with bash --version), you can use extended globbing, so you can have template.txt in the same directory as the files you wish to over-write:

shopt -s extglob
for f in ./!(*template).txt; do cat template.txt > "$f"; done

If you have text files in many subdirectories (WARNING: THIS WILL OVER-WRITE ALL *.txt FILES IN THE WORKING DIRECTORY AND ALL ITS SUB-DIRECTORIES, AND ALL THEIR SUB-DIRECTORIES...), you can use globstar:

shopt -s globstar
for f in ./**/*.txt; do cat /path/to/template.txt > "$f"; done

##  OR:
shopt -s globstar extglob
for f in ./**/!(*template).txt; do cat template.txt > "$f"; done

Windows

This works similarly, though the syntax is different. Instead of cat, use type:

type template.txt > file.txt

To work on every .txt in a directory:

FOR %F IN (*.txt) DO type C:\path\to\template.txt > %F

For recursiveness, use /R:

FOR /R %F IN (*.txt) DO type C:\path\to\template.txt > %F
share|improve this answer
    
Is there any reason to use cat or type instead of cp or copy? –  terdon Jun 6 '13 at 11:01
    
@terdon only that for some reason this solution is what came into my head first. –  evilsoup Jun 6 '13 at 11:04
    
It works (i'm on a windows platform, no linux at work) but it erase the content of the template, is there a way to prevent this? –  Chipsgoumerde Jun 10 '13 at 6:33

Well, what you want could be done by an automated search and replace(like what powergrep does but free), but it's easier to just take the template and copy it as each new file. This can be done from the command line.

Create a file with a list of all the filenames you want to put that template in.

So listoffiles.txt has contents:

file1.txt
file2.txt
file3.txt

Then open a cmd prompt and do:

C:\fdf>for /f %f in (listoffiles.txt) do @echo copy template.txt %f

copy template.txt file1.txt
copy template.txt file2.txt
copy template.txt file3.txt

C:\fdf>

Just change the @echo copy template.txt line, to read copy template.txt. i.e. remove the @echo or just the echo.

You need listoffiles.txt in c:\fdf and template.txt in there too. And it will create file1.txt file2.txt etc.

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.