bubunia...@gmail.com:
> I am getting the file from a third party which i stored in a
> shared storage and cp the file forcefully.
>
> when i am coping the file:
>
> cp /temp/test/test.tct /temp/test
>
> The copy fails with the below error:
>
> cp: ‘/temp/test.txt’ and ‘/temp/test/test.txt’ are the same file
'cp' tells you, that it would have copied a file over itself, if
it had not refrained from doing that.
> and my script fails. How do I forcefully copy even if the file
> is same file using cp command?
Imagine, what would happen if "cp" would just copy a file over itself.
Copying a file over itself would mean:
'cp' would open the file for reading; then it would open the same
file for writing, truncating any old contents. Then, "cp" would
try to read the first block of data, but, because the destination
file (that is: the source file, as both of them are the same) has
already been truncated, all its contents would have been lost,
certainly not, what you intended to get.
Thank the programmers of the 'cp' command for refraining from
letting 'cp' copy a file over itself!
As long as the source and the destination pathnames of the 'cp'
command don't denote the same directory entry (I don't know, if
that's the case in your example), the option GNU cp option
'--remove-destination' should help:
cp --remove-destination -- /temp/test.txt /temp/test
That will give you two files with equal contents.
Regards.