How to Copy Files in Linux?
You may need to copy files and folders to another directory when using Linux. NewsPivot introduces different ways to copy files in Linux here, including using the Linux cp command in the terminal.
Way 1: Use Copy and Paste
Just like other operating systems, you can right-click the file or folder and then select Copy, then navigate to the destination directory, right-click the empty space, and select Paste.
Way 2: Use Copy to
Linux system has a Copy to feature, which can help you save while copying files. It is easy to do this.
Step 1. Right-click the target file or folder and select Copy to….
Step 2. Click the destination directory from the left menu on the pop-up interface.
Step 3. Click the green Select button to paste the copied file.
Way 3: Use Keyboard Shortcuts
Like copying and pasting a file in Linux, you can also use the keyboard shortcuts to copy and paste a file. The operations are the same: select the target file and click Ctrl + C, then go to the destination directory and click Ctrl + P.
Way 4: Use Linux cp Command
If you like using the terminal, you can use the Linux cp command to copy a file or folder to the desired destination. In the following content, we will introduce the cp command in Linux in detail.
Also read: Download Ubuntu LTS Releases (24.04, 22.04.4, 20.04.6) & Interim Release (23.10.1).
What Is the Linux cp Command?
The cp command in Linux is one of the most fundamental and frequently used commands for managing files and directories in Linux. The cp command stands for copy. So, this command is used to copy files and directories from one location to another within the filesystem.
The basic syntax is:
cp [OPTIONS] SOURCE DESTINATION
The cp command is useful when you want to back up files, organize files, or duplicate configuration files.
Also read: How to Install Ubuntu on a PC or Laptop: A Complete Guide.
How to Use Linux cp Command: Linux Copy File/Directory Command
Now, let’s get started with the cp command in Linux to help you copy files or directories in the terminal.
Case 1: Copy a Single File to Another Directory
The cp command should contain the source directory and filename, and the destination directory to copy and paste a file.
The syntax of this command is:
cp ./directoryA/filename.extention ./directory
In this command line:
- /directoryA/filename.extention stands for the path of the source file.
- /directory stands for the destination directory.
For example, to copy the PDR.doc file from the Downloads folder to the Documents folder, I this this command line in the terminal:
cp ./Downloads/PDR.docx ./Documents
Case 2: Copy Multiple Files to Another Directory
The cp command also allows you to copy more than one files at the same time. Just use this command:cp ./directoryA/filename1.extention ./directoryA/filename2.extention ./directory.
Case 3: Copy a Directory to Another One
The cp command can work with the -r flag to copy a directory (folder) to another location. The syntax of this command is:
cp -r ./DirectoryA/Folder/ ./DirectoryB
For example, I run the following command to copy the 123 folder from Downloads to Documents:
cp -r ./Downloads/123/ ./Documents
Case 4: Copy Files of One Type from One Directory to Another
The cp command can also work to copy files of one type from one path to another.
The syntax of this command is:
cp ./DirectoryA/*.extension ./DirectoryB
For example, I want to copy all .docx files from Downloads to Documents, so I run this Linux cp command in the terminal:
cp ./Downloads/*.docx ./Documents
Case 5: Copy the File with Preserving File Attributes
The cp command can work with the -p flag to preserve the original file attributes (such as modification time, permissions, and ownership) while copying and pasting.
The syntax of this command is:
cp -p ./DirectoryA/filename.docx ./DirectoryB
Conclusion
The Linux cp command is a fundamental tool for everyday file and directory management. This command can handle a wide range of copying needs, from a single file to multiple files or even an entire folder. Mastering the skills of using this command can help you make full use of this Linux ecosystem.