×

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

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.

ls Command in Linux/Unix with Examples

The ls command in Linux is a command-line utility that is used to list the files and directories in a directory. It is one of the most basic and commonly...

4 minutes read.

addr2line Command in Linux with Examples

Use of addr2line Command The addr2line command is used to convert addresses to line numbers and file names. This command uses debugging info to check which line number and file name...

4 minutes read.

addgroup Command in Linux with Examples

What is the addgroup command? It is a command in Linux to create a new group using specified command lined and the system’s default values. How to add groups Adding a user group:...

4 minutes read.

echo Command in Linux/Unix with Examples

The "echo" command in Linux is used to display a line of text on the terminal. It can also be used to print the value of a variable or to...

3 minutes read.

Linux Tutorial for Beginners

What is Linux? Linux is an open source Operating System like Windows, Android, Mac OS, and iOS. An operating system is a software that enables communication between the user and the...

12 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.

Linux pwd

The pwd command is used to print or display the name of the working or current directory. Some shells have their own version of the pwd with several features, in...

1 minute 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.

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.

Linux apt-get command

Apt-get is a command-line that helps to handle the packages in Linux. Its major task is to assist in retrieving data and packages from trusted sources for purposes of installation,...

6 minutes read.

ssh Command in Linux/Unix with Examples

The ssh (Secure Shell) command in Linux is used to remotely access and control another computer or device over a network. It provides a secure and encrypted connection between the...

6 minutes 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 rmdir command

The default use case of the rmdir command in the Linux system is to delete an empty directory. The below screenshot shows how to delete the empty directory. First, we...

1 minute read.

scp Command in Linux/Unix with Examples

The scp (secure copy) command in Linux is a command-line utility that is used to securely transfer files between two hosts over a network. It is similar to the cp...

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.

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.

Linux Commands List

Linux Commands List The Linux systems are filled with commands for several use cases. We have divided these commands into some of the most common implementations for a better understanding of...

2 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.

rm Command in Linux/Unix with Examples

The "rm" command in Linux (and other Unix-like operating systems) is used to remove files and directories. The "rm" command stands for "remove" and it is used to delete files...

4 minutes read.