Sunday 29 July 2018

Copy (cp) File And Directory Examples

LPI Certification, LPI Guides, LPI Tutorial and Material, LPI Learning

Copy (cp) is the frequently used command in Unix (or Linux). The cp Command is used to copy the files from one directory to another directory. The cp command can also be used to copy the directories also. The syntax of cp command is

cp [options] source destination

Examples of cp Command


1. Write a unix/linux cp command to copy file in to a directory?


The basic usage of cp command is to copy a file from the current directory to another directory.

cp sum.pl tmp/

The cp command copies the file sum.pl into the tmp directory. The cp command does not remove the source file. It just copies the file into a new location. If a file with the same name as the source exists in the destination location, then by default the cp command overwrites that new file

2. Write a unix/linux cp to prompt for user before overwriting a file ( Interactive cp command)?


The -i option to the cp command provides the ability to prompt for a user input whether to overwrite the destination file or not.

> cp sum.pl tmp/
cp: overwrite `tmp/sum.pl'?

If you enter y, then the cp command overwrites the destination file, otherwise the cp command does not copy the file.

3. Write a unix/linux cp command to copy multiple files in to a new directory?


You can specify multiple files as the source and can copy to the new location.

cp log.dat bad.dat tmp/

The cp command copies the log.dat, bad.dat files in the current directory to the tmp directory.

4. Write a unix/linux cp command to do a Regular expression copy?


You can copy a set of files by specifying a regular expression pattern.

cp *.dat tmp/

Here the cp command copies all the files which has "dat" as suffix to the destination directory.

5. Write a unix/linux cp command to copy a file in to the current directory?


You can copy a file from a different directory to the current directory.

cp /usr/local/bin/multiply.sh.

Here the cp command copies the multiply.sh file in the /usr/local/bin directory the current directory. The dot (.) indicates the current directory.

6. Write a unix/linux cp command to copy all the files in a directory?


The cp command can be used to copy all the files in directory to another directory.

cp docs/* tmp/

This command copies all the files in the docs directory to the tmp directory.

7. Write a unix/linux cp command to copy files from multiple directories?


You can copy the files from different directories into a new location.

cp docs/* scripts/* tmp/

The command copies the files from docs and script directories to the destination directory tmp.

8. Write a unix/linux cp command to Copy a directory.


You can recursively copy a complete directory and its sub directory to another location using the cp command

cp -r docs tmp/

This copies the complete directory docs into the new directory tmp

9. Write a unix/linux cp command to Forcibly copy a file with -f option?


You can force the cp command to copy an existing destination file even it cannot be opened.

cp -f force_file.txt /var/tmp/

Related Posts

0 comments:

Post a Comment