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
The basic syntax for copying a file using the cp
command is as follows:
cp source_file destination
This command creates a copy of the `source_file`
at the specified `destination`
. If the destination is a directory, the file is copied into that directory.
cp
CommandThe `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.
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
Dest_file`
does not exist, it is created.Dest_file`
already exists, it is overwritten without any warning.Example 1:
a.txt`
) in the directory.cp`
command is used to copy the contents of `a.txt`
to `b.txt`
.a.txt`
and the newly created `b.txt`
coexist in the directory.cp a.txt b.txt
copy a file in Linux
We used `ls` command to display all the file in the current directory.
Example 2:
a.txt`
and `c.txt`
) in the directory.cp`
command is used to copy the contents of `a.txt`
to `c.txt`
.c.txt`
is overwritten with the content of `a.txt`.
cp a.txt c.txt
Copy a file in Linux
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.
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.
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.
cp -R Src_directory Dest_directory
copying files between two directories
The behavior depends on whether `Dest_directory`
exists or not. If it doesn’t exist, `cp`
creates it and copies the content of `Src_directory`
recursively. If `Dest_directory`
exists, the copy of `Src_directory`
becomes a sub-directory under `Dest_directory`
There are many options of cp command, here we will discuss some of the useful options:
Option | Detail |
---|---|
-i |
Interactive copying with a warning before overwriting the destination file. |
-b |
Creates a backup of the destination file in the same folder with a different name and format. |
-f |
Forces copying, even if the user lacks writing permission; deletes destination file if necessary. |
-r or -R |
Copies directory structure recursively. |
-p |
Preserves file characteristics (modification time, access time, ownership, permission-bits). |
`*` |
Uses the * wildcard to represent all files and directories matching a pattern. |
-i(interactive): i stands for Interactive copying. With this option the system first warns the user before overwriting the destination file. cp prompts for a response, if you press y then it overwrites the file and with any other option leaves it uncopied.
Basic Syntax:
cp -i [Source_file] [Destination_file]
Example:
cp -i a.txt b.txt
Copy a File in Linux Using `-i`
Here,
`ls`
command shows existing files: `a.txt`
and `b.txt`
.`cat a.txt`
displays the content of `a.txt`
.`cat b.txt`
displays the content of `b.txt`
.`cp -i a.txt b.txt`
initiates an interactive copy.b.txt`
.`cat b.txt`
shows the updated content, which now matches `a.txt`
.-f(force): If the system is unable to open destination file for writing operation because the user doesn’t have writing permission for this file then by using -f option with cp command, destination file is deleted first and then copying of content is done from source to destination file.
Basic Syntax:
cp -f [Source_file] [Destination_file]
Example:
cp -f a.txt b.txt
Copy a File in Linux Using `-f`
Here,
`ls`
command shows existing files: `a.txt`
and `b.txt`
.`cat a.txt`
displays the content of `a.txt`
.`cat b.txt`
displays the content of `b.txt`
.`cp -f a.txt b.txt`
initiates a forceful copy.`cat b.txt`
shows the updated content, which now matches `a.txt`
.Copying directory structure recursively. With this option cp command shows its recursive behavior by copying the entire directory structure recursively.
Basic Syntax:
cp -r [Directory_name1] [Directory_name2]
Example:
cp -r geeksforgeeks gfg
-p(preserve): With -p option cp preserves the following characteristics of each source file in the corresponding destination file: the time of the last data modification and the time of the last access, the ownership (only if it has permissions to do this), and the file permission-bits.
Note: For the preservation of characteristics, you must be the root user of the system, otherwise characteristics change.
Basic Syntax:
cp -p [Source_file] [Destination_file]
Example:
cp -p a.txt c.txt
Copying using * wildcard: The star wildcard represents anything i.e., all files and directories. Suppose we have many texts documents in a directory and want to copy it to another directory, it takes lots of time if we copy files 1 by 1 or command becomes too long if specify all these file names as the argument, but by using * wildcard it becomes simple.
Basic Syntax:
cp *.txt [Destination Directory or file]
Example:
cp *.txt Folder1
Copy a File in Linux Using `*`
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.
The
cp
command is used in Linux to copy files and directories from one location to another. Below are the steps and options commonly used with this command:Copying Files
To copy a single file from one location to another, you can use:
cp source_file_path destination_file_path
For example:
cp /home/user/file1.txt /home/user/backup/file1.txt
This command copies
file1.txt
from the user directory to a backup directory.Copying Directories
To copy a directory, including all its contents (files and subdirectories), you use the
-r
(recursive) option:cp -r source_directory_path destination_directory_path
For example:
cp -r /home/user/documents /home/user/backup/documents
This copies the entire
documents
directory and its contents into thebackup
directory.
The
cp
command is used to copy both files and directories, often with various options to tailor its functionality:
-r
: Recursive, for copying directories.-a
: Archive, similar to-r
and preserves the structure and attributes of files but is generally used for backing up.-v
: Verbose, shows whatcp
is doing (useful for tracking what gets copied).
cp -R
Command in Linux?The
cp -R
command is an alternative tocp -r
, and it functions the same way by copying directories recursively. Some systems differentiate slightly in how they handle symbolic links and special files between-R
and-r
, but for most users and typical usage, they behave the same.
In the terminal, the
cp
command is used to copy files and directories:
- Syntax:
cp [options] source destination
- Options: Modify behavior (like
-r
for recursive copy).
To copy a directory in Linux, use the
cp
command with the-r
option. Here’s how you might copy a directory from one place to another:cp -r /path/to/original_directory /path/to/copy_directory
This command ensures that the entire directory, including all nested files and subdirectories, is copied to the new location.