×

cp Command in Linux/Unix with Examples

The cp (copy) command in Linux is a command-line utility that is used to copy files and directories. It is one of the most basic and commonly used commands in Linux and it allows you to create copies of files and directories in the same file system or to move them to another file system.

The basic syntax of the cp command is:

cp [options] [source] [destination]

The "source" is the file or directory that you want to copy, and the "destination" is the location where the file or directory will be copied to.

The most basic use of the cp command is to copy a single file from one location to another. For example, to copy a file called "fruits.txt" from the current directory to the directory "backup", you would use the following command:

cp fruits.txt backup/

This command will create a copy of the "fruits.txt" file in the "backup" directory.

The cp command can also be used to copy a directory and its contents from one location to another. To copy a directory called "fruits" from the current directory to the directory "backup", you would use the following command:

cp -r fruits backup/

This command will create a copy of the "fruits" directory and its contents in the "backup" directory. The -r option is used to copy the directory and its contents recursively.

The cp command also has several options that can be used to modify its behavior. Some of the most commonly used options include:

  • -R: recursive, copies the directory and its contents recursively.
  • -v: verbose, provides detailed information about the progress of the copy.
  • -n: no clobber, does not overwrite an existing file.
  • -p: preserves the modification and access times, and permissions of the files during the copy.

The cp command can also be used in combination with other commands to perform more complex tasks. For example, you can use the cp command in combination with the find command to copy all files of a certain type in a directory to a new location. For example, the following command will copy all files with the .txt extension in the current directory to the directory "backup":

find . -name "*.txt" -exec cp {} backup/ \;

It is important to note that the cp command can only be used by the owner of the file or by a user with superuser privileges. This is because the cp command modifies the ownership and permissions of a file, which are considered sensitive operations. Also, when copying files between different file systems, the cp command may not be able to preserve all the file attributes, such as permissions and timestamps, it is better to use the rsync command for this kind of task.

Another important feature of the cp command is the ability to overwrite or update the destination files with newer versions of the source files. This can be done by using the -u option, which stands for update. The -u option tells the cp command to copy the source file to the destination only if the source file is newer than the destination file or if the destination file does not exist.

cp -u fruits.txt backup/

The cp command also has the ability to copy a file and rename it at the same time. This can be done by specifying the new name of the file at the end of the destination path. For example, the following command will copy the file "fruits.txt" to the "backup" directory and rename it to "vegetables.txt":

cp fruits.txt backup/vegetables.txt

It is also important to note that the cp command can be used to copy files between different devices, such as a hard drive and a USB drive, but it can only copy files within the same file system. To copy files between different file systems, other commands such as tar or rsync should be used.

It's important to note that the cp command can also be used in conjunction with redirection operators to copy the output of a command to a file. For example, the following command will copy the output of the "ls" command to a file called "filelist.txt":

ls > filelist.txt

You can also use the >> operator to append the output of a command to an existing file.

ls >> filelist.txt

Another important feature of the cp command is the ability to copy files over a network. This can be done by using the -a option, which stands for archive. The -a option tells the cp command to preserve the file attributes, such as permissions, timestamps and ownership, during the copy.

cp -a /home/user/fruits.txt user@192.168.1.100:/home/user/backup/

It's also important to note that cp command can also be used to copy symbolic links instead of the files they point to. This can be done by using the -d option, which stands for directory. The -d option tells the cp command to copy the symbolic link instead of the file it points to.

cp -d fruits.txt backup/

In addition to the cp command, there are also other similar command-line utilities that can be used to copy files and directories in Linux such as cat, dd, and tee. Both of these utilities have different features and uses. The cat command is used to concatenate and display the contents of files, the dd command is used to copy and convert files, and tee command is used to split the output of a command to multiple files or to a file and the terminal.

In summary, the cp command in Linux is a powerful command-line utility that is used to copy files and directories. It allows you to create copies of files and directories in the same file system or move them to another file system. The cp command has several options that can be used to modify its behavior, such as copying files recursively, preserving file attributes, updating destination files, renaming files, copying files over a network, and copying symbolic links. It also can be used in conjunction with redirection operators to copy the output of a command to a file. It is important to note that the cp command can only be used by the owner of the file or by a user with superuser privileges. Also, when copying files between different file systems, the cp command may not be able to preserve all the file attributes, such as permissions and timestamps, it is better to use the rsync command for this kind of task. It's also important to be aware of other similar command-line utilities such as cat, dd, and tee, which can also be used for copying files and directories in Linux, but have different features and uses.


