Robo
Robo

Reputation: 271

cp command failing in Linux

I am facing a copy command problem while executing shell script in RHEL 5.

executed command is

cp -fp /fir1/dir2/*/bin/file1 `find . -name file1 -print`

error is

cp: Target ./6e0476aec9667638c87da1b17b6ccf46/file1 must be a directory

Would you please throw some ideas why it would be failing?

Thanks Robert.

Upvotes: 1

Views: 5529

Answers (5)

Hendrik Brummermann
Hendrik Brummermann

Reputation: 8312

it is hard to answer without knowing what you are trying to achieve.

If, for example, you want to copy all files named "file1" within a directory structure to a target place /tmp, building the same directory structure there, this command will do the trick:

cd /dir1/dir2
find . -name file1 | cpio -pvd /tmp

Upvotes: 2

nico
nico

Reputation: 51680

As the others said you cannot copy multiple files to one file using cp. On the other hand, if you want to append the content of multiple files together into one destination file you can use cat.

For instance:

cat file1 file2 file3 > destinationfile

Upvotes: 2

David Z
David Z

Reputation: 131780

When cp is called with more than two filenames as arguments, it treats the last one as a target directory, and copies all the files named in the other arguments into that target directory. So, for example,

cp file1 file2 dir3

will create dir3/file1 and dir3/file2. It seems that in your case, the pattern /fir1/dir2/*/bin/file1 matches more than one filename, so cp is trying to treat the result of find as a target directory - which it isn't - and failing.

Upvotes: 5

Borealid
Borealid

Reputation: 98559

You can't copy many files to one location unless that location is a directory.

cp should be used thusly: cp sourcefile destinationfile or cp source1 source2 destinationdir.

Upvotes: 3

Drakosha
Drakosha

Reputation: 12165

You cannot copy multiple multiple files to a file, only to a directory, i.e.

cp file1 file2 file2 file4 

is not possible, you need

cp file1 file2 file2 dir1

Upvotes: 1

Related Questions