Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to copying the file using cp command and avoid "same file error"

4,031 views
Skip to first unread message

bubunia...@gmail.com

unread,
Apr 3, 2018, 1:52:51 PM4/3/18
to
Hi all,

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
and my script fails. How do I forcefully copy even if the file is same file using cp command?

I tried many option like -u, -f, -ra etc with cp but did not work.

Regards
Pradeep

hymie!

unread,
Apr 3, 2018, 2:46:01 PM4/3/18
to
In our last episode, the evil Dr. Lacto had captured our hero,
bubunia...@gmail.com <bubunia...@gmail.com>, who said:
> Hi all,
>
> cp /temp/test/test.tct /temp/test
>
> cp: ‘/temp/test.txt’ and ‘/temp/test/test.txt’ are the same file

Assuming GNU cp, you might try the -b and -f flags:

As a special case, cp makes a backup of SOURCE when the force and
backup options are given and SOURCE and DEST are the same name for an
existing, regular file.

But from what I can tell (I don't have the source handy, I'm going by
the man page and testing) the behavior you see is defined in the
source code and is not alterable by a command-line flag.

It might be undefined behavior -- much like running a command with
<file >file , you can't be sure if it will read the file first, or
clear the file for writing first -- and thus the program refuses to
do it.

--hymie! http://lactose.homelinux.net/~hymie hy...@lactose.homelinux.net

Jerry Peters

unread,
Apr 3, 2018, 4:16:14 PM4/3/18
to
bubunia...@gmail.com wrote:
> Hi all,
>
> 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
> and my script fails. How do I forcefully copy even if the file is same file using cp command?
>
Because you're trying to copy it over itself!

You have a file test.tct in the directory /temp/test/, you tell cp to
copy it from the directory /temp/test to the directory /temp/test, cp
is telling you that it's the same file. Either copy it to a different
file name in /temp/test or copy it to a another directory, not
/temp/test.

Helmut Waitzmann

unread,
Apr 3, 2018, 5:19:50 PM4/3/18
to
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

That is, 'cp' would copy a file over itself, if it did not refrain
From trying that.

> and my script fails. How do I forcefully copy even if the file
> is same file using cp command?

Thank the programmers of the 'cp' command for refraining from
letting 'cp' copy a file over itself!

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 and open the file for
writing, truncating any old contents. Then, "cp" would try to
read the first block of data, but, because the file has already
been truncated, all its contents would be lost and you would get
an empty file, which has got two pathnames: '/temp/test.txt' and
'/temp/test/test.txt'.

> I tried many option like -u, -f, -ra etc with cp but did not
work.

Try the option '--remove-destination', which removes
'/temp/test/test.txt' before opening it for writing.

Helmut Waitzmann

unread,
Apr 3, 2018, 5:27:25 PM4/3/18
to
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

That cannot happen. The pathnames in the error message differ
From the pathnames in your 'cp' command above.

What was your actual 'cp' command? What error message did you
get?

Kaz Kylheku

unread,
Apr 3, 2018, 7:05:57 PM4/3/18
to
On 2018-04-03, bubunia...@gmail.com <bubunia...@gmail.com> wrote:
> Hi all,
>
> 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

Are you sure it's not this error:

cp: ‘/temp/test/test.tct’ and ‘/temp/test/test.tct’ are the same file

> and my script fails.

Must be because "set -e" is in effect, or else the script tests for
failure and bails out.

A possibility might be to do something like this:

if [ "$srcpath" != "$targetdir/$(basename "$srcpath")" ] ; then
cp "$srcpath" "$targetdir"
fi

If possible, I'd have such a sanity test somewhere earlier. Like if
the scenario is that we are moving multiple materials between two
areas in the filesystem, I'd have one check that they are not the same
area, before doing the individual transfers.

> How do I forcefully copy even if the file is same file using cp command?

What does that mean? Are you looking for an option of "cp" to not do
anything in this case? Or do you want it to appear as if a copy were
made: the file's modification timestamp is updated and you are now the
owner?

If you want the file to be modified and owned by you, as if a copy were
made, you can copy from the source name to a temporary name, then
delete the destination file (if it exists), and then copy from the
temporary name to the destination.

Jorgen Grahn

unread,
Apr 4, 2018, 3:18:38 AM4/4/18
to
On Tue, 2018-04-03, Helmut Waitzmann wrote:
> 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
>
> That cannot happen. The pathnames in the error message differ
> From the pathnames in your 'cp' command above.

(And it's highly unusual to have a directory named /temp.)

> What was your actual 'cp' command? What error message did you
> get?

/Jorgen

--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .

Helmut Waitzmann

unread,
Apr 4, 2018, 5:11:01 AM4/4/18
to
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.
0 new messages