cp command

ForestNymph

New Member
Joined
Sep 25, 2018
Messages
2
Reaction score
3
Credits
0
Hello all :). I'm new here. I'm currently learning linux and got confused with the cp command. Doesn't this command have the ability to rename a file?

Example: cp /home/student/Desktop/fencing /home/student/Desktop/ballet

I thought that the file "fencing" should be renamed to "ballet" when using the copy command. If the file "fencing" exist and I'm telling the terminal to copy "ballet" (that doesn't exit) over it, that should work right?
 


Hi @ForestNymph and welcome to linux.org :)

Doesn't this command have the ability to rename a file?

Yes, but only if the file is being copied "outside of", the current folder or environment. If not, then an additional file is created (same contents) with the new name.

This process is part of what is called "atomic".

So, in your example:

You will have generated a new file "ballet", also on your desktop, and with the same content as "fencing".

If you had a folder on your Desktop called, let's say "testcp", and your command was

Code:
cp /home/student/Desktop/fencing /home/student/Desktop/testcp/ballet

... then fencing would be copied to ballet.

Code:
mv

... on the other hand, is not so atomic, and so if you replaced cp for mv in your command, be careful that the destination file does not already exist. mv will move the file, overwrite the existing file, and delete the original file.

Chris Turner
wizardfromoz
 
Yes, but only if the file is being copied "outside of", the current folder or environment. If not, then an additional file is created (same contents) with the new name.
I dunno... this seems to work for me in the current folder (Desktop). Maybe I misunderstood. :confused::eek::D

First create fencing and add some text.
cp fencing ballet both creates the file ballet and copies the contents of the file fencing into the new file, ballet.

cp does not delete anything in this case. So both files still exist on my Desktop.

Now I modify fencing so that the contents are different. And...
mv fencing ballet will effectively delete the file named fencing, and the file ballet shows the new contents that were previously made in fencing (basically just renaming fencing to ballet). The old file named ballet, with different contents, is now lost.

Cheers
 

Members online


Top