In Linux we can copy files and directories around with the ‘cp’ command. Here we will demonstrate some common examples in which ‘cp’ can be used.
Note: The term ‘directory’ is simply Linux speak for ‘folder’ in Windows.
Copying Files And Directories
The ‘cp’ command works by specifying a source followed by the destination. For example, say we want to copy the /tmp/test file to /root, this would be done as follows.
[[email protected] ~]# echo "testing" > /tmp/test [[email protected] ~]# cat /tmp/test testing [[email protected] ~]# cp /tmp/test /root/ [[email protected] ~]# cat /root/test testing
This will copy the source file at /tmp/test to the directory /root/test. As shown when we check the contents of the newly copied /root/test file, they contain the “testing” string which we placed into the original /tmp/test file.
By default if no destination file is specified, the source file name is used, however we can specify the destination file name to be something different if we like, the contents of the file will be the same though.
[[email protected] ~]# echo "testing2" > /tmp/test2 [[email protected] ~]# cat /tmp/test2 testing2 [[email protected] ~]# cp /tmp/test2 /root/newfile [[email protected] ~]# cat /root/newfile testing2
We can also copy a whole directory, simply specify a directory as the source rather than a file name. The destination can either be the directory you wish to copy the source directory into, or it could be a newly named directory.
[[email protected] ~]# mkdir /tmp/testdir [[email protected] ~]# echo "testing3" > /tmp/testdir/testfile3 [[email protected] ~]# cp -R /tmp/testdir/ /root/ [[email protected] ~]# cat /root/testdir/testfile3 testing3
For this to work we need the -R flag, which we’ll cover next.
As the ‘cp’ command is for copying, it does not modify the source files or directories, so these will remain safely in place after the copy until you decide to remove them. If you want to move instead of copying, you can instead make use of the ‘mv’ command.
The ‘cp’ command has some useful additional options that we can use to perform specific actions.
This option is probably one of the most commonly used and useful available, as it allows us to recursively copy all files and directories within a directory. By default without copying a directory recursively, only the contents within the first level of the directory will be copied over, and not the contents of any sub directories. Using the -r flag will copy everything within recursively.
[[email protected] ~]# mkdir -p test1/test2/test3 [[email protected] ~]# touch test1/test1file [[email protected] ~]# touch test1/test2/test2file [[email protected] ~]# touch test1/test2/test3/test3file [[email protected] ~]# cp -r test1/ /tmp/ [[email protected] ~]# find /tmp/test1/ /tmp/test1/ /tmp/test1/test2 /tmp/test1/test2/test3 /tmp/test1/test2/test3/test3file /tmp/test1/test2/test2file /tmp/test1/test1file
In this example, we use mkdir to recursively create the path of directories test1/test2/test3, and we then create a single empty file within each directory with the ‘touch’ command. Next we run the copy with -r set and confirm that all of our files within the sub directories were correctly copied over, and indeed they were.
Force Overwrite Without Confirmation
If you attempt to copy one file over a file that already exists, you will be prompted to confirm that you want to do this by typing ‘y’ or ‘yes’ followed by pressing enter. If you are overwriting hundreds or thousands of files doing this manually would take a very long time. Luckily, there’s a simple way to automatically answer yes to all prompts as shown below.
yes | cp new-file existing-file
The above command will copy the file named ‘new-file’ and overwrite ‘existing-file’ without prompting you for a response as we are piping in yes.
With the -v option, we can enable verbose output which will display each file or directory that is copied over as it happens.
[[email protected] ~]# cp -v test1/test1file /opt/ test1/test1file -> /opt/test1file
So far we have covered how you can copy files and directory contents locally to different areas within the same Linux system, now we’ll demonstrate how you can use secure copy with the ‘scp’ command to copy to a remote system over SSH.
In order for this to work, you must have network connectivity from the source system to the destination system. By default SCP uses port 22, however we can set a specific port number with the -P option.
[[email protected] ~]# echo "testing scp" > /tmp/scp-test [[email protected] ~]# scp /tmp/scp-test [email protected]:/root [email protected]'s password: scp-test 100% 12 0.0KB/s 00:00 [[email protected] ~]# ssh [email protected] [email protected]'s password: Last login: Sun Jun 12 13:10:32 2016 from 192.168.1.13 [[email protected] ~]# cat /root/scp-test testing scp
The example above will copy the /tmp/scp-test file from the local system to the remote host’s /root directory, so long as the user specified has access to this location. We then SSH to the remote server and check the contents of the file have been copied over correctly.
Like ‘cp’, ‘scp’ also supports the -r option for copying recursively.
As shown we can easily copy files and directories within a Linux bash shell locally on the system with the ‘cp’ command. As a bonus we also covered how to remotely copy files and directories to a remote Linux system with the ‘scp’ command, which runs over the Secure Shell (SSH).