The Linux copy command is
cp! Pretty simple right? There are many ways to use the Linux
cp command. There are also many options for the
cp command, but I usually only use 2 of them daily. The other options for Unix
cp command I hardly use because there are other commands or programs in Linux that do a much better job for accomplishing the same tasks. So for the other options for the Linux copy command, I will not cover them in this tutorial. If there is some specific
cp command usage or syntax you want to know, please let me know and I will add it to this Linux tutorial.
The 2 options for the Linux
cp command that I will cover is how to copy file recursively and also how to copy files in Linux only if the source file is newer than the destination file.
First the syntax for using the Linux
cp command is
cp [options] [source] [destination], where
[destination] may be a file or a directory.
The main option for the Linux
cp command that I use is coping a directory recursively. To do this you would use
cp -R [source] [destination],
cp -r [source] [destination], or
cp --recursive [source] [destination]. I usually am lazy and just use the shortest easiest keystroke,
cp -r [source] [destination].
Coping a directory recursively means copying the entire folder, including it’s contents. For example if I was wanting to copy the directory
/home/max/images/family/ and all of the directories and files inside of it to
/media/backup_drive/, I would run
cp -r /home/max/images/family /media/backup_drive/
Now if I was not wanting to copy the entire directory and just wanted only the *.jpg files, I would not need the
-r option. I would simply run
cp /home/max/images/family/*.jpg /media/backup_drive/family/jpeg_files/ which
* is a wildcard, so anything.jpg will be copied. Yes, that is a bad example as that is a very ugly directory structure, but again it is about 4:30am and I should really be sleeping!
There are much better ways to backup your data in Linux than using the
cp command, that is the only example I could come up with at the moment. In another Linux tutorial I will cover how to backup your files in Linux using a lot more efficient methods.
The next option that I use from time to time on the Unix
cp command is
cp -u [source] [destination] or
cp --update [source] [destination] which will only copy files if the source files are newer than the destination files, or if the destination files do not exist.
For an example of the Linux
cp -u command I am wanting to copy files from my website and back them up, but I only want to copy the files that are newer, to save CPU usage and drive writes, no use in overwriting files that are the same or older right? So I will run
cp -u /home/max/htdocs/* /media/backup_drive/htdocs/
If you are wanting to copy hidden files and folders in Linux using the
cp command, the first thing people will think of is
cp -r .* /media/backup_drive/max/hidden_config_files/ but this will actually match
../ as well, which will copy all files in the current working directory, and also copy all the files from the parent directory. So to copy only hidden files in Linux, you would want to run
cp -r .[a-z,A-Z,0-9]* /media/backup_drive/max/hidden_config_files/ this way it will only match files that start with a . and the next character is a-z, A-Z, or 0-9 and everything after that being a wildcard.
I hope this Linux tutorial on the Linux
cp command for beginners has helped you understand how to use the Unix
cp command while in the Linux Command Line Interface.