🕷️ Crawler Inspector

URL Lookup

Direct Parameter Lookup

Raw Queries and Responses

1. Shard Calculation

Query:
Response:
Calculated Shard: 95 (from laksa105)

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
14 days ago
🤖
ROBOTS ALLOWED

Page Info Filters

FilterStatusConditionDetails
HTTP statusPASSdownload_http_code = 200HTTP 200
Age cutoffPASSdownload_stamp > now() - 6 MONTH0.5 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.linode.com/docs/guides/how-to-copy-files-and-directories-in-linux/
Last Crawled2026-03-29 15:20:47 (14 days ago)
First Indexed2022-11-02 00:18:06 (3 years ago)
HTTP Status Code200
Meta TitleHow to Copy Files and Directories in Linux | Linode Docs
Meta DescriptionThis guide explains how to copy files and folders in Linux, including how to copy to different directories.
Meta Canonicalnull
Boilerpipe Text
Copying a file is one of the most common Linux tasks. Ubuntu and other Linux distributions use the cp command to copy one or more files, and to copy entire directories. This guide explains how to use the cp command to copy files on Linux. It also lists the different variations of this command and describes the different cp command options. An Introduction to cp The cp command is used to copy one or more files on a Linux system to a new location. It is similar to the mv command, except it does not move or remove the original file, which remains in place. Like most Linux commands, cp is run using the command line of a system terminal. The cp command allows users to copy a file to either the same directory or a different location. It is also possible to give the copy a different name than the original file. The -r option enables the cp command to operate recursively and copy a directory along with any files and subdirectories it contains. cp has a number of options, allowing users to run it interactively, use verbose mode, or preserve the file attributes of the original. Users must have sudo privileges to copy protected files. Otherwise, sudo is not required. Before You Begin If you have not already done so, create a Linode account and Compute Instance. See our Getting Started with Linode and Creating a Compute Instance guides. Follow our Setting Up and Securing a Compute Instance guide to update your system. You may also wish to set the timezone, configure your hostname, create a limited user account, and harden SSH access. Note This guide is written for a non-root user. Commands that require elevated privileges are prefixed with sudo . If you are not familiar with the sudo command, see the Users and Groups guide. How to Use the cp Command to Copy Files and Directories in Linux The cp command works similarly on most Linux distributions. The command operates in four basic modes. Copies a file to the same directory. The new file must have a different name. Copies a file to a different directory. It is possible to rename the file or retain the old name. Copy multiple files to a different target directory. Recursively copy the contents of a directory, including subdirectories, to a different target directory. There are a number of concerns to be aware of when using cp . For instance, cp does not display a warning when overwriting an existing file. This situation occurs when copying a file to a new directory already containing a file with the same name. This problem is more likely to happen when copying multiple files. To avoid this problem, users can use interactive mode to force Linux to request confirmation before overwriting a file. cp is often used in conjunction with the ls command. ls lists the contents of the current directory. This is handy for confirming the exact name and location of the source files and directories. Some of the most important cp command options include the following: -f : Forces a copy in all circumstances. -i : Runs cp in interactive mode. In this mode, Linux asks for confirmation before overwriting any existing files or directories. Without this option, Linux does not display any warnings. -p : Preserves the file attributes of the original file in the copy. File attributes include the date stamps for file creation and last modification, user ID, group IP, and file permissions. -R : Copies files recursively. All files and subdirectories in the specified source directory are copied to the destination. -u : Overwrites the destination file only if the source file is newer than the destination file. -v : Runs cp in verbose mode. This mode provides extra information on the copying process. This is useful for keeping track of progress when copying a large number of files. The options -H , -L , and -P indicate how the cp command should process symbolic links. See the cp man page for a full description of cp and symbolic links. The options for cp vary between Linux distributions. A list for Ubuntu 22.04 LTS is available in the Ubuntu cp documentation . How to Copy a File in Linux One common use of cp is to make a second copy of the source file in the same directory. Supply a different name for the copy to differentiate it from the original. A common convention is to add an extra extension such as .bak or .cp to the existing file name. For example, a standard name for a backup copy of archive.txt is archive.txt.bak . The cp command operates in the context of the current working directory. However, files can be specified using either an absolute or relative path. Here is the basic cp command to copy a file within the same directory. cp [ options ] sourcefile targetfile The following example demonstrates how to make a backup copy of clock.txt named clock.txt.bak . cp clock.txt clock.txt.bak To confirm the copy operation, use ls to list the files in the directory. Both the original source file and the copy are listed. ls -l -rw-rw-r-- 1 test test 2208 Jul 18 06:00 clock.txt -rw-rw-r-- 1 test test 2208 Jul 20 08:11 clock.txt.bak The same command could be executed using absolute paths for both filenames. cp ~/clock.txt ~/clock.txt.bak To copy a protected file that the root account owns, use sudo . Important Be very careful when copying any files owned by root, especially those in the system / directories. cd /etc sudo cp bash.bashrc bash.bashrc.bak ls -l bash.* -rw-r--r-- 1 root root 2319 Jan 6 2022 bash.bashrc -rw-r--r-- 1 root root 2319 Jul 20 08:15 bash.bashrc.bak To protect against an accidental overwrite, use the interactive option -i . The Linux system prompts for confirmation before it overwrites any existing files. cp clock.txt clock.txt.bak -i cp: overwrite 'clock.txt.bak'? Use the -p option to retain the file attributes of the original file in the copy. For example, Ubuntu assigns the duplicate the same date stamp as the original. cp clock.txt clock.txt.bak -p -rw-rw-r-- 1 test test 2208 Jul 18 06:00 clock.txt -rw-rw-r-- 1 test test 2208 Jul 18 06:00 clock.txt.bak The -v command echoes each copy operation to the standard output. This can be handy for tracking operations that copy hundreds or thousands of files. cp -v clock.txt clock.txt.cp 'clock.txt' -> 'clock.txt.cp' If you accidentally make an unwanted copy of the wrong file, remove the copy using the rm command. sudo rm bash.bashrc.bak How to Copy a File to a Another Directory in Linux The cp command can copy a file to a completely different destination directory on the same Linux system. The copy can retain the same name as the original, but it is also possible to specify a new name for the file. The target directory must already exist before copying any files. The path of the target directory can be either relative or absolute. All the caveats and options that apply when copying a file within a directory also apply in this case. For instance, the cp command silently overwrites an existing file in the destination directory unless the - i option is added. Here is the pattern for copying a file to a directory on Linux. cp sourcefile target_directory_path To give the copy a new name, append the name to the path of the target directory. cp sourcefile target_directory_path/targetfile This example makes a new copy of clock.txt in the archive directory. In this example, the archive directory already exists. cp clock.txt ~/archive Change to the new directory to confirm the successful copy. cd archive ls -l -rw-rw-r-- 1 test test 2208 Jul 20 09:08 clock.txt To give the copy a new name, append the new name to the end of the directory path. Linux gives the new file the filename clock.txt.bak . cp clock.txt ~/archive/clock.txt.bak How to Copy Multiple Files in Linux cp allows users to copy multiple files at a time, but only to a different directory. Here is a template for using cp in this context: cp sourcefile1 sourcefile2 target_directory_path This example copies two files to the archive directory. cp clock.txt system.txt archive The cp command treats the * character is a wildcard. By itself, the wildcard symbol symbolizes all files. The command cp * targetdirectory copies all files in the current directory to targetdirectory . However, it does not copy any directories or perform a recursive copy. When used as part of a string, the * symbol matches any number of any characters. The filter *.txt matches all source files ending with the .txt extension. This example copies all .exe files to the archive directory. cp *.exe archive Change context to the archive directory to confirm both .exe files, and only those files, were copied. cd archive ls cleanup.exe mk_backup.exe How to Copy a Directory in Linux In addition to copying files, Linux can also copy directories. The -R option is used to copy a directory and all of its subdirectories and files recursively. The Linux command to copy a directory follows this structure. cp -R source_directory target_directory To copy the directory archive , along with all of its files and subdirectories, to archive_bkup use this command. cp -R archive archive_bkup Change context to the archive_bkup/archive directory and confirm all the files and subdirectories of archive have been copied. cd archive_bkup/archive ls -l -rw-rw-r-- 1 test test 0 Jul 20 11:10 cleanup.exe -rw-rw-r-- 1 test test 0 Jul 20 11:10 mk_backup.exe drwxrwxr-x 2 test test 4096 Jul 20 11:10 records Conclusion The cp command is used for copying files on Linux systems. It operates similarly to the mv directive, but leaves the original file unaltered and in place. To copy a file to the same directory on Linux, use cp with the name of the source file and the name of the copy. The cp command can also be used to copy the file to a different directory, to copy multiple files, or to recursively copy entire directories. For more in-depth documentation on how to copy files using the Linux cp command, see the cp man page . More Information You may wish to consult the following resources for additional information on this topic. While these are provided in the hope that they will be useful, please note that we cannot vouch for the accuracy or timeliness of externally hosted materials. man page for cp command Ubuntu 22.04 documentation for the cp command This page was originally published on July 18, 2022.
Markdown
- [Search](https://www.linode.com/search/) - [Docs](https://techdocs.akamai.com/cloud-computing/docs/welcome) - [Blog](https://www.linode.com/blog/) - [Support](https://www.linode.com/support/) - [Sales](https://www.linode.com/company/contact/) - [Log in](https://login.linode.com/login) [![Akamai Cloud](https://www.linode.com/linode/en/images/logo/akamai-cloud-logo.svg)](https://www.linode.com/) - Explore - Products - Solutions - Pricing - Resources - [Partners](https://www.linode.com/partners/) - [Sign up](https://login.linode.com/signup) - Mobile - Explore - [Lower costs](https://www.linode.com/reduce-cloud-costs/) - [Lower latency](https://www.linode.com/low-latency-applications/) - [Cloud portability](https://www.linode.com/cloud-portability/) - [Global scale](https://www.linode.com/cloud-global-scale/) - [Our platform](https://www.linode.com/global-infrastructure/) - [Support experience](https://www.linode.com/support-experience/) - Products - [Products Overview](https://www.linode.com/products/) - Compute - [Essential Compute](https://www.linode.com/products/essential-compute/) - [GPU](https://www.linode.com/products/gpu/) - [Accelerated Compute](https://www.linode.com/products/accelerated-compute/) - [Kubernetes](https://www.linode.com/products/kubernetes/) - [App Platform](https://www.linode.com/products/app-platform/) - Storage - [Block Storage](https://www.linode.com/products/block-storage/) - [Object Storage](https://www.linode.com/products/object-storage/) - [Backups](https://www.linode.com/products/backups/) - Networking - [Cloud Firewall](https://www.linode.com/products/cloud-firewall/) - [DNS Manager](https://www.linode.com/products/dns-manager/) - [NodeBalancers](https://www.linode.com/products/nodebalancers/) - [Private Networking](https://www.linode.com/products/private-networking/) - Databases - [Managed Databases](https://www.linode.com/products/databases/) - Services - [Linode Managed](https://www.linode.com/products/managed/) - Solutions - [AI inferencing](https://www.linode.com/solutions/ai-inferencing/) - [Ecommerce](https://www.linode.com/solutions/ecommerce/) - [Gaming](https://www.linode.com/solutions/gaming/) - [Managed hosting](https://www.linode.com/solutions/managed-hosting/) - [Media](https://www.linode.com/solutions/media/) - [SaaS](https://www.linode.com/solutions/saas/) - Pricing - [Pricing list](https://www.linode.com/pricing/) - [Cloud computing calculator](https://www.linode.com/pricing/cloud-cost-calculator/) - Resources - [White paper, ebooks, videos…](https://www.linode.com/resources/) - [Developer hub](https://www.linode.com/developers/) - [Documentation](https://techdocs.akamai.com/cloud-computing/docs/welcome) - [Guides](https://www.linode.com/docs/) - [Reference architectures](https://www.linode.com/docs/reference-architecture/) - [Community Q\&A](https://www.linode.com/community/questions/) - [Beta program](https://www.linode.com/green-light/) - [Startup program](https://www.linode.com/startup-programs/) - [Marketplace apps](https://www.linode.com/marketplace/) - [Blog](https://www.linode.com/blog/) - [Events and workshops](https://www.linode.com/events/) - [Partners](https://www.linode.com/partners/) - Company - [About Us](https://www.linode.com/company/about/) - [Newsroom](https://www.akamai.com/newsroom) - [Careers](https://www.akamai.com/careers) - [Legal](https://www.linode.com/legal/) - [Sales](https://www.linode.com/company/contact/) - [System Status](https://status.linode.com/) - Search - [Docs](https://techdocs.akamai.com/cloud-computing/docs/welcome) - [Blog](https://www.linode.com/blog/) - [Resources](https://www.linode.com/resources/) - [Pricing](https://www.linode.com/pricing/) - [Support](https://www.linode.com/support/) - [Sales](https://www.linode.com/company/contact/) - [Log in](https://login.linode.com/login) - [Sign up](https://login.linode.com/signup) ###### Featured [![Hero Forge Craft of Code Header](https://www.linode.com/ns/features/prod/images/hero-forge-craft-of-code-632x279.jpg)Hero Forge Secure, accessible GPU instances. Unleash millions of adventures. Read story](https://www.akamai.com/resources/customer-story/hero-forge) - [Lower costs](https://www.linode.com/reduce-cloud-costs/) - [Lower latency](https://www.linode.com/low-latency-applications/) - [Cloud portability](https://www.linode.com/cloud-portability/) - [Global scale](https://www.linode.com/cloud-global-scale/) - [Our platform](https://www.linode.com/global-infrastructure/) - [Support experience](https://www.linode.com/support-experience/) ###### Featured [![GPU acceleration](https://www.linode.com/ns/features/prod/images/gpu-438.png)GPU acceleration Power AI and media workloads with on-demand NVIDIA™ GPUs at cost-effective pricing. View product details](https://www.linode.com/products/gpu/) [View all products](https://www.linode.com/products/) ###### Compute - [Essential Compute](https://www.linode.com/products/essential-compute/) - [GPU](https://www.linode.com/products/gpu/) - [Accelerated Compute](https://www.linode.com/products/accelerated-compute/) - [Kubernetes](https://www.linode.com/products/kubernetes/) - [App Platform](https://www.linode.com/products/app-platform/) ###### Storage - [Block Storage](https://www.linode.com/products/block-storage/) - [Object Storage](https://www.linode.com/products/object-storage/) - [Backups](https://www.linode.com/products/backups/) ###### Networking - [Cloud Firewall](https://www.linode.com/products/cloud-firewall/) - [DNS Manager](https://www.linode.com/products/dns-manager/) - [NodeBalancers](https://www.linode.com/products/nodebalancers/) - [Private Networking](https://www.linode.com/products/private-networking/) ###### Databases - [Managed Databases](https://www.linode.com/products/databases/) ###### Services - [Linode Managed](https://www.linode.com/products/managed/) ###### Featured [![Portability in the Cloud: Best Practices for Building SaaS-Based Applications](https://www.linode.com/ns/features/prod/images/MRK-4404-Portability-i632x217-px.jpg)Portability in the cloud Best practices for building SaaS-based applications. Get your copy](https://www.linode.com/resources/ebook/cloud-portability-saas-applications/) ###### Solutions - [AI inferencing](https://www.linode.com/solutions/ai-inferencing/) - [Ecommerce](https://www.linode.com/solutions/ecommerce/) - [Gaming](https://www.linode.com/solutions/gaming/) - [Managed hosting](https://www.linode.com/solutions/managed-hosting/) - [Media](https://www.linode.com/solutions/media/) - [SaaS](https://www.linode.com/solutions/saas/) ###### Featured [![Cloud Pricing Calculator](https://www.linode.com/ns/features/prod/images/Cloud_Pricing_MockUp2-416x376.png)Cloud computing calculator Estimate your cloud costs. Price and configure cloud features to match your needs. Launch calculator](https://www.linode.com/pricing/cloud-cost-calculator/) ###### Pricing - [Pricing list](https://www.linode.com/pricing/) - [Cloud computing calculator](https://www.linode.com/pricing/cloud-cost-calculator/) ###### Featured [![](https://www.linode.com/ns/features/prod/images/developerHub-submenu-sm.jpg)Developer hub Comprehensive guides, documentation, and endpoints. Everything you need to get started. Visit hub](https://www.linode.com/developers/) ###### Library - [White paper, ebooks, videos…](https://www.linode.com/resources/) ###### Technical Resources - [Developer hub](https://www.linode.com/developers/) - [Documentation](https://techdocs.akamai.com/cloud-computing/docs/welcome) - [Guides](https://www.linode.com/docs/) - [Reference architectures](https://www.linode.com/docs/reference-architecture/) ###### Community - [Community Q\&A](https://www.linode.com/community/questions/) - [Beta program](https://www.linode.com/green-light/) - [Startup program](https://www.linode.com/startup-programs/) ###### Marketplace - [Browse apps](https://www.linode.com/marketplace/) ###### What's New - [Blog](https://www.linode.com/blog/) - [Events and workshops](https://www.linode.com/events/) Explore docs 1. [Docs Home](https://www.linode.com/docs/) 2. [Guides](https://www.linode.com/docs/guides/) 3. [Tools & Reference](https://www.linode.com/docs/guides/tools-reference/) 4. [Linux Basics](https://www.linode.com/docs/guides/tools-reference/basics/) - On this page On this page ### On this page 1. [An Introduction to cp](https://www.linode.com/docs/guides/how-to-copy-files-and-directories-in-linux/#an-introduction-to-cp) 2. [Before You Begin](https://www.linode.com/docs/guides/how-to-copy-files-and-directories-in-linux/#before-you-begin) 3. [How to Use the cp Command to Copy Files and Directories in Linux](https://www.linode.com/docs/guides/how-to-copy-files-and-directories-in-linux/#how-to-use-the-cp-command-to-copy-files-and-directories-in-linux) 1. [How to Copy a File in Linux](https://www.linode.com/docs/guides/how-to-copy-files-and-directories-in-linux/#how-to-copy-a-file-in-linux) 2. [How to Copy a File to a Another Directory in Linux](https://www.linode.com/docs/guides/how-to-copy-files-and-directories-in-linux/#how-to-copy-a-file-to-a-another-directory-in-linux) 3. [How to Copy Multiple Files in Linux](https://www.linode.com/docs/guides/how-to-copy-files-and-directories-in-linux/#how-to-copy-multiple-files-in-linux) 4. [How to Copy a Directory in Linux](https://www.linode.com/docs/guides/how-to-copy-files-and-directories-in-linux/#how-to-copy-a-directory-in-linux) 4. [Conclusion](https://www.linode.com/docs/guides/how-to-copy-files-and-directories-in-linux/#conclusion) 5. [More Information](https://www.linode.com/docs/guides/how-to-copy-files-and-directories-in-linux/#more-information) - [Cloud Guides & Tutorials]() - [Akamai + Linode]() - [Applications]() - [Audiences]() - [Concentrations]() - [Databases]() - [Development]() - [Email Server Guides]() - [Game Servers]() - [Kubernetes]() - [Languages]() - [IPs, Networking & Domains]() - [Linode Platform]() - [Quick Answers]() - [Security, Upgrades & Backups]() - [Tools & Reference]() - [Linux Basics]() - [9 Ways to Use the Ls Command in Linux]() - [Add and Remove sudo Access in Ubuntu]() - [An Overview of the usermod Command and How It's Used]() - [Basic Linux Commands: A Beginner’s Guide]() - [Check and Clean a Linux System's Disk Space]() - [Copy Files and Directories in Linux]() - [How to Create Linux Symlinks]() - [Introduction to Linux Concepts]() - [Linux System Administration Basics]() - [Linux Users and Groups]() - [Mount a File System on Linux]() - [Navigate the Linux Terminal and File System]() - [Remove Symbolic Links]() - [Rename Files in Linux]() - [Restart a Linux Server from the Command Line]() - [Setting and Using Linux Environment Variables]() - [Setting Filesystem Quotas on Ubuntu 22.04]() - [Step-by-Step Guide: How to Increase Swap Space in Ubuntu]() - [Using the Terminal]() - [Write to a File From the Shell]() - [Custom Kernels and Distributions]() - [File Transfer]() - [Linux Package Management]() - [Tools]() - [Uptime & Analytics]() - [Web Server Guides]() - [Website Guides]() - [Reference Architectures]() - [Quick Deploy Apps]() - [Products]() - [API]() - [Blog]() - [Content Resources]() Filtered by search term 1. [Docs Home](https://www.linode.com/docs/) List Sections Tiles Product docs and API reference are now on Akamai TechDocs. [Search product docs. Search for “” in product docs.]() [Search API reference. Search for “” in API reference.]() # Search Results results matching results ## No Results [![Article image]()]() Previous Next ## Filters Filters ( ) #### All #### Add tags All #### Add authors All 1. [Docs Home](https://www.linode.com/docs/) 2. [Guides](https://www.linode.com/docs/guides/) 3. [Tools & Reference](https://www.linode.com/docs/guides/tools-reference/) 4. [Linux Basics](https://www.linode.com/docs/guides/tools-reference/basics/) # Copy Files and Directories in Linux Published July 18, 2022 by [Jeff Novotny](https://www.linode.com/docs/contributors/jeff-novotny/) - [Report an Issue]() - [View File](https://github.com/linode/docs/blob/develop/docs/guides/tools-reference/basics/how-to-copy-files-and-directories-in-linux/index.md) - [Edit File]() - [Share on Twitter]() - [Share on Facebook]() - [Share on Hacker News]() > **Traducciones al Español** > > Estamos traduciendo nuestros guías y tutoriales al Español. Es posible que usted esté viendo una traducción generada automáticamente. Estamos trabajando con traductores profesionales para verificar las traducciones de nuestro sitio web. Este proyecto es un trabajo en curso. [Create a Linode account]() to try this guide with a \$100 credit. This credit will be applied to any valid services used during your first 60 days. Copying a file is one of the most common Linux tasks. Ubuntu and other Linux distributions use the `cp` command to copy one or more files, and to copy entire directories. This guide explains how to use the `cp` command to copy files on Linux. It also lists the different variations of this command and describes the different `cp` command options. ## An Introduction to cp The `cp` command is used to copy one or more files on a Linux system to a new location. It is similar to the `mv` command, except it does not move or remove the original file, which remains in place. Like most Linux commands, `cp` is run using the command line of a system terminal. The `cp` command allows users to copy a file to either the same directory or a different location. It is also possible to give the copy a different name than the original file. The `-r` option enables the `cp` command to operate recursively and copy a directory along with any files and subdirectories it contains. `cp` has a number of options, allowing users to run it interactively, use verbose mode, or preserve the file attributes of the original. Users must have `sudo` privileges to copy protected files. Otherwise, `sudo` is not required. ## Before You Begin 1. If you have not already done so, create a Linode account and Compute Instance. See our [Getting Started with Linode](https://www.linode.com/docs/products/platform/get-started/) and [Creating a Compute Instance](https://www.linode.com/docs/products/compute/compute-instances/guides/create/) guides. 2. Follow our [Setting Up and Securing a Compute Instance](https://www.linode.com/docs/products/compute/compute-instances/guides/set-up-and-secure/) guide to update your system. You may also wish to set the timezone, configure your hostname, create a limited user account, and harden SSH access. Note This guide is written for a non-root user. Commands that require elevated privileges are prefixed with `sudo`. If you are not familiar with the `sudo` command, see the [Users and Groups](https://www.linode.com/docs/guides/linux-users-and-groups/) guide. ## How to Use the cp Command to Copy Files and Directories in Linux The `cp` command works similarly on most Linux distributions. The command operates in four basic modes. - Copies a file to the same directory. The new file must have a different name. - Copies a file to a different directory. It is possible to rename the file or retain the old name. - Copy multiple files to a different target directory. - Recursively copy the contents of a directory, including subdirectories, to a different target directory. There are a number of concerns to be aware of when using `cp`. For instance, `cp` does not display a warning when overwriting an existing file. This situation occurs when copying a file to a new directory already containing a file with the same name. This problem is more likely to happen when copying multiple files. To avoid this problem, users can use *interactive mode* to force Linux to request confirmation before overwriting a file. `cp` is often used in conjunction with the `ls` command. `ls` lists the contents of the current directory. This is handy for confirming the exact name and location of the source files and directories. Some of the most important `cp` command options include the following: - `-f`: Forces a copy in all circumstances. - `-i`: Runs `cp` in interactive mode. In this mode, Linux asks for confirmation before overwriting any existing files or directories. Without this option, Linux does not display any warnings. - `-p`: Preserves the file attributes of the original file in the copy. File attributes include the date stamps for file creation and last modification, user ID, group IP, and file permissions. - `-R`: Copies files recursively. All files and subdirectories in the specified source directory are copied to the destination. - `-u`: Overwrites the destination file only if the source file is newer than the destination file. - `-v`: Runs `cp` in *verbose* mode. This mode provides extra information on the copying process. This is useful for keeping track of progress when copying a large number of files. The options `-H`, `-L`, and `-P` indicate how the `cp` command should process symbolic links. See the [cp man page](https://pubs.opengroup.org/onlinepubs/9699919799/utilities/cp.html) for a full description of `cp` and symbolic links. The options for `cp` vary between Linux distributions. A list for Ubuntu 22.04 LTS is available in the [Ubuntu cp documentation](https://manpages.ubuntu.com/manpages/jammy/man1/cp.1.html). ### How to Copy a File in Linux One common use of `cp` is to make a second copy of the source file in the same directory. Supply a different name for the copy to differentiate it from the original. A common convention is to add an extra extension such as `.bak` or `.cp` to the existing file name. For example, a standard name for a backup copy of `archive.txt` is `archive.txt.bak`. The `cp` command operates in the context of the current working directory. However, files can be specified using either an absolute or relative path. Here is the basic `cp` command to copy a file within the same directory. ``` cp [options] sourcefile targetfile ``` The following example demonstrates how to make a backup copy of `clock.txt` named `clock.txt.bak`. ``` cp clock.txt clock.txt.bak ``` To confirm the copy operation, use `ls` to list the files in the directory. Both the original source file and the copy are listed. ``` ls -l ``` ``` -rw-rw-r-- 1 test test 2208 Jul 18 06:00 clock.txt -rw-rw-r-- 1 test test 2208 Jul 20 08:11 clock.txt.bak ``` The same command could be executed using absolute paths for both filenames. ``` cp ~/clock.txt ~/clock.txt.bak ``` To copy a protected file that the `root` account owns, use `sudo`. Important Be very careful when copying any files owned by root, especially those in the system `/` directories. ``` cd /etc sudo cp bash.bashrc bash.bashrc.bak ls -l bash.* ``` ``` -rw-r--r-- 1 root root 2319 Jan 6 2022 bash.bashrc -rw-r--r-- 1 root root 2319 Jul 20 08:15 bash.bashrc.bak ``` To protect against an accidental overwrite, use the interactive option `-i`. The Linux system prompts for confirmation before it overwrites any existing files. ``` cp clock.txt clock.txt.bak -i ``` ``` cp: overwrite 'clock.txt.bak'? ``` Use the `-p` option to retain the file attributes of the original file in the copy. For example, Ubuntu assigns the duplicate the same date stamp as the original. ``` cp clock.txt clock.txt.bak -p ``` ``` -rw-rw-r-- 1 test test 2208 Jul 18 06:00 clock.txt -rw-rw-r-- 1 test test 2208 Jul 18 06:00 clock.txt.bak ``` The `-v` command echoes each copy operation to the standard output. This can be handy for tracking operations that copy hundreds or thousands of files. ``` cp -v clock.txt clock.txt.cp ``` ``` 'clock.txt' -> 'clock.txt.cp' ``` If you accidentally make an unwanted copy of the wrong file, remove the copy using the `rm` command. ``` sudo rm bash.bashrc.bak ``` ### How to Copy a File to a Another Directory in Linux The `cp` command can copy a file to a completely different destination directory on the same Linux system. The copy can retain the same name as the original, but it is also possible to specify a new name for the file. The target directory must already exist before copying any files. The path of the target directory can be either relative or absolute. All the caveats and options that apply when copying a file within a directory also apply in this case. For instance, the `cp` command silently overwrites an existing file in the destination directory unless the -`i` option is added. Here is the pattern for copying a file to a directory on Linux. ``` cp sourcefile target_directory_path ``` To give the copy a new name, append the name to the path of the target directory. ``` cp sourcefile target_directory_path/targetfile ``` This example makes a new copy of `clock.txt` in the `archive` directory. In this example, the `archive` directory already exists. ``` cp clock.txt ~/archive ``` Change to the new directory to confirm the successful copy. ``` cd archive ls -l ``` ``` -rw-rw-r-- 1 test test 2208 Jul 20 09:08 clock.txt ``` To give the copy a new name, append the new name to the end of the directory path. Linux gives the new file the filename `clock.txt.bak`. ``` cp clock.txt ~/archive/clock.txt.bak ``` ### How to Copy Multiple Files in Linux `cp` allows users to copy multiple files at a time, but only to a different directory. Here is a template for using `cp` in this context: ``` cp sourcefile1 sourcefile2 target_directory_path ``` This example copies two files to the `archive` directory. ``` cp clock.txt system.txt archive ``` The `cp` command treats the `*` character is a wildcard. By itself, the wildcard symbol symbolizes all files. The command `cp * targetdirectory` copies all files in the current directory to `targetdirectory`. However, it does not copy any directories or perform a recursive copy. When used as part of a string, the `*` symbol matches any number of any characters. The filter `*.txt` matches all source files ending with the `.txt` extension. This example copies all `.exe` files to the `archive` directory. ``` cp *.exe archive ``` Change context to the `archive` directory to confirm both `.exe` files, and only those files, were copied. ``` cd archive ls ``` ``` cleanup.exe mk_backup.exe ``` ### How to Copy a Directory in Linux In addition to copying files, Linux can also copy directories. The `-R` option is used to copy a directory and all of its subdirectories and files recursively. The Linux command to copy a directory follows this structure. ``` cp -R source_directory target_directory ``` To copy the directory `archive`, along with all of its files and subdirectories, to `archive_bkup` use this command. ``` cp -R archive archive_bkup ``` Change context to the `archive_bkup/archive` directory and confirm all the files and subdirectories of `archive` have been copied. ``` cd archive_bkup/archive ls -l ``` ``` -rw-rw-r-- 1 test test 0 Jul 20 11:10 cleanup.exe -rw-rw-r-- 1 test test 0 Jul 20 11:10 mk_backup.exe drwxrwxr-x 2 test test 4096 Jul 20 11:10 records ``` ## Conclusion The `cp` command is used for copying files on Linux systems. It operates similarly to the `mv` directive, but leaves the original file unaltered and in place. To copy a file to the same directory on Linux, use `cp` with the name of the source file and the name of the copy. The `cp` command can also be used to copy the file to a different directory, to copy multiple files, or to recursively copy entire directories. For more in-depth documentation on how to copy files using the Linux `cp` command, see the [cp man page](https://pubs.opengroup.org/onlinepubs/9699919799/utilities/cp.html). ## More Information You may wish to consult the following resources for additional information on this topic. While these are provided in the hope that they will be useful, please note that we cannot vouch for the accuracy or timeliness of externally hosted materials. - [man page for cp command](https://pubs.opengroup.org/onlinepubs/9699919799/utilities/cp.html) - [Ubuntu 22.04 documentation for the cp command](https://manpages.ubuntu.com/manpages/jammy/man1/cp.1.html) This page was originally published on July 18, 2022. - [Report an Issue]() - [View File](https://github.com/linode/docs/blob/develop/docs/guides/tools-reference/basics/how-to-copy-files-and-directories-in-linux/index.md) - [Edit File]() - [Share on Twitter]() - [Share on Facebook]() - [Share on Hacker News]() *** *** Join the conversation. Read other comments or post your own below. Comments must be respectful, constructive, and relevant to the topic of the guide. Do not post external links or advertisements. Before posting, consider if your comment would be better addressed by contacting our [Support team](https://www.linode.com/support/) or asking on our [Community Site](https://www.linode.com/community/questions/). The Disqus commenting system for Linode Docs requires the acceptance of Functional Cookies, which allow us to analyze site usage so we can measure and improve performance. To view and create comments for this article, please [update your Cookie Preferences](https://www.linode.com/docs/guides/how-to-copy-files-and-directories-in-linux/) on this website and refresh this web page. Please note: You must have JavaScript enabled in your browser. Please enable JavaScript to view the \<a href=http://disqus.com/?ref\_noscript\>comments powered by Disqus.\</a\> [comments powered by Disqus](http://disqus.com/) ### On this page 1. [An Introduction to cp](https://www.linode.com/docs/guides/how-to-copy-files-and-directories-in-linux/#an-introduction-to-cp) 2. [Before You Begin](https://www.linode.com/docs/guides/how-to-copy-files-and-directories-in-linux/#before-you-begin) 3. [How to Use the cp Command to Copy Files and Directories in Linux](https://www.linode.com/docs/guides/how-to-copy-files-and-directories-in-linux/#how-to-use-the-cp-command-to-copy-files-and-directories-in-linux) 1. [How to Copy a File in Linux](https://www.linode.com/docs/guides/how-to-copy-files-and-directories-in-linux/#how-to-copy-a-file-in-linux) 2. [How to Copy a File to a Another Directory in Linux](https://www.linode.com/docs/guides/how-to-copy-files-and-directories-in-linux/#how-to-copy-a-file-to-a-another-directory-in-linux) 3. [How to Copy Multiple Files in Linux](https://www.linode.com/docs/guides/how-to-copy-files-and-directories-in-linux/#how-to-copy-multiple-files-in-linux) 4. [How to Copy a Directory in Linux](https://www.linode.com/docs/guides/how-to-copy-files-and-directories-in-linux/#how-to-copy-a-directory-in-linux) 4. [Conclusion](https://www.linode.com/docs/guides/how-to-copy-files-and-directories-in-linux/#conclusion) 5. [More Information](https://www.linode.com/docs/guides/how-to-copy-files-and-directories-in-linux/#more-information) #### Products - [Products Overview](https://www.linode.com/products/) - [Essential Compute](https://www.linode.com/products/essential-compute/) - [GPU](https://www.linode.com/products/gpu/) - [Accelerated Compute](https://www.linode.com/products/accelerated-compute/) - [Kubernetes](https://www.linode.com/products/kubernetes/) - [App Platform](https://www.linode.com/products/app-platform/) - [Block Storage](https://www.linode.com/products/block-storage/) - [Object Storage](https://www.linode.com/products/object-storage/) - [Backups](https://www.linode.com/products/backups/) - [Cloud Firewall](https://www.linode.com/products/cloud-firewall/) - [DNS Manager](https://www.linode.com/products/dns-manager/) - [NodeBalancers](https://www.linode.com/products/nodebalancers/) - [Private Networking](https://www.linode.com/products/private-networking/) - [Managed Databases](https://www.linode.com/products/databases/) - [Linode Managed](https://www.linode.com/products/managed/) - [Distributions](https://www.linode.com/distributions/) - [Kernels](https://www.linode.com/kernels/) #### Company - [About Akamai](https://www.akamai.com/company) - [History](https://www.akamai.com/company/company-history) - [Leadership](https://www.akamai.com/company/leadership) - [Facts and Figures](https://www.akamai.com/company/facts-figures) - [Awards](https://www.akamai.com/company/our-awards) - [Board of Directors](https://www.akamai.com/company/leadership/board-of-directors) - [Investor Relations](https://www.ir.akamai.com/) - [Corporate Responsibility](https://www.akamai.com/company/corporate-responsibility) - [Ethics](https://www.akamai.com/company/ethics-and-compliance) - [Locations](https://www.akamai.com/company/locations) - [Vulnerability Reporting](https://www.akamai.com/global-services/support/vulnerability-reporting) - [Accessibility Commitment](https://www.linode.com/accessibility/) #### Careers - [Careers](https://www.akamai.com/careers) - [Working at Akamai](https://www.akamai.com/careers/working-at-akamai) - [Students and Recent Grads](https://www.akamai.com/careers/students-and-recent-graduates) - [Workplace Diversity](https://www.akamai.com/careers/workplace-diversity) - [Search Jobs](https://akamaicareers.inflightcloud.com/search?searchable=%5B%5D&section=aka_ext) - [Culture Blog](https://www.akamai.com/blog/culture) #### Newsroom - [Newsroom](https://www.akamai.com/newsroom) - [Press Releases](https://www.akamai.com/newsroom/press-release) - [In the News](https://www.akamai.com/newsroom/in-the-news) - [Media Resources](https://www.akamai.com/newsroom/media-resources) #### Legal & Compliance - [Linode Legal](https://www.linode.com/legal/) - [Akamai Legal](https://www.akamai.com/legal) - [Linode Terms of Service (MSA)](https://www.linode.com/legal-msa/) - [EU Digital Services Act (DSA)](https://www.linode.com/eu-digital-services-act/) - [Information Security Compliance](https://www.akamai.com/legal/compliance) - [Privacy Trust Center](https://www.akamai.com/legal/compliance/privacy-trust-center) - [Cookie Settings](https://www.linode.com/legal/manage-cookie-preferences/) #### Glossary - [What Is Cloud Security?](https://www.akamai.com/glossary/what-is-cloud-security) - [What Is Cloud Redundancy?](https://www.akamai.com/glossary/what-is-cloud-redundancy) - [What Is Cloud Computing Security?](https://www.akamai.com/glossary/what-is-cloud-computing-security) - [What is Cloud Portability?](https://www.akamai.com/glossary/what-is-cloud-portability) - [What Is Event-Driven Architecture?](https://www.akamai.com/glossary/what-is-event-driven-architecture) - [See all](https://www.akamai.com/glossary) [![Akamai Logo](https://www.linode.com/linode/en/images/logo/akamai-logo.svg)](https://www.akamai.com/) - Cookie Preferences - [Support](https://www.linode.com/support/) - [Contact Us](https://www.linode.com/company/contact/) - [System Status](https://status.linode.com/) - [Site Map](https://www.linode.com/site-map/) - © 2003-2025 Linode LLC. All rights reserved.
Readable Markdown
Copying a file is one of the most common Linux tasks. Ubuntu and other Linux distributions use the `cp` command to copy one or more files, and to copy entire directories. This guide explains how to use the `cp` command to copy files on Linux. It also lists the different variations of this command and describes the different `cp` command options. ## An Introduction to cp The `cp` command is used to copy one or more files on a Linux system to a new location. It is similar to the `mv` command, except it does not move or remove the original file, which remains in place. Like most Linux commands, `cp` is run using the command line of a system terminal. The `cp` command allows users to copy a file to either the same directory or a different location. It is also possible to give the copy a different name than the original file. The `-r` option enables the `cp` command to operate recursively and copy a directory along with any files and subdirectories it contains. `cp` has a number of options, allowing users to run it interactively, use verbose mode, or preserve the file attributes of the original. Users must have `sudo` privileges to copy protected files. Otherwise, `sudo` is not required. ## Before You Begin 1. If you have not already done so, create a Linode account and Compute Instance. See our [Getting Started with Linode](https://www.linode.com/docs/products/platform/get-started/) and [Creating a Compute Instance](https://www.linode.com/docs/products/compute/compute-instances/guides/create/) guides. 2. Follow our [Setting Up and Securing a Compute Instance](https://www.linode.com/docs/products/compute/compute-instances/guides/set-up-and-secure/) guide to update your system. You may also wish to set the timezone, configure your hostname, create a limited user account, and harden SSH access. Note This guide is written for a non-root user. Commands that require elevated privileges are prefixed with `sudo`. If you are not familiar with the `sudo` command, see the [Users and Groups](https://www.linode.com/docs/guides/linux-users-and-groups/) guide. ## How to Use the cp Command to Copy Files and Directories in Linux The `cp` command works similarly on most Linux distributions. The command operates in four basic modes. - Copies a file to the same directory. The new file must have a different name. - Copies a file to a different directory. It is possible to rename the file or retain the old name. - Copy multiple files to a different target directory. - Recursively copy the contents of a directory, including subdirectories, to a different target directory. There are a number of concerns to be aware of when using `cp`. For instance, `cp` does not display a warning when overwriting an existing file. This situation occurs when copying a file to a new directory already containing a file with the same name. This problem is more likely to happen when copying multiple files. To avoid this problem, users can use *interactive mode* to force Linux to request confirmation before overwriting a file. `cp` is often used in conjunction with the `ls` command. `ls` lists the contents of the current directory. This is handy for confirming the exact name and location of the source files and directories. Some of the most important `cp` command options include the following: - `-f`: Forces a copy in all circumstances. - `-i`: Runs `cp` in interactive mode. In this mode, Linux asks for confirmation before overwriting any existing files or directories. Without this option, Linux does not display any warnings. - `-p`: Preserves the file attributes of the original file in the copy. File attributes include the date stamps for file creation and last modification, user ID, group IP, and file permissions. - `-R`: Copies files recursively. All files and subdirectories in the specified source directory are copied to the destination. - `-u`: Overwrites the destination file only if the source file is newer than the destination file. - `-v`: Runs `cp` in *verbose* mode. This mode provides extra information on the copying process. This is useful for keeping track of progress when copying a large number of files. The options `-H`, `-L`, and `-P` indicate how the `cp` command should process symbolic links. See the [cp man page](https://pubs.opengroup.org/onlinepubs/9699919799/utilities/cp.html) for a full description of `cp` and symbolic links. The options for `cp` vary between Linux distributions. A list for Ubuntu 22.04 LTS is available in the [Ubuntu cp documentation](https://manpages.ubuntu.com/manpages/jammy/man1/cp.1.html). ### How to Copy a File in Linux One common use of `cp` is to make a second copy of the source file in the same directory. Supply a different name for the copy to differentiate it from the original. A common convention is to add an extra extension such as `.bak` or `.cp` to the existing file name. For example, a standard name for a backup copy of `archive.txt` is `archive.txt.bak`. The `cp` command operates in the context of the current working directory. However, files can be specified using either an absolute or relative path. Here is the basic `cp` command to copy a file within the same directory. ``` cp [options] sourcefile targetfile ``` The following example demonstrates how to make a backup copy of `clock.txt` named `clock.txt.bak`. ``` cp clock.txt clock.txt.bak ``` To confirm the copy operation, use `ls` to list the files in the directory. Both the original source file and the copy are listed. ``` ls -l ``` ``` -rw-rw-r-- 1 test test 2208 Jul 18 06:00 clock.txt -rw-rw-r-- 1 test test 2208 Jul 20 08:11 clock.txt.bak ``` The same command could be executed using absolute paths for both filenames. ``` cp ~/clock.txt ~/clock.txt.bak ``` To copy a protected file that the `root` account owns, use `sudo`. Important Be very careful when copying any files owned by root, especially those in the system `/` directories. ``` cd /etc sudo cp bash.bashrc bash.bashrc.bak ls -l bash.* ``` ``` -rw-r--r-- 1 root root 2319 Jan 6 2022 bash.bashrc -rw-r--r-- 1 root root 2319 Jul 20 08:15 bash.bashrc.bak ``` To protect against an accidental overwrite, use the interactive option `-i`. The Linux system prompts for confirmation before it overwrites any existing files. ``` cp clock.txt clock.txt.bak -i ``` ``` cp: overwrite 'clock.txt.bak'? ``` Use the `-p` option to retain the file attributes of the original file in the copy. For example, Ubuntu assigns the duplicate the same date stamp as the original. ``` cp clock.txt clock.txt.bak -p ``` ``` -rw-rw-r-- 1 test test 2208 Jul 18 06:00 clock.txt -rw-rw-r-- 1 test test 2208 Jul 18 06:00 clock.txt.bak ``` The `-v` command echoes each copy operation to the standard output. This can be handy for tracking operations that copy hundreds or thousands of files. ``` cp -v clock.txt clock.txt.cp ``` ``` 'clock.txt' -> 'clock.txt.cp' ``` If you accidentally make an unwanted copy of the wrong file, remove the copy using the `rm` command. ``` sudo rm bash.bashrc.bak ``` ### How to Copy a File to a Another Directory in Linux The `cp` command can copy a file to a completely different destination directory on the same Linux system. The copy can retain the same name as the original, but it is also possible to specify a new name for the file. The target directory must already exist before copying any files. The path of the target directory can be either relative or absolute. All the caveats and options that apply when copying a file within a directory also apply in this case. For instance, the `cp` command silently overwrites an existing file in the destination directory unless the -`i` option is added. Here is the pattern for copying a file to a directory on Linux. ``` cp sourcefile target_directory_path ``` To give the copy a new name, append the name to the path of the target directory. ``` cp sourcefile target_directory_path/targetfile ``` This example makes a new copy of `clock.txt` in the `archive` directory. In this example, the `archive` directory already exists. ``` cp clock.txt ~/archive ``` Change to the new directory to confirm the successful copy. ``` cd archive ls -l ``` ``` -rw-rw-r-- 1 test test 2208 Jul 20 09:08 clock.txt ``` To give the copy a new name, append the new name to the end of the directory path. Linux gives the new file the filename `clock.txt.bak`. ``` cp clock.txt ~/archive/clock.txt.bak ``` ### How to Copy Multiple Files in Linux `cp` allows users to copy multiple files at a time, but only to a different directory. Here is a template for using `cp` in this context: ``` cp sourcefile1 sourcefile2 target_directory_path ``` This example copies two files to the `archive` directory. ``` cp clock.txt system.txt archive ``` The `cp` command treats the `*` character is a wildcard. By itself, the wildcard symbol symbolizes all files. The command `cp * targetdirectory` copies all files in the current directory to `targetdirectory`. However, it does not copy any directories or perform a recursive copy. When used as part of a string, the `*` symbol matches any number of any characters. The filter `*.txt` matches all source files ending with the `.txt` extension. This example copies all `.exe` files to the `archive` directory. ``` cp *.exe archive ``` Change context to the `archive` directory to confirm both `.exe` files, and only those files, were copied. ``` cd archive ls ``` ``` cleanup.exe mk_backup.exe ``` ### How to Copy a Directory in Linux In addition to copying files, Linux can also copy directories. The `-R` option is used to copy a directory and all of its subdirectories and files recursively. The Linux command to copy a directory follows this structure. ``` cp -R source_directory target_directory ``` To copy the directory `archive`, along with all of its files and subdirectories, to `archive_bkup` use this command. ``` cp -R archive archive_bkup ``` Change context to the `archive_bkup/archive` directory and confirm all the files and subdirectories of `archive` have been copied. ``` cd archive_bkup/archive ls -l ``` ``` -rw-rw-r-- 1 test test 0 Jul 20 11:10 cleanup.exe -rw-rw-r-- 1 test test 0 Jul 20 11:10 mk_backup.exe drwxrwxr-x 2 test test 4096 Jul 20 11:10 records ``` ## Conclusion The `cp` command is used for copying files on Linux systems. It operates similarly to the `mv` directive, but leaves the original file unaltered and in place. To copy a file to the same directory on Linux, use `cp` with the name of the source file and the name of the copy. The `cp` command can also be used to copy the file to a different directory, to copy multiple files, or to recursively copy entire directories. For more in-depth documentation on how to copy files using the Linux `cp` command, see the [cp man page](https://pubs.opengroup.org/onlinepubs/9699919799/utilities/cp.html). ## More Information You may wish to consult the following resources for additional information on this topic. While these are provided in the hope that they will be useful, please note that we cannot vouch for the accuracy or timeliness of externally hosted materials. - [man page for cp command](https://pubs.opengroup.org/onlinepubs/9699919799/utilities/cp.html) - [Ubuntu 22.04 documentation for the cp command](https://manpages.ubuntu.com/manpages/jammy/man1/cp.1.html) This page was originally published on July 18, 2022.
Shard95 (laksa)
Root Hash1749289081436496295
Unparsed URLcom,linode!www,/docs/guides/how-to-copy-files-and-directories-in-linux/ s443