Home Icon Home Computer Science How To Copy A File in Linux | Linux cp Command | Syntax & Options

How To Copy A File in Linux | Linux cp Command | Syntax & Options

For those new to Linux, grasping the fundamentals of the 'cp' command is essential. This article is a perfect guide to learn the basics, understand the syntax, and explore options for effective file copying in Linux.
Shivangi Vatsal
Schedule Icon 0 min read
How To Copy A File in Linux | Linux cp Command | Syntax & Options
Schedule Icon 0 min read

Table of content: 

  • Syntax of cp command
  • Features of the cp command
  • How to copy a file using the cp command
  • Options used in the cp command
  • Conclusion
expand icon

Copying a file or a directory is one of the most common and easy tasks done in the Linux terminal. We can copy the source files and source directories in Linux using the cp command. Cp is the abbreviation for the word copy. It creates a same image of a file with a different file name. In this article, we are going to learn how we can copy a file in Linux using the cp command and the various options present in the cp command.

Syntax of cp command

At least two filenames must be specified as arguments to the cp command. The basic syntax of the cp command is as follows: 

cp source_file destination_file

Here source_file is the file you want to copy, and destination_file is the name of the copy you want to create.

How To Copy A File in Linux | Linux cp Command | Syntax & Options

Features of the cp command

The cp command in Linux is a powerful tool that provides several features for copying different files and directories. Here we are going to discuss some of the important features of the cp command:

  • Copying files and directories: The cp command is mainly used to copy both target files and target directories.
  • Copying multiple files: The command can be used to copy multiple files at once by specifying the source files as arguments and the destination directory as the last argument.
  • Linking files: It can create hard links and symbolic links to files using the -l and -s options, respectively. This can be useful when you want to create a link to a single file rather than making a copy of it.
  • Preserving the file attributes: It can preserve the original file attributes like ownership, permissions, timestamps, and links when copying a file. This can be done by using the -p option which preserves the file mode.
  • Overwriting the files: This command can overwrite files if a file with the same name already exists in the destination directory. This behavior can be modified using the -i option to prompt the user before overwriting a file.
  • Recursive copy: It can recursively copy an entire directory and its contents in the current folder using the -r option. This is useful when you want to copy a current directory with subdirectories and files.
  • Verbose output: The cp command can provide verbose output using the -v option that displays information about the copied file in the current working directory, the copy operation in progress, and any errors that occur during the copy process.

Step-by-step process to copy a file using the cp command

Step 1: Open the terminal window in your Linux operating system.

Step 2: Use the cp command to go to the directory containing the file you wish to copy.

Step 3: Once you are in the current folder where the file is located, use the cp command to copy the file. For example, if you want to copy a file named file.txt to a file called file_copy.txt in the same directory, use the following command:

cp file.txt file_copy.txt

Step 4: If you want to copy the file to a different directory, you need to mention the full path to the destination directory as the second argument. For example, to copy file.txt to the /home/user/newdocuments/ directory, use the following command:

cp file.txt /home/user/newdocuments/

How To Copy A File in Linux | Linux cp Command | Syntax & Options

Options used in the cp command

There are additional options present in the cp command which are very important and widely used by the programmers. Some of the most commonly used options are listed below: 

1. -i option

This option prompts the user before overwriting a file that already exists in the destination directory. Using the -i option with the cp command can help prevent accidental overwriting of files and ensure that important data is not lost. For example: 

cp -i file.txt /home/user/newdocuments/

2. -f option

This option forces the cp command to overwrite the destination file without prompting the user. For example: 

cp -f file.txt /home/user/newdocuments/

3. -p option

This option preserves the original file attributes like ownership, permissions, timestamps, and links when copying a file. It forces the system to preserve modification time, access time, user id, file flags, file mode, access control lists, and extended attributes. For example: 

cp -p file.txt /home/user/newdocuments/

4. -r option

This option recursively copies a directory and its contents, including subdirectories and files. For example: 

cp -r /home/user/documents/ /home/user/backup/

5. -u option

This option copies the source file to the destination directory only if the source file is newer than the destination file or if the destination file does not exist. For example:

cp -u file.txt /home/user/backup/

6. -v option

This option provides a verbose output that displays information about the files being copied, the progress of the copy operation, and any errors that occur during the copy process. For example

cp -v file.txt /home/user/newdocuments/

Conclusion

The cp command is a versatile and powerful tool that provides a wide range of features for copying files and directories in Linux. With these simple commands and options mentioned in the article, we hope now you have a good understanding of how to copy a file in Linux and manage your files and directories efficiently.

Suggested reads:

Edited by
Shivangi Vatsal
Sr. Associate Content Strategist @Unstop

I am a storyteller by nature. At Unstop, I tell stories ripe with promise and inspiration, and in life, I voice out the stories of our four-legged furry friends. Providing a prospect of a good life filled with equal opportunities to students and our pawsome buddies helps me sleep better at night. And for those rainy evenings, I turn to my colors.

Comments

Add comment
No comments Image No comments added Add comment
Powered By Unstop Logo
Best Viewed in Chrome, Opera, Mozilla, EDGE & Safari. Copyright © 2024 FLIVE Consulting Pvt Ltd - All rights reserved.