cp command in Linux with examples

The cp command in Linux is a powerful tool that is used to copy files and directories from one location to another. It is an essential tool for managing files and directories on the Linux (and other UNIX based systems) command line, and its functionality extends to a wide range of use cases.

This guide on the cp (copy) command serves as another addition to my series of articles covering the previously published 90 Linux Commands frequently used by Linux Sysadmins. As time allows, I will continue to publish articles on the 90+ commands geared toward Linux sysadmins and Linux power users. Let’s continue this series with the cp command.

We will explore the cp command looking at its syntax, options, and examples of how it can be used.

cp command examples

 

Basic Syntax of the cp Command

The basic syntax of the cp command is as follows:

cp [options] source_file target_file

Here, cp is the command itself, [options] are any additional options that can be used with the command, source_file is the file that you want to copy, and target_file is the location where you want to copy the file to.

The source file can be a file or directory, and the target file can be a file or directory. If the target file is a directory, the source file will be copied into that directory with the same name. If the target file is a file, the source file will be copied and renamed as the target file.

 

Copying a File to Another Location

One of the most basic use cases of the cp command is to copy a file to another location. To do this, you can use the following command:

cp path/to/source_file.ext path/to/target_file.ext

In this command, path/to/source_file.ext is the location of the file that you want to copy, and path/to/target_file.ext is the location where you want to copy the file to.

For example, if you want to copy a file called my_file.txt from the directory /home/user/documents/ to the directory /home/user/pictures/, you would use the following command:

cp /home/user/documents/my_file.txt /home/user/pictures/

This command would copy the file my_file.txt from the documents directory to the pictures directory.

 

Copying a File into Another Directory, Keeping the Filename

Another common use case of the cp command is to copy a file into another directory while keeping the filename. To do this, you can use the following command:

cp path/to/source_file.ext path/to/target_parent_directory

In this command, path/to/source_file.ext is the location of the file that you want to copy, and path/to/target_parent_directory is the location of the directory where you want to copy the file to.

For example, if you want to copy the file my_file.txt from the directory /home/user/documents/ to the directory /home/user/pictures/ while keeping the filename, you would use the following command:

cp /home/user/documents/my_file.txt /home/user/pictures/

This command would copy the file my_file.txt from the documents directory to the pictures directory while keeping the filename.

 

Recursively Copying a Directory’s Contents to Another Location

The cp command can also be used to copy the contents of a directory to another location. To do this, you can use the following command:

cp -R path/to/source_directory path/to/target_directory

In this command, path/to/source_directory is the location of the directory that you want to copy, and path/to/target_directory is the location where you want to copy the directory to.

For example, if you want to copy the directory my_directory from the directory /home/user/documents/ to the directory /home/user/pictures/, you would use the following command:

cp -R /home/user/documents/my_directory

This command would copy the entire my_directory directory from the documents directory to the pictures directory. If the target_directory already exists, the source_directory will be copied inside it.

 

Copying a Directory Recursively, in Verbose Mode

The cp command can also be used to copy a directory recursively in verbose mode. This will show the files as they are being copied. To do this, you can use the following command:

cp -vR path/to/source_directory path/to/target_directory

In this command, -v is the verbose option, which tells the command to show the files as they are being copied. path/to/source_directory is the location of the directory that you want to copy, and path/to/target_directory is the location where you want to copy the directory to.

For example, if you want to copy the directory my_directory from the directory /home/user/documents/ to the directory /home/user/pictures/ in verbose mode, you would use the following command:

cp -vR /home/user/documents/my_directory /home/user/pictures/

This command would copy the entire my_directory directory from the documents directory to the pictures directory while showing the files as they are being copied.

 

Copying Text Files to Another Location in Interactive Mode

The cp command can also be used to copy text files to another location in interactive mode. This will prompt the user before overwriting any files. To do this, you can use the following command:

cp -i *.txt path/to/target_directory

In this command, -i is the interactive option, which tells the command to prompt the user before overwriting any files. *.txt is a wildcard that tells the command to copy all text files in the current directory, and path/to/target_directory is the location where you want to copy the files to.

For example, if you want to copy all text files in the directory /home/user/documents/ to the directory /home/user/pictures/ in interactive mode, you would use the following command:

cp -i /home/user/documents/*.txt /home/user/pictures/

This command would copy all text files in the documents directory to the pictures directory, and it would prompt the user before overwriting any files.

 

The cp command can also be used to follow symbolic links before copying. To do this, you can use the following command:

cp -L link path/to/target_directory

In this command, -L is the follow symbolic links option, which tells the command to follow any symbolic links before copying. link is the symbolic link that you want to follow, and path/to/target_directory is the location where you want to copy the file to.

For example, if you want to follow a symbolic link called my_link in the directory /home/user/documents/ and copy it to the directory /home/user/pictures/, you would use the following command:

cp -L /home/user/documents/my_link /home/user/pictures/

This command would follow the symbolic link my_link in the documents directory and copy it to the pictures directory.

 

Using the First Argument as the Destination Directory

The cp command can also be used to use the first argument as the destination directory. This is useful for piping the output of other commands to the cp command. To do this, you can use the following command:

bashCopy code

cp -t path/to/target_directory path/to/file_or_directory1 path/to/file_or_directory2 ...

In this command, -t is the target directory option, which tells the command to use the first argument as the target directory. path/to/target_directory is the location where you want to copy the files or directories to, and path/to/file_or_directory1path/to/file_or_directory2, and so on, are the files or directories that you want to copy.

For example, if you want to copy all text files in the directory /home/user/documents/ to the directory /home/user/pictures/ using the output of another command, you would use the following command:

ls /home/user/documents/*.txt | cp -t /home/user/pictures/

This command would list all text files in the documents directory and pipe the output to the cp command. The cp command would then copy the files to the pictures directory.

 

The infamous cp command: typos and oopsies!

In the early days of UNIX, the cp command could only copy files and directories one at a time. But as computers became more powerful, the command was updated to allow users to copy multiple files and directories at once.

But we must not forget the infamous typos that can happen when using the cp command. How many times have we accidentally overwritten an important file because we forgot to add the target directory or filename at the end of the command? It’s a rite of passage for anyone who has ever used the command line.

Who knows, maybe one day, the cp command will become the star of its own sitcom, where the main character spends days copying files to the wrong directories and accidentally deleting important documents. The possibilities are endless!

But fear not! The cp command has a failsafe option in the form of the “-i” flag, which prompts the user before overwriting any files. So, next time you’re copying important files, be sure to use the “-i” flag.

 

Related command and useful links

 

Conclusion

The cp command is a versatile and powerful tool that is essential for managing files and directories on the Linux command line. Used to copy files and directories from one location to another, and it offers a wide range of options for customizing its behavior.

In this article, we explored the basic syntax of the cp command and provided examples of how it can be used to copy files and directories to different locations, including copying files in interactive mode and following symbolic links before copying. I hope that this article has provided you with a solid foundation for using the cp command effectively in your Linux system!

Tags: , ,



Top ↑