“cp” command in Linux/Unix

The “cp (copy)” command is a command-line utility in Linux and Unix systems that allows users to copy files and directories. This is useful for creating backups of files, duplicating files, and moving files to different locations.

To use the “cp” command, you must specify the source file or directory and the destination file or directory. For example, to copy a file called “file.txt” from the current directory to the “Documents” directory, you would use the following command:

cp file.txt Documents

This command copies the file “file.txt” from the current directory to the “Documents” directory, creating a new copy of the file in the destination directory.

You can also use the “cp” command to copy multiple files at once. For example, to copy both “file1.txt” and “file2.txt” from the current directory to the Documents directory, you can use the following command:

cp file1.txt file2.txt Documents

To copy a directory and all of its contents, you can use the “-R (recursive)” option. For example, to copy the “Photos” directory and all of its contents to the “Backup” directory, you can use the following command:

cp -R Photos Backup

This command copies the “Photos” directory and all of its subdirectories and files to the “Backup” directory, creating a duplicate of the entire directory tree.

You can also use the “cp” command to overwrite existing files or directories. For example, to copy a file called “new.txt” over an existing file called “old.txt” in the “Documents” directory, you can use the following command:

Advertisements
cp new.txt Documents/old.txt

To force the “cp” command to overwrite an existing file or directory without prompting the user, you can use the “-f” (force) option. For example, to force the “cp” command to overwrite an existing file called “old.txt” in the “Documents” directory, you can use the following command:

cp -f new.txt Documents/old.txt

Overall, the “cp” command is a powerful tool for copying files and directories in Linux and Unix systems. With a variety of options and the ability to copy multiple files at once, the “cp” command can be an essential part of your workflow when working with files and directories on the command line.

Advertisements

LEAVE A REPLY

Please enter your comment!
Please enter your name here