1834230 Members
2658 Online
110066 Solutions
New Discussion

CP command constraints

 
SOLVED
Go to solution
Scott McDade
Frequent Advisor

CP command constraints

I am trying to copy the contents of several files into a single file using the cp command and it is not allowing me to as it seems to need the target file to be a directory. Is there another way to do this? Probably a silly question but I am writing a file parser and need all the context in a single file.

I am trying this: cp *.txt >target.file

Keep it Simple!~
5 REPLIES 5
Eileen Millen
Trusted Contributor

Re: CP command constraints

You can append files with the cat command.
cat file1 file2 >> file3

Eileen
Magdi KAMAL
Respected Contributor
Solution

Re: CP command constraints

Hi Scott,

cp command copies ( source ) file(s) or directory(ies) to ( destination ) file or dirctory respectively.

examples :
1. cp file1 file2 file3 destDir
2. cp -r dir1 dir2 dir3 destDir

If you want to copy the content of files then use cat command ( as mentioned before ).

cat sourceFile1 sourceFile2 sourceFile3 >> destinationFile

Magdi
Robin Wakefield
Honored Contributor

Re: CP command constraints

Hi,

If you'd only had two files, you'd have overwritten one with the other, since the command would expand to:

cp file1.txt file2.txt > file3

file2 would then be a copy of file1, file3 would contain nothing.

More than 2 and you'd just get the error you describe. Use cat, as suggested.

Rgds, Robin.
MANOJ SRIVASTAVA
Honored Contributor

Re: CP command constraints

Hi Scott

I tried this

cp *.txt /

like i went to /etc and gave

cp *.log /tmp and it copied all files with *.log under /tmp with the same name .

Also after you have copied and want to combine it into a single file then

cat *.txt >

Manoj Srivastava
A. Clay Stephenson
Acclaimed Contributor

Re: CP command constraints

Hi Scott:

You gotten the correct answers but why not do this the real unix way and have your parser simply read stdin (in this case a pipe) and then write to stdout.

e.g. cat file1 file2 file3 | myprogram > newfile.

This way 'myprogram' acts as a filter and is the typical way to do this in a unix environment.

Regards, Clay
If it ain't broke, I can fix that.