- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to copy a file, group of files, or directory in Linux?
In this article, we will learn to copy a file or directory in the Linux/Unix operating system using terminal, we use the cp command to copy a file or directory in the Linux system.
cp (copy) - The cp command is used to copy files or directories in the Linux system. If we want to copy a file from a source directory and paste it into another destination directory then we have to use the cp command. By default, the cp command will not copy directories to copy directories we have to use -R option with cp command otherwise error will be prompt.
The general syntax of the cp command is as follows −
cp [OPTION]...[-T] SOURCE DESTINATION cp [OPTION]...SOURCE DESTINATION cp [OPTION]...-t DIRECTORY SOURCE...
Brief description of option available in the cp command.
|Sr.No.||Option & Description|
Copy only attributes of file
Create backup of destination file
Create backup file without argument
Prompt before overwriting files
Create hard link of files instead of copying
|7||-R, -r, --recursive|
Copy directories recursively
Displays a help message and then exits.
It gives info about the version and then exits.
We will copy a file from a directory and paste it into another directory using cp command
$ cp file.txt ../snow
After executing this command files will be copied, to ensure we will change the directory and list the contents of the directory using the below command.
$ cd ../snow $ ls file.txt
We will copy all .txt (text) files available in a source directory and paste it into the destination directory using * wildcard with cp command in the Linux system.
$ cp *.txt ../snow
Here, we use the -i option to copy a file from the source directory and paste it into the destination directory if the file is already existing in the destination directory with the same name then a warning message will be prompt before overriding the file.
$ cp -I file1.txt ../shadow cp: overwrite ‘../shadow/file.txt’? y
- How to move a file, group of files, and directories in Linux?
- How to copy files into a directory in C#?
- How to change file or directory permission in Linux/Unix?
- How to copy files to a new directory using Python?
- How to unzip all zipped files in a Linux directory?
- How to Copy a File to Multiple Directories in Linux?
- How to add, delete, change ownership (of files) of a group in Linux?
- How to Count Number of Files and Subdirectories inside a Given Linux Directory?
- How to Copy files or Folder without overwriting existing files?
- How to disable delete permission of File and Directory in Linux?
- How to Copy File Permissions and Ownership to Another File in Linux?
- How to create a zip file and ignore directory structure in Linux?
- How to find the most recent file in a directory on Linux?
- Java Program to rename a file or directory
- How to change the file owner and group in Linux?