{"id":9530,"date":"2026-04-16T12:56:08","date_gmt":"2026-04-16T12:56:08","guid":{"rendered":"https:\/\/commandlinux.com\/?p=9530"},"modified":"2026-04-16T12:56:11","modified_gmt":"2026-04-16T12:56:11","slug":"linux-copy-file","status":"publish","type":"post","link":"https:\/\/commandlinux.com\/qa\/linux-copy-file\/","title":{"rendered":"Copy File Using CP Command In Linux"},"content":{"rendered":"\n<style>\n.clx-wrap pre{background:#f5f5f5;border-left:3px solid #888;padding:13px 16px;overflow-x:auto;margin:0 0 1rem;border-radius:4px;font-size:.875rem}\n.clx-wrap code{font-family:'SFMono-Regular',Consolas,'Liberation Mono',Menlo,monospace;background:#f5f5f5;padding:2px 5px;border-radius:3px;font-size:.875rem}\n.clx-wrap pre code{background:none;padding:0;font-size:.875rem}\n.clx-wrap table{width:100%;border-collapse:collapse;margin:0 0 1rem;font-size:.95rem}\n.clx-wrap th{background:#f7f7f7;font-weight:600;text-align:left;padding:9px 14px;border:1px solid #ddd}\n.clx-wrap td{padding:8px 14px;border:1px solid #ddd;vertical-align:top}\n.clx-wrap tr:nth-child(even) td{background:#fafafa}\n.clx-wrap a{color:#0056b3;text-decoration:none}\n.clx-wrap a:hover{text-decoration:underline}\n<\/style>\n\n<div class=\"clx-wrap\">\n\n<p>The <code>cp<\/code> command is how you copy files and directories from the terminal in Linux. No GUI needed. One command, one result. This guide covers the syntax, the <code>-r<\/code> flag for directories, wildcard patterns, and the most useful options \u2014 with practical examples for each.<\/p>\n\n<h2>cp Command Syntax for Copying Files in Linux<\/h2>\n\n<p>The command takes at minimum two arguments: a source and a destination.<\/p>\n\n<pre><code>cp [options] source destination<\/code><\/pre>\n\n<p>The source is the file or directory you want to duplicate. The destination is where it ends up. If the destination is an existing directory, the copied file lands inside it with its original name. If the destination is a new filename, <code>cp<\/code> creates the copy under that name. The <a href=\"https:\/\/commandlinux.com\/man-page\/man1\/cp\/\">full cp command reference<\/a> lists every available option and its behavior.<\/p>\n\n<h2>How to Linux Copy File to Another Directory<\/h2>\n\n<p>To copy a single file, pass the source path first and the destination second.<\/p>\n\n<pre><code>cp .\/DirectoryA_1\/README.txt .\/DirectoryA_2<\/code><\/pre>\n\n<p>This places a copy of <code>README.txt<\/code> inside <code>DirectoryA_2<\/code>. The original file stays where it is. To rename the copy at the same time, include the new filename in the destination path:<\/p>\n\n<pre><code>cp .\/DirectoryA_1\/README.txt .\/DirectoryA_2\/README_backup.txt<\/code><\/pre>\n\n<p>After any copy operation, run the <a href=\"https:\/\/commandlinux.com\/man-page\/man1\/ls\/\">ls command<\/a> on the destination directory to confirm the file landed where you expected.<\/p>\n\n<h2>Copy Multiple Files at Once with cp<\/h2>\n\n<p>List all source files before the destination. The last argument is always treated as the destination \u2014 everything before it is a source.<\/p>\n\n<pre><code>cp .\/DirectoryA_1\/README.txt .\/DirectoryA_1\/ANOTHER_FILE.txt .\/DirectoryA_2<\/code><\/pre>\n\n<p>Both files end up in <code>DirectoryA_2<\/code>. The rule holds regardless of how many files you list \u2014 the final argument is where they all go.<\/p>\n\n<h2>Linux Copy File Directory Using cp -r<\/h2>\n\n<p>By default, <code>cp<\/code> only handles files. Attempt to copy a directory without any flags and the command stops with an error:<\/p>\n\n<pre><code>cp .\/DirectoryA_1\/Folder\/ .\/DirectoryA_2\n# cp: omitting directory '.\/DirectoryA_1\/Folder\/'<\/code><\/pre>\n\n<p>The <code>-r<\/code> flag tells <code>cp<\/code> to copy recursively \u2014 the named directory along with everything inside it, including nested subdirectories.<\/p>\n\n<pre><code>cp -r .\/DirectoryA_1\/Folder\/ .\/DirectoryA_2<\/code><\/pre>\n\n<p>If the destination path doesn&#8217;t exist, <code>cp<\/code> creates it. If it does exist, the source directory is copied inside it as a subdirectory. The <a href=\"https:\/\/commandlinux.com\/qa\/cp-directory\/\">duplicating folders guide on commandlinux.com<\/a> covers force-copy behavior and how destination path existence changes the output.<\/p>\n\n<h2>Match Files for Linux Copy Using Glob Patterns<\/h2>\n\n<p>Glob patterns let you select multiple files based on naming rules instead of listing them one by one. The <code>*<\/code> character matches any sequence of characters in a filename.<\/p>\n\n<pre><code>cp .\/DirectoryA_1\/*.txt .\/DirectoryA_2<\/code><\/pre>\n\n<p>This copies every <code>.txt<\/code> file in <code>DirectoryA_1<\/code> to <code>DirectoryA_2<\/code> in one shot. You can build more targeted patterns \u2014 matching files that start with a specific string, end with a number, or contain a particular substring \u2014 by adjusting the glob expression.<\/p>\n\n<p>One thing worth knowing: <code>*<\/code> does not match hidden files (those beginning with a dot). To include them in a directory copy, use <code>cp -r<\/code> on the source directory rather than a wildcard.<\/p>\n\n<h2>Useful cp Options for File Copy Operations in Linux<\/h2>\n\n<table>\n  <tr>\n    <th>Flag<\/th>\n    <th>What It Does<\/th>\n  <\/tr>\n  <tr>\n    <td><code>-r<\/code><\/td>\n    <td>Copies a directory and all its contents recursively<\/td>\n  <\/tr>\n  <tr>\n    <td><code>-p<\/code><\/td>\n    <td>Preserves file mode, ownership, and timestamps from the source<\/td>\n  <\/tr>\n  <tr>\n    <td><code>-i<\/code><\/td>\n    <td>Prompts before overwriting any existing file at the destination<\/td>\n  <\/tr>\n  <tr>\n    <td><code>-n<\/code><\/td>\n    <td>Skips the copy entirely if the destination file already exists<\/td>\n  <\/tr>\n  <tr>\n    <td><code>-v<\/code><\/td>\n    <td>Prints each filename as it gets copied (verbose output)<\/td>\n  <\/tr>\n  <tr>\n    <td><code>-f<\/code><\/td>\n    <td>Removes write-protected destination files before copying without prompting<\/td>\n  <\/tr>\n<\/table>\n\n<p>Flags can be combined. <code>cp -rp<\/code> copies a directory recursively while preserving all timestamps and permissions \u2014 useful when archiving project folders or making system backups where original modification times need to stay intact.<\/p>\n\n<h2>Copying Linux Files Between Remote Machines<\/h2>\n\n<p><code>cp<\/code> works within the local filesystem only. For transfers between two machines, <code>scp<\/code> is the right tool. It routes data through SSH, so authentication and encryption are built into every transfer. All the available flags are in the <a href=\"https:\/\/commandlinux.com\/man-page\/man1\/scp\/\">scp command documentation<\/a>.<\/p>\n\n<p>The syntax follows the same source-then-destination pattern, with remote paths formatted as <code>user@host:\/path\/to\/file<\/code>:<\/p>\n\n<pre><code>scp user@remotehost:\/home\/data\/file.txt \/local\/destination\/\nscp -r user@remotehost:\/home\/project\/ \/local\/backup\/<\/code><\/pre>\n\n<p>Before running any remote copy, you need an active SSH connection to the target machine. The <a href=\"https:\/\/commandlinux.com\/qa\/ssh-login\/\">SSH login guide on commandlinux.com<\/a> walks through setup, key-based authentication, and common connection errors. For large or incremental transfers, rsync is generally a better fit than scp since it only sends the parts that changed.<\/p>\n\n<h2>FAQs<\/h2>\n\n<h3>What does the cp command do in Linux?<\/h3>\n<p>The <code>cp<\/code> command copies files or directories from a source path to a destination. The original stays in place. It requires at least two arguments: a source file or directory and a destination path.<\/p>\n\n<h3>How do I copy a directory in Linux using cp?<\/h3>\n<p>Use the <code>-r<\/code> flag: <code>cp -r \/source\/folder\/ \/destination\/<\/code>. Without <code>-r<\/code>, <code>cp<\/code> refuses to copy directories and returns an error. The flag copies the directory along with all its contents recursively.<\/p>\n\n<h3>How do I copy multiple files at once with cp?<\/h3>\n<p>List all source files before the destination: <code>cp file1.txt file2.txt \/destination\/<\/code>. The last argument is always the destination. Every argument before it is treated as a source file to be copied.<\/p>\n\n<h3>How do I prevent cp from overwriting existing files?<\/h3>\n<p>Add the <code>-n<\/code> flag: <code>cp -n source.txt destination\/<\/code>. This skips the copy if a file with the same name already exists at the destination. Use <code>-i<\/code> instead to get a prompt before each overwrite.<\/p>\n\n<h3>What is the difference between cp and scp in Linux?<\/h3>\n<p><code>cp<\/code> copies files within the local filesystem only. <code>scp<\/code> copies files between two machines over a network using SSH encryption. For any remote transfer, <code>scp<\/code> is the correct command \u2014 <code>cp<\/code> has no network capability.<\/p>\n\n<\/div>\n\n","protected":false},"excerpt":{"rendered":"<p>Copy files and directories in Linux quickly using the cp command\u2014no GUI required. One simple terminal command delivers fast, reliable results every time.<\/p>\n","protected":false},"author":1,"featured_media":9531,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[43],"tags":[],"class_list":["post-9530","post","type-post","status-publish","format-standard","has-post-thumbnail","category-qa"],"_links":{"self":[{"href":"https:\/\/commandlinux.com\/wp-json\/wp\/v2\/posts\/9530","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/commandlinux.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/commandlinux.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/commandlinux.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/commandlinux.com\/wp-json\/wp\/v2\/comments?post=9530"}],"version-history":[{"count":1,"href":"https:\/\/commandlinux.com\/wp-json\/wp\/v2\/posts\/9530\/revisions"}],"predecessor-version":[{"id":9532,"href":"https:\/\/commandlinux.com\/wp-json\/wp\/v2\/posts\/9530\/revisions\/9532"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/commandlinux.com\/wp-json\/wp\/v2\/media\/9531"}],"wp:attachment":[{"href":"https:\/\/commandlinux.com\/wp-json\/wp\/v2\/media?parent=9530"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/commandlinux.com\/wp-json\/wp\/v2\/categories?post=9530"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/commandlinux.com\/wp-json\/wp\/v2\/tags?post=9530"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}