07. Copying, Moving and Renaming Files and Directories cp

Copying files

To copy a file into another directory, use the cp command with the file name as the first argument, and the target path name as the second.

$ cp fileName destination

You may rename the file by attaching it to the end of the path.

$ cp fileName destination/newFileName

This takes a file, and sends a copy of it to the specified path, with the new file name.

And to copy into the same directory with a new name:

$ cp fileName newFileName

Copying Directories

To copy directories and the contents within, we must apply the -r option, which is the command to copy recursively.

The following command would copy all contents of dir1 into dir2.

$ cp -r dir1 dir2     

Copying multiple files in one go!

We can copy multiple items at once into a directory. As long as the last argument is the path name, we're good.

$ cp file1 file2 file3 pathName

Additional options

Here are some additional options for fine-tuning.

Force overwriting.
Interactive mode. The program will ask before overwriting any files.
Preserves ownership and permissions.
Perform archive. Preserves ownership and copies links as is.
Update. Only copy the file if original is newer than target (or if target does not exist).

Take your Linux skills to the next level!

Command Line Kung Fu

Take your Linux skills to the next level! Try Linux & UNIX

Command Line Kung Fu is packed with dozens of tips and practical real-world examples. You won't find theoretical examples in this book. The examples demonstrate how to solve actual problems. The tactics are easy to find, too. Each chapter covers a specific topic and groups related tips and examples together.

$ Check price
14.9914.99Amazon 4.5 logo(27+ reviews)

More Linux & UNIX resources

Aching back from coding all day?

Self-Massage Tool

Aching back from coding all day? Try Back Problems

Relieve spasms, tight muscles, trigger points and pressure points with the Body Back Buddy! This trigger point massage is designed to help you self-message any area of your body - especially those that are hard to reach. Keeping your muscles relaxes and out of contraction is importan in helping to reduce pain and prevent muscle injury.

$ Check price
29.9529.95Amazon 4.5 logo(3,443+ reviews)

More Back Problems resources