Related Topics

mkdir Command in Linux/Unix with Examples

The "mkdir" command in Linux (and other Unix-like operating systems) is used to create new directories. The "mkdir" command stands for "make directory" and it is used to create a...

3 minutes read.

pwd Command in Linux/Unix with Examples

The "pwd" command in Linux (and other Unix-like operating systems) stands for "print working directory." When executed, it simply displays the current directory (or folder) that the user is currently...

3 minutes read.

Difference Between Linux and Windows

Linux vs. Windows Here in this tutorial, we are going to discuss several features of Linux and Windows. These features or characteristics help us to come to a conclusion about which...

5 minutes read.

find Command in Linux/Unix with Examples

The find command in Linux is a command-line utility that is used to search for files and directories in a file system. It is a versatile command that can be...

5 minutes read.

cut Command in Linux/Unix with Examples

The "cut" command in Linux (and other Unix-like operating systems) is used to remove sections from each line of a file or stream of data. The cut command is often...

3 minutes read.

Linux Absolute and Relative Path

The concept of the absolute and relative path is very simple. The names “relative path” and “absolute path” give us little understanding of the concept we are learning here. The...

1 minute read.

adduser Command in Linux with Examples

What is the adduser command? The adduser command creates a new user, and this command may be used to add apre-existing user to a group that already exists. How to use the...

4 minutes read.

df Command in Linux/Unix with Examples

The 'df' command in Linux is used to display the amount of disk space available on the file system. The command reports the amount of disk space used and available...

3 minutes read.

chmod Command in Linux/Unix with Examples

The chmod command in Linux is a command-line utility that is used to change the permissions of files and directories. It stands for "change mode" and it is used to...

4 minutes read.

Linux Admin Tutorial

In publishing and graphic design, lorem ipsum is a placeholder text commonly used to demonstrate the visual form of a document without relying on meaningful content....

1 minute read.

netstat Command in Linux/Unix with Examples

The "netstat" command in Linux is a command-line utility that displays network connections, routing tables, and various network statistics. It is used to display the status of network connections, both...

3 minutes read.

Linux Commands with Examples

Linux Commands with Examples All the Linux commands are executed on the terminal provided by the Linux system. This terminal is a command-line interface to implement and display command outputs. Linux...

10 minutes read.

ping Command in Linux/Unix with Examples

The "ping" command in Linux (and other Unix-like operating systems) is used to test the connectivity between two networked devices. The "ping" command sends a small packet of data, called...

4 minutes read.

sudo Command in Linux/Unix with Examples

The sudo command in Linux is used to run a command or set of commands with the privileges of another user, typically the root user. This allows users to perform...

3 minutes read.

Top 20 Linux Commands

1) clear command clear is a standard operating system command which is used to clear the terminal screen. This command brings the command line on the top of the terminal. By pressing the...

10 minutes read.

touch Command in Linux/Unix with Examples

The "touch" command in Linux (and other Unix-like operating systems) is used to create new empty files or update the timestamps of existing files. The touch command is a simple...

4 minutes read.

grep Command in Linux/Unix with Examples

The grep command in Linux is a command-line utility that searches for patterns in text files. It stands for "global regular expression print" and it is used to search for...

5 minutes read.

Rename Folder in Linux

Rename Folder in Linux In the Linux system, there is not any single dedicated command for renaming the files and directories. Mostly, the mv command is used extensively for the purpose...

3 minutes read.

apt-get Command in Linux with Examples

The apt-get command is a command-line tool that is used to handle packages in Linux. This command handles the processes like retrieving the packages and information from authentic sources for...

5 minutes read.

Linux mkdir | Linux Create Directory

The Linux mkdir command is very much similar to Windows’s "new folder" feature. This command is used in Linux os to create new directories. The command mkdir stands for "make...

1 minute read.