🕷️ Crawler Inspector

URL Lookup

Direct Parameter Lookup

Raw Queries and Responses

1. Shard Calculation

Query:
Response:
Calculated Shard: 103 (from laksa183)

2. Crawled Status Check

Query:
Response:

3. Robots.txt Check

Query:
Response:

4. Spam/Ban Check

Query:
Response:

5. Seen Status Check

ℹ️ Skipped - page is already crawled

đź“„
INDEXABLE
âś…
CRAWLED
1 day ago
🤖
ROBOTS ALLOWED

Page Info Filters

FilterStatusConditionDetails
HTTP statusPASSdownload_http_code = 200HTTP 200
Age cutoffPASSdownload_stamp > now() - 6 MONTH0.1 months ago
History dropPASSisNull(history_drop_reason)No drop reason
Spam/banPASSfh_dont_index != 1 AND ml_spam_score = 0ml_spam_score=0
CanonicalPASSmeta_canonical IS NULL OR = '' OR = src_unparsedNot set

Page Details

PropertyValue
URLhttps://www.geeksforgeeks.org/linux-unix/cp-command-linux-examples/
Last Crawled2026-04-12 10:51:40 (1 day ago)
First Indexed2025-06-16 00:39:49 (10 months ago)
HTTP Status Code200
Meta Titlecp Command in Linux - GeeksforGeeks
Meta DescriptionYour All-in-One Learning Portal: GeeksforGeeks is a comprehensive educational platform that empowers learners across domains-spanning computer science and programming, school education, upskilling, commerce, software tools, competitive exams, and more., Your All-in-One Learning Portal. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.
Meta Canonicalnull
Boilerpipe Text
Last Updated : 11 Apr, 2026 The cp (copy) command in Linux is used to duplicate files or directories from one location to another within the file system. It supports copying single files, multiple files, and entire directories, with options to control overwriting and attribute preservation. Copy data from one file to another Copy multiple files into a directory Recursively copy directories and subdirectories Overwrite existing files by default It offers three principal modes of operation, each serving different purposes. 1. Copying Between Two Files in Linux When the cp command is provided with two file names, it copies the contents of the source file to the destination file. If the destination file does not exist, it is created. If the destination file already exists, it is overwritten without warning. cp Sorce_file Destination_file Example 1: Copy to a New File Create a new file by copying the contents of an existing file. cp a.txt b.txt a.txt exists in the directory b.txt does not exist, so it is created Contents of a.txt are copied into b.txt copy a file in Linux We used `ls` command to display all the file in the current directory. Example 2: Overwrite an Existing File Replace the contents of an existing file with another file’s contents. cp a.txt c.txt c.txt already exists Its contents are replaced with the contents of a.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. Syntax The cp command has a flexible syntax depending on whether you are copying a single file, multiple files, or directories. cp [options] <source> <destination> cp [options] <source1> <source2> ... <destination_directory> cp: invokes the copy command [options]: optional flags that modify the behavior (e.g., -i, -f, -r, -p) <source>: the file or directory to be copied <source1> <source2> ...: allows specifying multiple source files <destination>: target file or directory <destination_directory>: if copying multiple files, the destination must be a directory ...: indicates that multiple sources can be specified 2. Copy files to a Directory in Linux When the cp command is given one or more source files followed by a destination directory, it copies each source file into the destination directory using the same file names. f the destination directory does not exist, it is created. If files with the same name already exist in the destination, they are overwritten without warning. cp Src_file1 Src_file2 ... Dest_directory Src_file1 Src_file2 ...: one or more source files Dest_directory: directory where files are copied ...: indicates that multiple source files can be specified Example: Copy Multiple Files to a Directory Copy several files into a single directory in one command. cp a.txt b.txt c.txt new/ a.txt, b.txt, and c.txt are source files new/ is the destination directory All files are copied into new/ with their original names 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 By default, the cp command cannot copy directories. To copy a directory and its contents, the -r or -R (recursive) option must be used. cp -R Src_directory Dest_directory -R: enables recursive copying of directories Src_directory: directory to be copied Dest_directory: target directory copying files between two directories Behavior Details If Dest_directory does not exist, it is created and the contents of Src_directory are copied into it. If Dest_directory already exists, Src_directory is copied as a subdirectory inside Dest_directory. Options Available in cp Command 1. -i (Interactive Mode) – Prompt Before Overwriting Files The -i option enables interactive mode for the cp command. In this mode, cp asks for user confirmation before overwriting an existing destination file. By default, cp overwrites files silently. The -i option changes this behavior by introducing a safety check. Syntax: cp -i Source_file Destination_file Example: cp -i a.txt b.txt Copy a File in Linux Using `-i` Here, ls: command shows that both a.txt and b.txt exist cat a.txt: displays the contents of a.txt cat b.txt: displays the contents of b.txt cp -i a.txt b.txt: starts an interactive copy operation The system prompts for confirmation before overwriting b.txt User enters y to confirm the overwrite cat b.txt: now shows the contents copied from a.txt 2. -f (Force Mode) – Overwrite Files Without Prompt The -f option enables force mode for the cp command. In this mode, cp overwrites the destination file without asking for confirmation, even if it has restrictive permissions. By default, if cp cannot write to the destination file due to permissions, it fails. The -f option deletes the destination file first (if needed) and then copies the source file. Syntax: cp -f Source_file Destination_file Example: cp -f a.txt b.txt Copy a File in Linux Using `-f` ls: command shows a.txt and b.txt exist cat a.txt: displays the contents of a.txt cat b.txt: displays the contents of b.txt cp -f a.txt b.txt: starts a forced copy operation b.txt: is overwritten without any prompt, even if permissions are restrictive cat b.txt: now shows the contents copied from a.txt 3. -r / -R (Recursive Mode) – Copy Directories and Subdirectories The -r or -R option enables recursive copying in the cp command. With this option, cp can copy an entire directory along with all its subdirectories and files. By default, cp cannot copy directories without this option. Using -r ensures that the complete directory structure is preserved in the destination. Syntax: cp -r Source_directory Destination_directory Example: cp -r geeksforgeeks gfg geeksforgeeks: is the source directory gfg: is the destination directory (created automatically if it doesn’t exist) All files and subdirectories inside geeksforgeeks are copied recursively 4. -p (Preserve Attributes) – Retain File Permissions, Ownership, and Timestamps The -p option preserves important file attributes while copying. These attributes include: File permissions (read, write, execute) Ownership (user and group, if you have permission) Timestamps (last modification and last access times) By default, cp creates a new file with default attributes. Using -p ensures the copy maintains the same characteristics as the source file. Note: For the preservation of characteristics, you must be the root user of the system, otherwise characteristics change. Syntax: cp -p Source_file Destination_file Example: cp -p d.txt b.txt b.txt is the source file d.txt is the destination file All permissions, ownership (if allowed), and timestamps of b.txt are retained in d.txt File content is also copied exactly 5. * (Wildcard) – Copy Multiple Files Matching a Pattern The * wildcard allows the cp command to select and copy multiple files at once based on a pattern. This is especially useful when you want to copy files with a common extension or naming convention without listing each file individually. How it works: * matches zero or more characters in file names For example, *.txt matches all files ending with .txt in the current directory Syntax: cp *.txt Destination_directory Example: cp *.txt new/ Copy a File in Linux Using `*` All files in the current directory ending with .txt are selected new/ is the destination directory Each matched file is copied into new/ with the same name
Markdown
[![geeksforgeeks](https://media.geeksforgeeks.org/gfg-gg-logo.svg)](https://www.geeksforgeeks.org/) ![search icon](https://media.geeksforgeeks.org/auth-dashboard-uploads/Property=Light---Default.svg) - Sign In - [Courses]() - [Tutorials]() - [Interview Prep]() - [Linux-Unix](https://www.geeksforgeeks.org/linux-unix/linux-tutorial/) - [Interview Questions](https://www.geeksforgeeks.org/linux-unix/linux-interview-questions/) - [Shell Scripting](https://www.geeksforgeeks.org/linux-unix/introduction-linux-shell-shell-scripting/) - [Kali](https://www.geeksforgeeks.org/linux-unix/introduction-to-kali-linux/) - [Ubuntu](https://www.geeksforgeeks.org/linux-unix/how-to-install-ubuntu-on-virtualbox/) - [Red Hat](https://www.geeksforgeeks.org/tag/red-hat/) - [CentOS](https://www.geeksforgeeks.org/linux-unix/getting-started-with-centos/) - [Docker](https://www.geeksforgeeks.org/devops/introduction-to-docker/) - [Kubernetes](https://www.geeksforgeeks.org/devops/introduction-to-kubernetes-k8s/) - [Python](https://www.geeksforgeeks.org/python/python-programming-language-tutorial/) # cp Command in Linux Last Updated : 11 Apr, 2026 The cp (copy) command in Linux is used to duplicate files or directories from one location to another within the file system. It supports copying single files, multiple files, and entire directories, with options to control overwriting and attribute preservation. - Copy data from one file to another - Copy multiple files into a directory - Recursively copy directories and subdirectories - Overwrite existing files by default It offers three principal modes of operation, each serving different purposes. ### 1\. Copying Between Two Files in Linux When the cp command is provided with two file names, it copies the contents of the source file to the destination file. - If the destination file does not exist, it is created. - If the destination file already exists, it is overwritten without warning. ``` cp Sorce_file Destination_file ``` ****Example 1: Copy to a New File**** Create a new file by copying the contents of an existing file. ``` cp a.txt b.txt ``` - a.txt exists in the directory - b.txt does not exist, so it is created - Contents of a.txt are copied into b.txt ![Copy a file in Linux](https://media.geeksforgeeks.org/wp-content/uploads/20231130115327/139.png) copy a file in Linux We used \`ls\` command to display all the file in the current directory. ****Example 2: Overwrite an Existing File**** Replace the contents of an existing file with another file’s contents. ``` cp a.txt c.txt ``` - c.txt already exists - Its contents are replaced with the contents of a.txt ![Copy a file in Linux](https://media.geeksforgeeks.org/wp-content/uploads/20231130120903/140.png) 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. ## Syntax The cp command has a flexible syntax depending on whether you are copying a single file, multiple files, or directories. ``` cp [options] <source> <destination> cp [options] <source1> <source2> ... <destination_directory> ``` - ****cp:**** invokes the copy command - ****\[options\]:**** optional flags that modify the behavior (e.g., -i, -f, -r, -p) - ****\<source\>:**** the file or directory to be copied - ****\<source1\> \<source2\> ...:**** allows specifying multiple source files - ****\<destination\>:**** target file or directory - ****\<destination\_directory\>:**** if copying multiple files, the destination must be a directory - ****...:**** indicates that multiple sources can be specified ### 2\. Copy files to a Directory in Linux When the cp command is given one or more source files followed by a destination directory, it copies each source file into the destination directory using the same file names. - f the destination directory does not exist, it is created. - If files with the same name already exist in the destination, they are overwritten without warning. ``` cp Src_file1 Src_file2 ... Dest_directory ``` - ****Src\_file1 Src\_file2 ...:**** one or more source files - ****Dest\_directory:**** directory where files are copied - ****...:**** indicates that multiple source files can be specified ****Example: Copy Multiple Files to a Directory**** Copy several files into a single directory in one command. ``` cp a.txt b.txt c.txt new/ ``` - a.txt, b.txt, and c.txt are source files - ****new/**** is the destination directory - All files are copied into new/ with their original names ![Copy multiple files to another directory ](https://media.geeksforgeeks.org/wp-content/uploads/20231130121858/141.png) 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 - By default, the cp command cannot copy directories. - To copy a directory and its contents, the -r or -R (recursive) option must be used. ``` cp -R Src_directory Dest_directory ``` - ****\-R:**** enables recursive copying of directories - ****Src\_directory:**** directory to be copied - ****Dest\_directory:**** target directory ![copying files between two directories](https://media.geeksforgeeks.org/wp-content/uploads/20231130124441/142.png) copying files between two directories ****Behavior Details**** - If Dest\_directory does not exist, it is created and the contents of Src\_directory are copied into it. - If Dest\_directory already exists, Src\_directory is copied as a subdirectory inside Dest\_directory. ## Options Available in cp Command ### 1\. -i (Interactive Mode) – Prompt Before Overwriting Files The -i option enables interactive mode for the cp command. In this mode, cp asks for user confirmation before overwriting an existing destination file. By default, cp overwrites files silently. The -i option changes this behavior by introducing a safety check. ****Syntax:**** ``` cp -i Source_file Destination_file ``` ****Example:**** ``` cp -i a.txt b.txt ``` ![Copy a File in Linux Using \`-i\` ](https://media.geeksforgeeks.org/wp-content/uploads/20231130150108/143.png) Copy a File in Linux Using \`-i\` Here, - ****ls:**** command shows that both a.txt and b.txt exist - ****cat a.txt:**** displays the contents of a.txt - ****cat b.txt:**** displays the contents of b.txt - ****cp -i a.txt b.txt:**** starts an interactive copy operation - The system prompts for confirmation before overwriting b.txt - User enters y to confirm the overwrite - ****cat b.txt:**** now shows the contents copied from a.txt ### 2\. -f (Force Mode) – Overwrite Files Without Prompt The -f option enables force mode for the cp command. In this mode, cp overwrites the destination file without asking for confirmation, even if it has restrictive permissions. By default, if cp cannot write to the destination file due to permissions, it fails. The -f option deletes the destination file first (if needed) and then copies the source file. ****Syntax:**** ``` cp -f Source_file Destination_file ``` ****Example:**** ``` cp -f a.txt b.txt ``` ![Copy a File in Linux Using \`-f\` ](https://media.geeksforgeeks.org/wp-content/uploads/20231130150837/144.png) Copy a File in Linux Using \`-f\` - ****ls:**** command shows a.txt and b.txt exist - ****cat a.txt:**** displays the contents of a.txt - ****cat b.txt:**** displays the contents of b.txt - ****cp -f a.txt b.txt:**** starts a forced copy operation - ****b.txt:**** is overwritten without any prompt, even if permissions are restrictive - ****cat b.txt:**** now shows the contents copied from a.txt ### 3\. -r / -R (Recursive Mode) – Copy Directories and Subdirectories The -r or -R option enables recursive copying in the cp command. With this option, cp can copy an entire directory along with all its subdirectories and files. By default, cp cannot copy directories without this option. Using -r ensures that the complete directory structure is preserved in the destination. ****Syntax:**** ``` cp -r Source_directory Destination_directory ``` ****Example:**** ``` cp -r geeksforgeeks gfg ``` ![cp-r](https://media.geeksforgeeks.org/wp-content/uploads/20260128164845028561/cp-r.webp) - ****geeksforgeeks:**** is the source directory - ****gfg:**** is the destination directory (created automatically if it doesn’t exist) - All files and subdirectories inside geeksforgeeks are copied recursively ### 4\. -p (Preserve Attributes) – Retain File Permissions, Ownership, and Timestamps The -p option preserves important file attributes while copying. These attributes include: - File permissions (read, write, execute) - Ownership (user and group, if you have permission) - Timestamps (last modification and last access times) By default, cp creates a new file with default attributes. Using -p ensures the copy maintains the same characteristics as the source file. > ****Note:**** For the preservation of characteristics, you must be the root user of the system, otherwise characteristics change. ****Syntax:**** ``` cp -p Source_file Destination_file ``` ****Example:**** ``` cp -p d.txt b.txt ``` ![cp-p](https://media.geeksforgeeks.org/wp-content/uploads/20260128174345607659/cp-p.webp) - ****b.txt**** is the source file - ****d.txt**** is the destination file - All permissions, ownership (if allowed), and timestamps of b.txt are retained in d.txt - File content is also copied exactly ### 5\. \* (Wildcard) – Copy Multiple Files Matching a Pattern The \* wildcard allows the cp command to select and copy multiple files at once based on a pattern. This is especially useful when you want to copy files with a common extension or naming convention without listing each file individually. ****How it works:**** - \* matches zero or more characters in file names - For example, \*.txt matches all files ending with .txt in the current directory ****Syntax:**** ``` cp *.txt Destination_directory ``` ****Example:**** ``` cp *.txt new/ ``` ![Copy a File in Linux Using \`\*\` ](https://media.geeksforgeeks.org/wp-content/uploads/20231130152418/145.png) Copy a File in Linux Using \`\*\` - All files in the current directory ending with .txt are selected - new/ is the destination directory - Each matched file is copied into new/ with the same name Suggested Quiz ![reset](https://media.geeksforgeeks.org/auth-dashboard-uploads/Reset-icon---Light.svg) 5 Questions What happens if you run cp a.txt b.txt and b.txt already exists? - A Copy fails with an error - B A backup is created automatically - C b.txt is overwritten silently - D a.txt is deleted Which option allows copying an entire directory including all subfolders and files? - A cp -d - B cp -p - C cp -r - D cp -f You want to be asked before a file is overwritten during copy. Which option do you use? - A cp -v - B cp -i - C cp -f - D cp -p Which option preserves original permissions, timestamps, and ownership while copying? - A cp -t - B cp -d - C cp -p - D cp -s You want to copy a folder but avoid overwriting files that already exist in the target location. Which method works safest? - A cp -i -r source/ target/ - B cp -i source/ target/ - C cp source/ target/ - D cp -p source/ target/ ![success](https://media.geeksforgeeks.org/auth-dashboard-uploads/sucess-img.png) Quiz Completed Successfully Your Score :0/5 Accuracy :0% Login to View Explanation **1**/5 \< Previous Next \> Comment [A](https://www.geeksforgeeks.org/user/AKASH%20GUPTA%206/) [AKASH GUPTA 6](https://www.geeksforgeeks.org/user/AKASH%20GUPTA%206/) 43 Article Tags: Article Tags: [Misc](https://www.geeksforgeeks.org/category/misc/) [Linux-Unix](https://www.geeksforgeeks.org/category/linux-unix/) [linux-command](https://www.geeksforgeeks.org/tag/linux-command/) [Linux-file-commands](https://www.geeksforgeeks.org/tag/linux-file-commands/) ### Explore [![GeeksforGeeks](https://media.geeksforgeeks.org/auth-dashboard-uploads/gfgFooterLogo.png)](https://www.geeksforgeeks.org/) ![location](https://media.geeksforgeeks.org/img-practice/Location-1685004904.svg) Corporate & Communications Address: A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305) ![location](https://media.geeksforgeeks.org/img-practice/Location-1685004904.svg) Registered Address: K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305 [![GFG App on Play Store](https://media.geeksforgeeks.org/auth-dashboard-uploads/googleplay-%281%29.png)](https://geeksforgeeksapp.page.link/gfg-app)[![GFG App on App Store](https://media.geeksforgeeks.org/auth-dashboard-uploads/appstore-%281%29.png)](https://geeksforgeeksapp.page.link/gfg-app) - Company - [About Us](https://www.geeksforgeeks.org/about/) - [Legal](https://www.geeksforgeeks.org/legal/) - [Privacy Policy](https://www.geeksforgeeks.org/legal/privacy-policy/) - [Contact Us](https://www.geeksforgeeks.org/about/contact-us/) - [Advertise with us](https://www.geeksforgeeks.org/advertise-with-us/) - [GFG Corporate Solution](https://www.geeksforgeeks.org/gfg-corporate-solution/) - [Campus Training Program](https://www.geeksforgeeks.org/campus-training-program/) - Explore - [POTD](https://www.geeksforgeeks.org/problem-of-the-day) - [Job-A-Thon](https://practice.geeksforgeeks.org/events/rec/job-a-thon/) - [Blogs](https://www.geeksforgeeks.org/category/blogs/?type=recent) - [Nation Skill Up](https://www.geeksforgeeks.org/nation-skill-up/) - Tutorials - [Programming Languages](https://www.geeksforgeeks.org/computer-science-fundamentals/programming-language-tutorials/) - [DSA](https://www.geeksforgeeks.org/dsa/dsa-tutorial-learn-data-structures-and-algorithms/) - [Web Technology](https://www.geeksforgeeks.org/web-tech/web-technology/) - [AI, ML & Data Science](https://www.geeksforgeeks.org/machine-learning/ai-ml-and-data-science-tutorial-learn-ai-ml-and-data-science/) - [DevOps](https://www.geeksforgeeks.org/devops/devops-tutorial/) - [CS Core Subjects](https://www.geeksforgeeks.org/gate/gate-exam-tutorial/) - [Interview Preparation](https://www.geeksforgeeks.org/aptitude/interview-corner/) - [Software and Tools](https://www.geeksforgeeks.org/websites-apps/software-and-tools-a-to-z-list/) - Courses - [ML and Data Science](https://www.geeksforgeeks.org/courses/category/machine-learning-data-science) - [DSA and Placements](https://www.geeksforgeeks.org/courses/category/dsa-placements) - [Web Development](https://www.geeksforgeeks.org/courses/category/development-testing) - [Programming Languages](https://www.geeksforgeeks.org/courses/category/programming-languages) - [DevOps & Cloud](https://www.geeksforgeeks.org/courses/category/cloud-devops) - [GATE](https://www.geeksforgeeks.org/courses/category/gate) - [Trending Technologies](https://www.geeksforgeeks.org/courses/category/trending-technologies/) - Videos - [DSA](https://www.geeksforgeeks.org/videos/category/sde-sheet/) - [Python](https://www.geeksforgeeks.org/videos/category/python/) - [Java](https://www.geeksforgeeks.org/videos/category/java-w6y5f4/) - [C++](https://www.geeksforgeeks.org/videos/category/c/) - [Web Development](https://www.geeksforgeeks.org/videos/category/web-development/) - [Data Science](https://www.geeksforgeeks.org/videos/category/data-science/) - [CS Subjects](https://www.geeksforgeeks.org/videos/category/cs-subjects/) - Preparation Corner - [Interview Corner](https://www.geeksforgeeks.org/interview-prep/interview-corner/) - [Aptitude](https://www.geeksforgeeks.org/aptitude/aptitude-questions-and-answers/) - [Puzzles](https://www.geeksforgeeks.org/aptitude/puzzles/) - [GfG 160](https://www.geeksforgeeks.org/courses/gfg-160-series) - [System Design](https://www.geeksforgeeks.org/system-design/system-design-tutorial/) [@GeeksforGeeks, Sanchhaya Education Private Limited](https://www.geeksforgeeks.org/), [All rights reserved](https://www.geeksforgeeks.org/copyright-information/) ![]()
Readable Markdown
Last Updated : 11 Apr, 2026 The cp (copy) command in Linux is used to duplicate files or directories from one location to another within the file system. It supports copying single files, multiple files, and entire directories, with options to control overwriting and attribute preservation. - Copy data from one file to another - Copy multiple files into a directory - Recursively copy directories and subdirectories - Overwrite existing files by default It offers three principal modes of operation, each serving different purposes. ### 1\. Copying Between Two Files in Linux When the cp command is provided with two file names, it copies the contents of the source file to the destination file. - If the destination file does not exist, it is created. - If the destination file already exists, it is overwritten without warning. ``` cp Sorce_file Destination_file ``` ****Example 1: Copy to a New File**** Create a new file by copying the contents of an existing file. ``` cp a.txt b.txt ``` - a.txt exists in the directory - b.txt does not exist, so it is created - Contents of a.txt are copied into b.txt ![Copy a file in Linux](https://media.geeksforgeeks.org/wp-content/uploads/20231130115327/139.png) copy a file in Linux We used \`ls\` command to display all the file in the current directory. ****Example 2: Overwrite an Existing File**** Replace the contents of an existing file with another file’s contents. ``` cp a.txt c.txt ``` - c.txt already exists - Its contents are replaced with the contents of a.txt ![Copy a file in Linux](https://media.geeksforgeeks.org/wp-content/uploads/20231130120903/140.png) 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. ## Syntax The cp command has a flexible syntax depending on whether you are copying a single file, multiple files, or directories. ``` cp [options] <source> <destination> cp [options] <source1> <source2> ... <destination_directory> ``` - ****cp:**** invokes the copy command - ****\[options\]:**** optional flags that modify the behavior (e.g., -i, -f, -r, -p) - ****\<source\>:**** the file or directory to be copied - ****\<source1\> \<source2\> ...:**** allows specifying multiple source files - ****\<destination\>:**** target file or directory - ****\<destination\_directory\>:**** if copying multiple files, the destination must be a directory - ****...:**** indicates that multiple sources can be specified ### 2\. Copy files to a Directory in Linux When the cp command is given one or more source files followed by a destination directory, it copies each source file into the destination directory using the same file names. - f the destination directory does not exist, it is created. - If files with the same name already exist in the destination, they are overwritten without warning. ``` cp Src_file1 Src_file2 ... Dest_directory ``` - ****Src\_file1 Src\_file2 ...:**** one or more source files - ****Dest\_directory:**** directory where files are copied - ****...:**** indicates that multiple source files can be specified ****Example: Copy Multiple Files to a Directory**** Copy several files into a single directory in one command. ``` cp a.txt b.txt c.txt new/ ``` - a.txt, b.txt, and c.txt are source files - ****new/**** is the destination directory - All files are copied into new/ with their original names ![Copy multiple files to another directory ](https://media.geeksforgeeks.org/wp-content/uploads/20231130121858/141.png) 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 - By default, the cp command cannot copy directories. - To copy a directory and its contents, the -r or -R (recursive) option must be used. ``` cp -R Src_directory Dest_directory ``` - ****\-R:**** enables recursive copying of directories - ****Src\_directory:**** directory to be copied - ****Dest\_directory:**** target directory ![copying files between two directories](https://media.geeksforgeeks.org/wp-content/uploads/20231130124441/142.png) copying files between two directories ****Behavior Details**** - If Dest\_directory does not exist, it is created and the contents of Src\_directory are copied into it. - If Dest\_directory already exists, Src\_directory is copied as a subdirectory inside Dest\_directory. ## Options Available in cp Command ### 1\. -i (Interactive Mode) – Prompt Before Overwriting Files The -i option enables interactive mode for the cp command. In this mode, cp asks for user confirmation before overwriting an existing destination file. By default, cp overwrites files silently. The -i option changes this behavior by introducing a safety check. ****Syntax:**** ``` cp -i Source_file Destination_file ``` ****Example:**** ``` cp -i a.txt b.txt ``` ![Copy a File in Linux Using \`-i\` ](https://media.geeksforgeeks.org/wp-content/uploads/20231130150108/143.png) Copy a File in Linux Using \`-i\` Here, - ****ls:**** command shows that both a.txt and b.txt exist - ****cat a.txt:**** displays the contents of a.txt - ****cat b.txt:**** displays the contents of b.txt - ****cp -i a.txt b.txt:**** starts an interactive copy operation - The system prompts for confirmation before overwriting b.txt - User enters y to confirm the overwrite - ****cat b.txt:**** now shows the contents copied from a.txt ### 2\. -f (Force Mode) – Overwrite Files Without Prompt The -f option enables force mode for the cp command. In this mode, cp overwrites the destination file without asking for confirmation, even if it has restrictive permissions. By default, if cp cannot write to the destination file due to permissions, it fails. The -f option deletes the destination file first (if needed) and then copies the source file. ****Syntax:**** ``` cp -f Source_file Destination_file ``` ****Example:**** ``` cp -f a.txt b.txt ``` ![Copy a File in Linux Using \`-f\` ](https://media.geeksforgeeks.org/wp-content/uploads/20231130150837/144.png) Copy a File in Linux Using \`-f\` - ****ls:**** command shows a.txt and b.txt exist - ****cat a.txt:**** displays the contents of a.txt - ****cat b.txt:**** displays the contents of b.txt - ****cp -f a.txt b.txt:**** starts a forced copy operation - ****b.txt:**** is overwritten without any prompt, even if permissions are restrictive - ****cat b.txt:**** now shows the contents copied from a.txt ### 3\. -r / -R (Recursive Mode) – Copy Directories and Subdirectories The -r or -R option enables recursive copying in the cp command. With this option, cp can copy an entire directory along with all its subdirectories and files. By default, cp cannot copy directories without this option. Using -r ensures that the complete directory structure is preserved in the destination. ****Syntax:**** ``` cp -r Source_directory Destination_directory ``` ****Example:**** ``` cp -r geeksforgeeks gfg ``` ![cp-r](https://media.geeksforgeeks.org/wp-content/uploads/20260128164845028561/cp-r.webp) - ****geeksforgeeks:**** is the source directory - ****gfg:**** is the destination directory (created automatically if it doesn’t exist) - All files and subdirectories inside geeksforgeeks are copied recursively ### 4\. -p (Preserve Attributes) – Retain File Permissions, Ownership, and Timestamps The -p option preserves important file attributes while copying. These attributes include: - File permissions (read, write, execute) - Ownership (user and group, if you have permission) - Timestamps (last modification and last access times) By default, cp creates a new file with default attributes. Using -p ensures the copy maintains the same characteristics as the source file. > ****Note:**** For the preservation of characteristics, you must be the root user of the system, otherwise characteristics change. ****Syntax:**** ``` cp -p Source_file Destination_file ``` ****Example:**** ``` cp -p d.txt b.txt ``` ![cp-p](https://media.geeksforgeeks.org/wp-content/uploads/20260128174345607659/cp-p.webp) - ****b.txt**** is the source file - ****d.txt**** is the destination file - All permissions, ownership (if allowed), and timestamps of b.txt are retained in d.txt - File content is also copied exactly ### 5\. \* (Wildcard) – Copy Multiple Files Matching a Pattern The \* wildcard allows the cp command to select and copy multiple files at once based on a pattern. This is especially useful when you want to copy files with a common extension or naming convention without listing each file individually. ****How it works:**** - \* matches zero or more characters in file names - For example, \*.txt matches all files ending with .txt in the current directory ****Syntax:**** ``` cp *.txt Destination_directory ``` ****Example:**** ``` cp *.txt new/ ``` ![Copy a File in Linux Using \`\*\` ](https://media.geeksforgeeks.org/wp-content/uploads/20231130152418/145.png) Copy a File in Linux Using \`\*\` - All files in the current directory ending with .txt are selected - new/ is the destination directory - Each matched file is copied into new/ with the same name
Shard103 (laksa)
Root Hash12046344915360636903
Unparsed URLorg,geeksforgeeks!www,/linux-unix/cp-command-linux-examples/ s443