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.
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.
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
.
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
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
You cannot copy multiple multiple files to a file, only to a directory, ie
cp file1 file2 file2 file4
is not possible, you need
cp file1 file2 file2 dir1
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.