Copying a File with cp
Copying works similarly to moving, but retains the original in the original location. To copy a file named filename from ~/documents to ~/archive, use this command:
matthew@seymour:~$ cp documents/filename archive
Notice that the filename is not included in the destination. The destination here must be an existing directory. If it is not, the file is renamed to the term used. Some examples will make this clear.
To copy a file that is in my current directory I could use the following, and it will work exactly the same as mv, except that both files will exist afterward:
matthew@seymour:~$ cp oldfilename newfilename
To rename a file as I copy it from ~/documents to ~/archive, I could use this:
matthew@seymour:~$ cp documents/oldfilename ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access