Copying few lines from one file to another

Hi all

I have a requirement

I need to copy only first 2 lines from a file of one location to another file of same location

I used to code as

cp head -2 abc 123

But i get the following error
cp: 0653-437 123 is not a directory.

The files used are

cat abc
ABCD
EFDG
TYUI

cat 123
1234
1ert
6789
7890

I want the output file as

ABCD
EFGD
6789
7890

Please help!!

use piping

head -2 abc >a |cp a 123

Refer to man cp for why the command is not working.
In short, If you provide two argument to "cp", it would copy the first file (arg1) to the second file (arg2) but If you provide more then two arg (your case), it expects the last arg to be a directory to copy all the files inside that directory. In this case, "123" is a file hence error.

You can probably use

for file in abc 123
do
 head -2 $file > new_file
done

I can use a for loop

Instead is there any way in a single command where the copy operation can be done ??

man head , look for -q option.

You are not copying the files, you are modifying them. cp won't work. Try

head -2q abc 123