Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have 12 files each containing 10 lines.

I need to copy the content of the first file to a new file (new.txt) Then I need to copy the lines 6 to the end (10) of the 11 remaining files in the same new.txt

Anybody knows any tips to do that?

Regards, Serge

share|improve this question
up vote 1 down vote accepted

Try this:

cat 1.txt > new.txt
for f in 2.txt 3.txt 4.txt 5.txt 6.txt 7.txt 8.txt 9.txt 10.txt 11.txt 12.txt; do tail -5 $f >> new.txt; done

This copies the contents of the first file to new.txt. Then the for extracts the last 5 lines (6 to 10) of your files (excluding the first file) and appends to new.txt If the file names following a certain pattern, you don't need to list all of them by name in the for, but use e.g. *.txt. But the pattern would also need to exclude the first file and new.txt.

share|improve this answer
Dear williamvoor, thanks for the suggestion but it gives: tail: cannot open `$f' for reading: No such file or directory. – Serge May 5 '12 at 10:18
@Serge Can you paste here the exact for line you used?. – Boj May 5 '12 at 10:53
It is working, thanks. The error occured when I tried to run the script in .bat file (I'm on Windows using cygwin) and other linux functionalities. When I type directly in Cygwin, it was fine. On the other hand, I also made another script for it using cat 1.asc > new.asc foreach n (2 3 4 5) sed -n '7,200p' $n.asc | cat >> new.asc end that I could run as .bat file. – Serge May 5 '12 at 12:45

Your Answer


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.