cp
cp is used to copy files or directories.
$ cp source destination
$ cp file1.txt file2.txt
$ cp -b file1.txt file2.txt
$ cp -v file1.txt file2.txt
$ cp * ~/Desktop
Useful Options / Examples
cp
$ cp file1.txt file2.txt
Break it down
- This is the simplest use of
cp. It copies the contents offile1.txtand puts it intofile2.txt. Iffile2.txtdoes not exist, it creates it and puts the contents offile1.txtinto it. Iffile2.txtdoes exist, it overwrites it.
cp -b
$ cp -b file1.txt file2.txt
Break it down
- This flag makes a backup of the destination (if it exists) before overwriting it.
The backup file will be called
file2.txt~. If the destination does not exist, the flag doesn’t do anything.
cp -r
$ cp -r eecs280-w15 eecs280-w15-copy
Break it down
- The
-roption will copy directories recursively. That means that if you want to make a copy of a folder, including its sub-folders, you must include the-rflag. In the example above, the command will copy over all of the contents ofeecs280-w15into another folder calledeecs280-w15-copy.
cp -u
$ cp -u file1.txt file2.txt
Break it down
- This option ensures that the copy only occurs when the source file is newer than the
destination file or when the destination file is missing. If
file2.txtis updated afterfile1.txt, thefile1.txtwill not be copied over tofile2.txt. Iffile2.txtdoes not exist, it is created and will contain the contents offile1.txt.
cp -v
$ cp -v file1.txt file2.txt
'file1.txt' -> 'file2.txt'
Break it down
- This option explains what is being done during the copy process. It can be combined with
other flags (like
-u) so that it can be determined whether the copy occurred or not. If the copy did not occur, there will be no output.
cp file1.txt ~/Desktop/HW4
$ cp file1.txt ~/Desktop/HW4
Break it down
- This usage of cp is very useful. Instead of copying directly to another file, a directory
can be specified.
file1.txtwill be copied over to a newly created file in the specified directory.
cp * ~/Desktop/HW4
$ cp * ~/Desktop/HW4
Break it down
- This usage of cp is similar to the example above. This time, all the files of the current folder will be copied to the specified directory. It is particularly useful if you wish to copy all files in one directory into another.