Leaderboard ad


Linux Copy (cp) File Command Help and Examples

To copy files in Linux and most other UNIX operating systems, you will use the cp command.  This command allows you to copy one or multiple files from one directory to another.  There are many options to the cp command and here we will explore those options and also give some examples.

* Basic Copy

To simply copy a file into the same directory:

cp file.txt filecopy.txt

To copy a file to another location, you simple enter the command cp, then the source file to copy from, and the target file (with path) to which you would like to copy it.

cp /var/tmp/file.txt /home/savona/file.txt

The above example simply copies files.txt from /var/tmp/ to /home/savona/

* Copy Multiple Files

To copy multiple files, you can simple add multiple source files like so:

cp /var/tmp/file.txt /var/tmp/file1.txt /var/tmp/file2.txt /home/savona/

NOTE: If the files are in the current working directory, you do not need to specify the full path.  For example:

cd /var/tmp/
cp file.txt file1.txt file2.txt /home/savona/

* Verbose output

Using verbose output if handy when copying a lot of files, or when scripting.  Using the -v option will give you feedback on exactly what is being done.

Here is an example of a successful copy using the verbose mode:

[savona@putor tmp]$ cp -v file.txt resume.txt
‘file.txt’ -> ‘resume.txt’

* Recursive Copy

Using the -r or recursive option will allow you to copy directories and subdirectories recursively.  For example, if we had a directory called "testcopy" that contained a subdirectory and files in each, you can copy them all recursively like so:

NOTE: I used the -v (verbose) option with the -r (recursive) option to show you the results.

[savona@putor tmp]$ cp -rv /var/tmp/testcopy /tmp/
‘/var/tmp/testcopy’ -> ‘/tmp/testcopy’
‘/var/tmp/testcopy/file.txt’ -> ‘/tmp/testcopy/file.txt’
‘/var/tmp/testcopy/file1.txt’ -> ‘/tmp/testcopy/file1.txt’
‘/var/tmp/testcopy/testsubdir’ -> ‘/tmp/testcopy/testsubdir’
‘/var/tmp/testcopy/testsubdir/anotherfile2.txt’ -> ‘/tmp/testcopy/testsubdir/anotherfile2.txt’
‘/var/tmp/testcopy/testsubdir/anotherfile1.txt’ -> ‘/tmp/testcopy/testsubdir/anotherfile1.txt’
‘/var/tmp/testcopy/testsubdir/anotherfile.txt’ -> ‘/tmp/testcopy/testsubdir/anotherfile.txt’
‘/var/tmp/testcopy/file2.txt’ -> ‘/tmp/testcopy/file2.txt’

As you can see, the cp command recursively copied all files and folders under the specified target directory.

* Preserve File Attributes

The output below shows the file (notice timestamp) to be copied.

[savona@putor tmp]$ ls -lrt
total 4
-rw-rw-r--. 1 savona savona 53 Jul 14 07:25 savonatest.txt

Now we will copy it without the -p (preserve) option, and check the timestamp (Notice timestamp change):

[savona@putor tmp]$ cp savonatest.txt testnopreserve.txt
[savona@putor tmp]$ ls -lrt
total 8
-rw-rw-r--. 1 savona savona 53 Jul 14 07:25 savonatest.txt
-rw-rw-r--. 1 savona savona 53 Jul 14 07:28 testnopreserve.txt

Now we will copy the file with the -p (preserve) option, notice the timestamp is preserved:

[savona@putor tmp]$ cp -p savonatest.txt testpreserve.txt
[savona@putor tmp]$ ls -lrt
total 12
-rw-rw-r--. 1 savona savona 53 Jul 14 07:25 testpreserve.txt
-rw-rw-r--. 1 savona savona 53 Jul 14 07:25 savonatest.txt
-rw-rw-r--. 1 savona savona 53 Jul 14 07:28 testnopreserve.txt

This was a simple example, but using the preserve option allows you to preserve User and Group permissions, File Mode, Flags, Access time, Modification time, Access Controls Lists (ACLs), and Extended Attributes.


* Wildcards

The star wildcard can respresent zero characters, any single character, or any string made up of multiple characters.

If you want to copy all files from your current directory to /var/tmp/ you can use this wildcard to respresent all files.

cp * /var/tmp/

You can also use it to copy all like files.  For example if you wanted to copy all txt files from your currect directory to /var/tmp/.

cp *.txt /var/tmp/

For more information on wildcards see THIS ARTICLE for a list of wildcards and examples of how to use them.

No comments:

Post a Comment