How to copy file in linux
How to copy file in linux
CP Command in Linux with Examples [Copy File in Linux]
Increasing your productivity by going with the shortcuts is always beneficial, For that cp command in Linux is also helpful to do that. This guide will help you to understand copy files in Linux concepts.
List of content you will read in this article:
Linux offers flexible and simple commands for performing various operations on the files and folders. One of the commonly performed tasks is to copy files in Linux by running specific commands along with various options for additional functionality.
In this article, we will be learning various cp command in Linux for copying files along with various options. Apart from this, you will get to know how to create system-wide backups for copying specific files.
Copy file in Linux using the CP Command
You can use the cp command in Linux to copy files and directories from one location to another. It works showhome similar to the mv command that uses to move the file or directory from one location to another. Well, every Linux distribution uses the cp command. Below is the basic format of the cp command that you can use.
cp [additional_option] source_file target_file
cp my_file_name.txt my_file2_name.txt
The above Linux command will help in creating a copy of the my_file_name and then renaming the new file to the my_file_name.
By default, the copy command in Linux will get executed in the same directory you are in. however, two files with the same name cannot exist in the same directory. If you want to copy a file that already exists in the target location, you need to change the name of the file to differentiate the new file.
Copy Command Options
Below are some additional copy command options that you can use with the cp command to perform various operations.
How to copy file in Linux to a different directory
If you want to copy a specific file from the current working directory to a different location other than the working directory, you can use the following copy command to do so.
cp my_file_name.txt /new_directory
You can even copy the file from a directory other than the current working directory by specifying the path of the file in the source and specifying the path to the target as shown below.
cp /etc/my_file_name.txt /new_directory
The above command will allow you to copy the file without changing the directory. If the new_directory does not exist then the copy file command will create this directory.
If we want to rename and copy the file to a different path, you can run the following command as shown below.
cp my_file_name.txt /new_directory/my_file_name2.txt
This option is also useful if you want to create backups of configuration files, or for copying data to a storage device.
How to Copy Multiple Files from One Directory to Another in Linux
Using the cp file command, you will also be able to copy multiple files at the same time. You can mention multiple files separated by spaces followed by the path where you want to copy those files.
cp my_file_name.txt my_file2_name.txt my_file3_name.txt /new_directory
The above command will not move the files but create a copy of these files to the destination path. You can even use the wildcard character for copying the files as shown below.
cp /pictures/*.jpg /new_directory
If you want to copy the entire folder along with its sub-directory then you can use the “-R” option along with the cp command. Where, “-R” stands for the recursive operation.
cp –R /documents /new_directory
rsync command to copy files
You can use the rsync command for synchronizing or transferring the data between two different locations. It is somehow similar to the cp Linux command but shows some differences. If you want to copy a single file, then you can run the following command.
rsync –a my_file.txt /new_directory/my_file_backup.txt
Conclusion
Copy command in Linux is another commonly used command that will help in creating a copy of the existing file to another specific location. In the copy command, the files will remain in the source folder along with moving to the destination folder.
In this article, you will be able to understand the concept of the CP Linux command and how it works differently with the various options.
The Linux cp Command – How to Copy Files in Linux
There are a couple different ways to copy and paste content when you’re working on your computer.
If you spend more time in the user interface of your device, you’ll probably use your mouse to do this. You can copy files by right-clicking on the file and selecting «Copy», then going to a different directory and selecting «Paste».
For my terminal friends, you can also perform file copy-paste operations without leaving the terminal. In a Linux-based terminal, you do this using the cp command.
In this article, I’ll explain what the cp command is and show you how to copy and paste files and directories in Linux using the terminal.
What is the cp command?
You use the cp command for copying files from one location to another. This command can also copy directories (folders).
The syntax of this command is:
[file/directory-sources] specifies the sources of the files or directories you want to copy. And the [destination] argument specifies the location you want to copy the file to.
To understand the rest of this article, I will use this folder structure example. Let’s say a directory called DirectoryA has two directories in it: DirectoryA_1 and DirectoryA_2. These subdirectories have many files and sub directories in them.
I’ll also assume you’re currently in the DirectoryA location in the terminal, so if you aren’t, make sure you are:
How to copy files with the cp command
If you want to copy a file, say README.txt from DirectoryA_1 to DirectoryA_2, you will use the cp command like this:
If you want to copy more than a file from DirectoryA_1 to DirectoryA_2, you will use the cp command like this:
As you can see, you will put all the source files first, and the last argument will be the destination.
How to copy directories with the cp command
By default, the cp command works with files. So if you attempt to copy a directory like this:
You will get an error stating:
./DirectoryA_1/Folder/ is a directory
This command will recursively copy the Folder directory in ./DirectoryA_1/ as well as all files and directories in the Folder directory.
How to copy files that match a glob pattern
A glob pattern is similar to Regex, which allows you to match multiple files with names that match a specific pattern. Learn more about the difference here: Regex vs Glob patterns.
For example, if you want to copy all files in DirectoryA_1 with the .txt extension, you can execute this command:
You can check out the glob documentation to learn more about globbing patterns and characters you can use.
Now you know how to copy files (and directories) right from the command line. Thanks for reading!
Developer Advocate and Content Creator passionate about sharing my knowledge on Tech. I teach JavaScript / ReactJS / NodeJS / React Frameworks / TypeScript / et al
If you read this far, tweet to the author to show them you care. Tweet a thanks
Learn to code for free. freeCodeCamp’s open source curriculum has helped more than 40,000 people get jobs as developers. Get started
freeCodeCamp is a donor-supported tax-exempt 501(c)(3) nonprofit organization (United States Federal Tax Identification Number: 82-0779546)
Donations to freeCodeCamp go toward our education initiatives, and help pay for servers, services, and staff.
How to Copy Files and Directories in Linux
Home » SysAdmin » How to Copy Files and Directories in Linux
This guide will show you how to copy files and directories in Linux by executing commands from the command line. Furthermore, the commands listed below detail how to create system-wide backups or filter out and copy only specific files.
Note: These Linux commands can only be run from a terminal window. If your version of Linux boots to a desktop graphical interface, launch a terminal window by pressing CTRL-ALT-F2 or CTRL-ALT-T.
Using the cp Command to Copy Files and Directories in Linux
This Linux command creates a copy of the my_file.txt file and renames the new file to my_file2.txt.
By default, the cp command runs in the same directory you are working in. However, the same file cannot exist twice in the same directory. You’ll need to change the name of the target file to copy in the same location. Some users will add _old, some will add a number, and some will even change the three-letter extension (e.g., .bak instead of .txt).
You may not get a warning before Linux overwrites your file – be careful, or see below for the –i option.
Additional Options
Additional options can be used in combination with the cp command:
Note: The -p (preserve) option forces the system to preserve the following source file attributes: modification time, access time, user ID (UID), group ID (GID), file flags, file mode, access control lists (ACLs), and extended attributes (EAs).
How to Copy File to Another Directory in Linux
To copy a file from the directory you’re working in to a different location, use the command:
You don’t need to rename the file unless there’s already one with the same name in the target directory.
To specify a path for the source file:
This lets you copy without having to change directories. The cp command will create the /new_directory if it doesn’t exist.
To rename and copy a file to a different path:
This option is useful for creating backups of configuration files, or for copying data to a storage device.
Note: Learn how to move directories in Linux.
Copy Multiple Files from One Directory to Another in Linux
You may need to copy more than one file at a time.
List each file to be copied before the target directory:
This example created a copy of all three files in the /new_directory folder.
Use a wildcard to specify all files that share a string of characters:
This would find all the files with the .jpg extension in the /pictures directory, and copy them into the /new_directory folder.
To copy an entire folder and its subfolders and files, use the –R option:
–R stands for recursive, which means “everything in that location.” This would copy all the files, as well as all the directories, to the /new_directory folder.
Copy Using rsync Command
To copy a single file, enter the following into a terminal:
To copy a directory with rsync, enter the following:
This copies the contents of the /etc/docker/ directory to /home/backup/docker/. Make sure to keep the slashes. Omitting the slash on the source directory will copy the contents into a subdirectory.
To omit files from being copied, check out our guide on how to exclude files and directories in data transfer using rsync command.
Other Options
The ls command is a handy partner to the cp command in Linux.
To list the contents of a directory enter the command:
The example above displays all the files in /directory. Use this command after copying to verify the files were copied successfully.
To change directories, use cd and the name of the directory. For example:
The command prompt will change to display that you’ve changed directories.
Now you understand how to copy files in Linux. The cp command is a versatile and powerful tool for managing and backing up files.
Copy Command in Linux
By Priya Pedamkar
Introduction to Copy Command in Linux
Cp command is an abbreviated form of “Copy”.As the name suggests, we can replication either the file or the directory in a particular ecosystem like Unix, Linux, Windows or even Mac Operating system.
Copy Command(cp) is based on a command-line terminal used for copying files/directories. This command creates an exact image of a file/directory with the same/different file name on the disk.cp command takes at least two arguments as input.
Web development, programming languages, Software testing & others
Nowadays its very command to use cp for replicating the files and rysnc command for copying the directories. Copying of file is different from moving a file from source to destination.
Prerequisite
To copy a file using cp command is that the user must have permission for source and target files or directories.
Syntax
where the first & second syntax we will copy source file to destination files or directories
Moreover, the third syntax we can copy multiple source files to target directory and fourth and fifth syntax is with options that we can use for different purposes.
Options
Examples of Copy Command in Linux
Now let us discuss each option with an example for a better understanding of the concept.
1. Copying file to a target directory
Suppose we want to copy the /application/Praveen/file.txt file to /application/Praveen/Backup directory, so need to run below:
Command :
Output :
The above output shows that the file has been copied at the target directory.
2. Multiple file copy at the same time
Command :
Output :
3. Copying the file in interactive mode (-i)
If we want to copy the file in interactive mode, then we will use the option “-i”.Interactive Mode work if the same file already exists in the target directory already.
Command :
Output :
4. Copying File with Verbose mode enabled
We will use “-v” option for this.
Command :
Output :
If we want to use both verbose and interactive mode then use option “-iv”
Command :
Output :
Command :
Output :
The above output shows the files and directory will be recursive.
Let us verify the contents :
Source :
Target:
6. Archiving of files and directory
We will use the option “-a” instead of “-r” or “-R” options
Command :
Output :
7. Copying of file only when new source arrives
There are cases where we want to copy files only when the source file is newer than the target by using the option “-u”. We have added file2.txt at the source.
Command :
Output :
Only file2.txt has been updated in the target directory.
8. If not to overwrite the existing file
There may be the case where we do not want to overwrite the existing file in the target.
In this case use “-n” option
Command:
Command :
Output :
9. Creation of symbolic link of a file
In case we need to create a symbolic link instead of copying then we will use “-s” option.
Command :
Output :
In the above output, we can see that file2.txt colored and pointing to the link.
Also, the permission is “lrwxrwxrwx”, where l stands for a symbolic link.
10. Creation of Hard link of a file
There are cases where we need to have a hard link instead of the symbolic link and copying of file, so we will achieve this by the option “-l”.
In Hard link inode, no. of source and link file will be the same.
Command :
Target file inode :
Source file inode :
We can see that the inode is “131081” for both source and target files.
11. Creation of backup file of existing destination
As we know the by default cp command overwrites the file if exists. If we need to take the backup then we have to use “–backup” option, where we have to give the path at which backup to be taken.
Command :
Output: It is created the same file with the extension(
) on the target path.
12. Copying files/directory forcefully(-f)
There are cases where the existing destination file cannot be opened and removed and if we want to copy the file in place of the existing destination file, then the “-f” option can be used.
Command :
Recommended Articles
This is a guide to Copy Command in Linux. Here we discuss the examples of Copy Command in Linux and its different applications in the Linux system. You may also have a look at the following articles to learn more –
All in One Software Development Bundle (600+ Courses, 50+ projects)
13 ways to use the copy command in Linux (with examples)
You have two ways in which you can copy a file – using the graphical method and the command-line method. In this particular article, we shall look at the command-line method using the cp command. Not just copying, you can perform a lot more with the cp command other than copying files and directories. Let’s get into details.
B eing a Linux user, copying files and directories is one of the everyday tasks that you have to carry out. It can be copying a file to make a backup or copy it to another partition, directory, or external storage drive.
With Unix systems and all Linux distributions, you have two ways in which you can copy a file – using the graphical method and the command-line method. In this particular article, we shall look at the command-line method using the cp command.
CP stands for Copy in Linux systems. The command generally takes two main arguments – the file to copy and the destination to paste the file. This is illustrated with the syntax below:
Below are 13 CP commands examples for Linux users:
1. Copy a file in the same directory but with a different name.
It is a method you can use to make a backup of a file in a different name. The syntax is as follows:
copy a file with CP to a directory in a different name.
From the above image, when we run the ls command, we see that we have a created a duplicate of the testFile with the name fileOne.
2. Copy a single file into a new directory.
Suppose you want to copy a file and paste it to another directory, use the syntax below:
copy a file into a directory
From the above image, we have a file called testFile in the ‘FOSSLINUX’ directory on the Desktop. We want to copy it to the UBUNTU directory inside the FOSSLINUX directory using the command in the image. By running the ls command on the UBUNTU directory, we see that we have successfully copied the file.
3. Copying multiple files into a directory
Suppose you have several files that you want to put in one directory, you don’t need to run a single command for each of them. Below is the syntax we shall use:
Copying multiple files into a directory
From the image above, we have copied the files – testFile testFile2 and testFile3 to the UBUNTU directory with a single command.
4. Use the CP command to copy all files into a directory.
The previous command showed us how we could copy several files into a directory. The method is efficient only for a relatively small number of files since it might be time-consuming. If you want to copy all the contents present to a directory, there is even a more straightforward method – using the asterisk sign ‘*.’ See the syntax below:
Copy ALL files into a directory
From the above image, we have copied all the files in the FOSSLINUX directory on the Desktop to the FOSSTUTS directory.
5. Use the cp command to copy files interactively.
It is an efficient method to copy multiple files to a directory. If the destination directory has a similar file to that in the source directory, it gives you a warning asking you whether to overwrite the file. It is possible using the ‘-i’ parameter. See the syntax below:
From the above image, we are copying files from the FOSSLINUX directory to the FOSSTUTS directory. However, since the FOSSTUTS directory contains files similar to those in the source – FOSSLINUX directory, we get a prompt whether to overwrite the file. Enter ‘y’ for YES and ‘n’ for NO.
6. Use CP to copy files with the verbose(-v) option.
Like most Linux commands, you can use the CP command with the verbose ‘-v’ option. See the syntax below:
Copying files with the Verbose option
From the above image, we added the verbose parameter ‘-v,’ which gives us an output/report of the action we performed.
We can also use both verbose and interactive parameters in one command, as shown in the image below. The syntax is as follows:
Using both verbose and interactive method
7. Use CP to copy a Directory and its contents recursively.
To copy a directory to another destination, we need to use the r or R parameter, which stands for ‘recursive.’ The syntax is as follows:
Copying a directory recursively
From the above image, we have copied all the contents of the FOSSLINUX directory to the FOSSTUTS directory. When we run the ls command on the FOSSTUTS directory, we see that it has contents similar to those in the FOSSLINUX directory.
8. Archive files and directories using the CP command.
Archiving files using cp command
9. Copy only new files than those in the destination directory.
copy only newest files
From the above image, we see that even though we gave the command for copying all files, only the file with the latest edits were copied.
10. Avoid overwriting existing files when using the cp command.
Let’s look at the image below.
11. Create a symbolic link using the cp command.
create a symbolic link with cp command
As illustrated in the image above, we have created a symbolic link to the Documents directory using the testFile1 in the FOSSLINUX directory on the Desktop.
12. Create a hard link using the cp command.
Create a hard link using cp command
13. Copying attributes of file/directory with cp command.
You can use the cp command to copy only the attributes of a file. See the syntax below:
copying attributes-only of a file
Conclusion
From the above examples, we see you can perform a lot more with the cp command other than copying files and directories. These commands can work in any Linux distribution you use, including Ubuntu, Debian, Fedora, and Arch Linux. Is there a cp command you would like to share with our readers? Feel free to post in the comment section below.