Score:1

How to copy from full path to full path under new name with overwrite?

cn flag

I want to copy a file from one directory to another under a different name, using the full path of both directories. The different name already exists in the destination directory so I want to overwrite at the same time. For example:

cp /home/rcw/subgroup/File_Name-#05.c  /opt/PWC/Bchs/File_Name.c

where File_Name-#05.c is copied from the left directory to the right directory under the name File_Name.c, and File_Name.c must be overwritten because it already exists in the right directory.

But I get:

cp: target '/opt/PWC/Bchs/File_Name.c’ is not a directory.  

This seems like it should be so simple, but a lot of research hasn’t turned up the right answer.

Thanks for any help.

hr flag
Are you actually using `bash` (as tagged) or are you perhaps using `zsh`, where `#` may be treated as a glob character in certain circumstances?
RTC222 avatar
cn flag
I am using /bin/bash.
Raffles avatar
in flag
Try using -f after `cp` to overwrite the existing file. Also consider -p to preserve permissions. In other words `cp -f -p etc`
RTC222 avatar
cn flag
With cp -f /home/rcw/subgroup/File_Name-#05.c /opt/PWC/Bchs/File_Name.c I still get the same error. It seems to be how I specified the new file name File_Name.c
Raffles avatar
in flag
Do `/opt` and the file you wish to overwrite have the correct permissions? Otherwise you might have to do sudo.
RTC222 avatar
cn flag
I do precede the string with sudo, but when I ask a question I omit it. But permissions are not the problem. If I use the same string where both files have the same name then it works.
Raffles avatar
in flag
Try putting a slash at the end `cp /home/rcw/subgroup/File_Name-#05.c /opt/PWC/Bchs/File_Name.c/`
RTC222 avatar
cn flag
No. same error.
hr flag
It's suspicious that the error message says `target 'File_Name.c' is not a directory` rather than `target '/opt/PWC/Bchs/File_Name.c' is not a directory` - do you have `cp` aliased to something like `cp -t File_Name.c`? What does `type -a cp` say?
Raffles avatar
in flag
There is also the switch -T, from the man page `-T, --no-target-directory [treat DEST as a normal file]`
RTC222 avatar
cn flag
@steeldriver -- you are right. I edited my question above.
RTC222 avatar
cn flag
@Raffles -- the -T switch did the trick. Thanks.
Raffles avatar
in flag
Okay I will put it in the answers below to help others.
Score:2
in flag

Use the switch -T

from the man page cp(1),

-T, --no-target-directory [treat DEST as a normal file]

mook765 avatar
cn flag
Yes, [here](https://www.gnu.org/software/coreutils/manual/html_node/Target-directory.html) some more details.
Raffles avatar
in flag
@mook765 Thanks for the link, it gives much more insight.
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.