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.

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

Aching back from coding all day?

Prism Glasses

Aching back from coding all day? Try Back Problems

Ever feel achy from sitting crunched up on your computer table? Try lying down with these optical glasses that allow you to work on your laptop while lying flat on your back. This is the perfect solution with those with limited mobility or those who wish to prevent neck cramps and back strains.

$ Check price
4.454.45Amazon 4 logo(128+ reviews)

More Back Problems resources

Take your Linux skills to the next level!

System Admin Handbook

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

This book approaches system administration in a practical way and is an invaluable reference for both new administrators and experienced professionals. It details best practices for every facet of system administration, including storage management, network design and administration, email, web hosting, scripting, and much more.

$ Check price
74.9974.99Amazon 4.5 logo(142+ reviews)

More Linux & UNIX resources

Ad