1

How can I add the contents of a text file to the start of another text file?

There are 3 files named f1 f2 f3. All of three have some text content.

How can I add the contents of file f1 at the start of file f3 and the contents of file f2 to the end of file f3?

9

Use the cat command. Using your example, cat f1 f3 f2 will con*cat*enate the files together so that it will read like f1, f3, then f2. It outputs to stdout, so if you want f3 to read like that, you would redirect to a temporary file and then move that file to f3: cat f1 f3 f2 > tmp ; mv tmp f3

2
  • thanks chan-ho suh, but i want to make the changes permanently – pankaj sharma Mar 24 '12 at 9:51
  • 1
    Hello. Sorry about that. You need to redirect the output of the cat command to a new file and then simply move that file to f3. Then the change is permanent. Take a look at my edited answer. – Chan-Ho Suh Mar 24 '12 at 10:03
1

Make a temp dir.. touch temp

cat f1 >> temp .. temp now has the content of f1
cat f3 >> temp .. temp now has content of f1 and then f3
cat f2 >> temp .. temp now has the content in following order.. f1 f3 f2
mv temp f3 .. now file f3 contains f1 ..f3..f2
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.