Copying Directories and Files with cp

When working in Unix, you’ll frequently want to make copies of directories and files. For example, you may want to copy a file you’re working on to keep an original, unscathed version handy. Or, you might want to maintain duplicate copies of important directories and files in case you inadvertently delete them or save something over them. Accidents do happen, according to Murphy.

Whatever your reason, you copy directories and files using the cp command, as shown in Code Listing 2.3. When you copy directories and files, all you’re doing is putting a duplicate in another location; you leave the original untouched.

To copy a directory:

1.
cp -r /home/ejr/Projects
→ /home/shared/deb/Projects
At the shell ...

Get Unix Third Edition: Visual Quickstart Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.