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. 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: 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. 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 To copy directories, you have to pass the -r flag. This flag informs the cp command to recursively copy a directory and its contents (which could be files or other sub directories). So for the previous command, you can add the flag before the directory sources like this:What is the cp command?
How to copy directories with the cp command
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:
cp ./DirectoryA_1/*.txt ./DirectoryA_2./DirectoryA_1/*.txt matches files with the .txt extension in their names, and the cp command can copy all those files to the destination.
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!
Learn to code for free. freeCodeCamp's open source curriculum has helped more than 40,000 people get jobs as developers. Get started
Introduction
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.
Using the cp Command to Copy Files and Directories in Linux
The cp command is the primary method for copying files and directories in Linux. Virtually all Linux distributions can use cp. The basic format of the command is:
cp [additional_option] source_file target_fileFor example:
cp my_file.txt my_file2.txtThis Linux command creates a copy of the my_file.txt file and renames the new file to my_file2.txt.
By default, the cpcommand 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 –ioption.
Additional Options
Additional options can be used in combination with the cp command:
- –v verbose: shows the progress of multiple copied files
- –p preserve: keeps the same attributes, like creation date and file permissions
- –f force: force the copy by deleting an existing file first
- –i interactive: prompts for confirmation, highly advised
- –R recursive: copies all files and subfolders in a directory
- –u update: copy only if source is newer than destination
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:
cp my_file.txt /new_directoryYou 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:
cp /etc/my_file.txt /new_directoryThis lets you copy without having to change directories. The cpcommand will create the /new_directory if it doesn’t exist.
To rename and copy a file to a different path:
cp my_file.txt /new_directory/my_file2.txt
This option is useful for creating backups of configuration files, or for copying data to a storage device.
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:
cp my_file.txt my_file2.txt my_file3.txt /new_directoryThis 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:
cp /pictures/*.jpg /new_directoryThis 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 –Roption:
–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
The rsync command in Linux is used to synchronize or transfer data between two locations. Usage is similar to cp, but there are a few key differences to note.
To copy a single file, enter the following into a terminal:
rsync –a my_file.txt /new_directory/my_file_backup.txt- The –a option means all, and is included with rsync commands – this preserves subdirectories, symbolic links, and other metadata.
- Replace the my_file.txt file in the working directory.
- Replace /new_directory/ with the destination.
- Using my_file_backup.txt as the target indicates the file will be renamed during the copy.
To copy a directory with rsync, enter the following:
rsync –a /etc/docker/ /home/backup/docker/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:
ls /directoryThe 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:
cd /other_directoryThe command prompt will change to display that you’ve changed directories.
Conclusion
Now you understand how to copy files in Linux. The cp command is a versatile and powerful tool for managing and backing up files.