Copying Directories and Files with cp


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 prompt, type cp -r, followed by the old and new (to be created) directory names, to copy a complete directory. The r stands for "recursive," if that'll help you remember it.

2.

ls /home/shared/deb/Projects

You can use ls plus the new directory name to verify that the duplicate directory and its contents are in the intended location (Code Listing 2.3).

Code Listing 2.3. Use cp -r to copy directories.

$ cp -r /home/ejr/Projects  /home/shared/deb/Projects $ ls /home/shared/deb/Projects current   new.ideas schedule $ 

To copy a file:

1.

cp existingfile newfile

At the prompt, type cp, followed by the old and new (to be created) filename.

2.

ls -l

Optionally, check out the results with ls -l. The -l (for long format) flag displays the file sizes and dates so you can see that the copied file is exactly the same as the new one (Code Listing 2.4).

3.

cp -i existingfile oldfile

If you use cp with the -i flag, it prompts you before overwriting an existing file, also shown in Code Listing 2.4.

Code Listing 2.4. Just use cp to copy files and add -i to insist that the system prompt you before you overwrite an existing file.

$ cp existingfile newfile $ ls -l total 7 -rw-rw-r     1 ejr   users   1475 Jun 29  12:18 existingfile -rw-rw-r     1 ejr   users   1475 Jun 29  12:37 newfile -rw-rw-r     1 ejr   users   2876 Jun 29  12:17 oldfile $ cp -i existingfile oldfile cp: overwrite  'oldfile'? n $ 

Tips

  • When copying directories and files, you can use either absolute (complete) names, which are measured from the root directory (/home/ejr/Projects), or relative (partial) names, which specify files or directories in relationship to the current directory (ejr/Projects) and aren't necessarily valid from elsewhere in the Unix file system. Using absolute names, you can manipulate directories and files anywhere in the Unix system. Using relative names, you can manipulate files only with reference to your current location.

  • You can compare the contents of two files or two directories using cmp and dircmp, respectively. For example, typing cmp filename1 filename2 would compare the contents of the specified files. Use diff or sdiff to see the differences between files. See Chapter 6 for more information.

  • You can copy directories and files to or from someone else's directory. Skip to Chapter 5 to find out how to get access, then use the copying procedure described here.

  • Use cp with a -i flag to force the system to ask you before overwriting files. Then, if you like that, visit Chapter 8 to find out about using aliases with cp so that the system always prompts you before overwriting files.





Unix(c) Visual Quickstart Guide
UNIX, Third Edition
ISBN: 0321442458
EAN: 2147483647
Year: 2006
Pages: 251

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net