Copy files to a Directory in Linux

When the cp command has one or more source file arguments and is followed by a destination directory argument, it copies each source file to the destination directory with the same name. If the destination directory does not exist, it is created. If it already exists, the files are overwritten without warning.

cp Src_file1 Src_file2 Src_file3 Dest_directory

Example:

Suppose we have to copy three files name “a.txt“, “b.txt” and “c.txt” to a directory name “new

cp a.txt b.txt c.txt new/

Copy multiple files to another directory

We used `ls` command to display all the file in the “new” directory to confirm the successful copy of file in that directory.

How to Copy Files and Directories in Linux | cp Command

In the world of Linux computers, a common and important job is copying files. The key tool for this task is the “cp” command. In this simple guide, we’ll explore how to copy a file in Linux, looking at the basic steps, different choices you can make, and giving clear examples. We’ll also take a peek behind the scenes to understand how the “cp” command works. Let’s get started on this easy journey to become familiar with the ins and outs of copying files in Linux!

Table of Content

  • Syntax of cp Command
  • How to Copy files in Linux with the cp Command
  • 1. Copying Between Two Files in Linux
  • 2. Copy files to a Directory in Linux
  • 3. How to Copy Directories in Linux
  • Options Available in `cp` Command in Linux

Similar Reads

Syntax of cp Command

The basic syntax for copying a file using the cp command is as follows:...

How to Copy files in Linux with the cp Command

The `cp` command is a versatile tool used in Unix-like operating systems for copying files and directories. It offers three principal modes of operation, each serving different purposes....

1. Copying Between Two Files in Linux

If the `cp` command contains two file names, it copies the contents of the first file to the second file. If the second file doesn’t exist, it is created, and the content is copied into it. However, if the second file already exists, it is overwritten without warning....

2. Copy files to a Directory in Linux

When the cp command has one or more source file arguments and is followed by a destination directory argument, it copies each source file to the destination directory with the same name. If the destination directory does not exist, it is created. If it already exists, the files are overwritten without warning....

3. How to Copy Directories in Linux

In this mode, if the cp command contains two directory names, it copies all files from the source directory to the destination directory. The `-R` option is typically used to indicate recursive copying for directories....

Options Available in cp Command in Linux

There are many options of cp command, here we will discuss some of the useful options:...

Conclusion

The `cp` command is an essential tool which is used for copying files or groups of files and directories in Unix-Like operating systems. If we talk about its syntax it takes at least two filenames in as an argument (source and destination). As mentioned, the command has three principles: copying two file names, copying one or more arguments, and copying two directory names. Then we also mention the multiple options available while using `cp` command: `-i` , `-b` , `-f“ , `-r` , `-p`. To work with easy in Unix shell for file management one should know the proper working of `cp` command....