Copy (cp) File And Directory Examples

  • Updated
  • Posted in Programming
  • 3 mins read


Copy (cp) is the continuously used command in Unix (or Linux). The cp Command is used to repeat the information from one listing to a different listing. The cp command can be used to repeat the directories additionally. The syntax of cp command is

cp [options] supply vacation spot

Examples of cp Command

1. Write a unix/linux cp command to repeat file in to a listing?

The primary utilization of cp command is to repeat a file from the present listing to a different listing.

cp sum.pl tmp/

The cp command copies the file sum.pl into the tmp listing. The cp command doesn’t take away the supply file. It simply copies the file into a brand new location. If a file with the identical identify because the supply exists within the vacation spot location, then by default the cp command overwrites that new file

2. Write a unix/linux cp to immediate for person earlier than overwriting a file ( Interactive cp command)?

The -i choice to the cp command gives the power to immediate for a person enter whether or not to overwrite the vacation spot file or not.

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

If you enter y, then the cp command overwrites the vacation spot file, in any other case the cp command doesn’t copy the file.

3. Write a unix/linux cp command to repeat a number of information in to a brand new listing?

You can specify a number of information because the supply and might copy to the brand new location.

cp log.dat unhealthy.dat tmp/

The cp command copies the log.dat, unhealthy.dat information within the present listing to the tmp listing.

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

You can copy a set of information by specifying an everyday expression sample.

cp *.dat tmp/

Here the cp command copies all of the information which has “dat” as suffix to the vacation spot listing.

5. Write a unix/linux cp command to repeat a file in to the present listing?

You can copy a file from a special listing to the present listing.

cp /usr/native/bin/multiply.sh .

Here the cp command copies the multiply.sh file within the /usr/native/bin listing the present listing. The dot (.) signifies the present listing.

6. Write a unix/linux cp command to repeat all of the information in a listing?

The cp command can be utilized to repeat all of the information in listing to a different listing.

cp docs/* tmp/

This command copies all of the information within the docs listing to the tmp listing.

7. Write a unix/linux cp command to repeat information from a number of directories?

You can copy the information from completely different directories into a brand new location.

cp docs/* scripts/* tmp/

The command copies the information from docs and script directories to the vacation spot listing tmp.

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

You can recursively copy an entire listing and its sub listing to a different location utilizing the cp command

cp -r docs tmp/

This copies the whole listing docs into the brand new listing tmp

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

You can power the cp command to repeat an present vacation spot file even it can’t be opened.

cp -f force_file.txt /var/tmp/

Leave a Reply