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.
cp Src_file Dest_file
- If `
Dest_file`
does not exist, it is created. - If `
Dest_file`
already exists, it is overwritten without any warning.
Example 1:
- Initially, there is only one file (`
a.txt`
) in the directory. - The `
cp`
command is used to copy the contents of `a.txt`
to `b.txt`
. - After the command execution, both `
a.txt`
and the newly created `b.txt`
coexist in the directory.
cp a.txt b.txt
We used `ls` command to display all the file in the current directory.
Example 2:
- Initially, there are two files (`
a.txt`
and `c.txt`
) in the directory. - The `
cp`
command is used to copy the contents of `a.txt`
to `c.txt`
. - After the command execution, the content of `
c.txt`
is overwritten with the content of `a.txt`.
cp a.txt c.txt
We used `ls` command to display all the file in the current directory and used `cat`command to display the content in the text file.
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