A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/linux-unix/cp-command-linux-examples/ below:

How to Copy Files and Directories in Linux | cp Command

The cp (copy) command is your go-to tool in Linux for duplicating files and folders quickly. Whether you’re backing up data, organizing files, or sharing content, cp lets you copy items between locations while keeping the original intact. The cp command requires at least two filenames in its arguments.

Syntax of cp Command

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.

Why Use the cp Command? 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.

cp Src_file Dest_file

Example 1:

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:

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.

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.

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.

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.

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`

Options Available in cp Command in Linux

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

Option What It Does Example -i Prompts before overwriting files. cp -i file.txt backup/file.txt -v Shows progress (verbose mode). cp -v file.txt backup/file.txt -n Prevents overwriting existing files. cp -n file.txt backup/file.txt -p Preserves file permissions & timestamps. cp -p file.txt backup/file.txt -u Copies only if the source is newer (update). cp -u file.txt backup/file.txt -b Creates a backup of overwritten files. cp -b file.txt backup/file.txt 1. Copy a File in Linux Using `-i` Option

-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,

2. Copy a File in Linux Using `-f` Option

-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,

3. Copy a File in Linux Using `-r` or `-R` Option

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
4. Copy a File in Linux Using `-p` Option

-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
5. Copy a File in Linux Using `*` Option

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 `*` 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. 



RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4