how to use cp to copy a file to to a file named the same as one directory name in a directory?

267 Views Asked by At

I am a linux beginner, and today I have a question. I have searched it on the internet,but not solved.
suppose there is a file named 1 and a directory named 2, if i want to copy the file1 and make the new file named 2, how can i accomplish it user the command cp ?

mypc:testcp RVFU98$ ls -l
total 0
-rw-r--r--  1 RVFU98  staff   0 Aug 27 11:14 1
drwxr-xr-x  2 RVFU98  staff  68 Aug 27 11:13 2
mypc:testcp RVFU98$ cp 1 2
mypc:testcp RVFU98$ ls -l
total 0
-rw-r--r--  1 RVFU98  staff    0 Aug 27 11:14 1
drwxr-xr-x  3 RVFU98  staff  102 Aug 27 11:14 2
mypc:testcp RVFU98$ cd 2
mypc:2 RVFU98$ ls -l
total 0
-rw-r--r--  1 RVFU98  staff  0 Aug 27 11:14 1

i know it is because the new file's name is the same as an existing directory,but i don't know how to solve it ,can any one help me?

1

There are 1 best solutions below

1
On

You cannot copy a file over an existing directory. If you know 2 exists and is a directory, you could use this to remove it and replace it with 1:

rm -rf 2 && cp 1 2