âšī¸ Skipped - page is already crawled
| Filter | Status | Condition | Details |
|---|---|---|---|
| HTTP status | PASS | download_http_code = 200 | HTTP 200 |
| Age cutoff | PASS | download_stamp > now() - 6 MONTH | 0.1 months ago |
| History drop | PASS | isNull(history_drop_reason) | No drop reason |
| Spam/ban | PASS | fh_dont_index != 1 AND ml_spam_score = 0 | ml_spam_score=0 |
| Canonical | PASS | meta_canonical IS NULL OR = '' OR = src_unparsed | Not set |
| Property | Value |
|---|---|
| URL | https://www.freecodecamp.org/news/the-linux-cp-command-how-to-copy-files-in-linux/ |
| Last Crawled | 2026-04-06 14:31:20 (2 days ago) |
| First Indexed | 2022-06-06 18:50:25 (3 years ago) |
| HTTP Status Code | 200 |
| Meta Title | The Linux cp Command â How to Copy Files in Linux |
| Meta Description | By Dillion Megida There are a couple different ways to copy and paste content when you're working on your computer. If you spend more time in the user interface of your device, you'll probably use your mouse to do this. You can copy files by right-cl... |
| Meta Canonical | null |
| Boilerpipe Text | By Dillion Megida
There are a couple different ways to copy and paste content when you're working on your computer.
If you spend more time in the user interface of your device, you'll probably use your mouse to do this. You can copy files by right-clicking on the file and selecting "Copy", then going to a different directory and selecting "Paste".
For my terminal friends, you can also perform file copy-paste operations without leaving the terminal. In a Linux-based terminal, you do this using the
cp
command.
In this article, I'll explain what the
cp
command is and show you how to copy and paste files and directories in Linux using the terminal.
What is the
cp
command?
You use the
cp
command for copying files from one location to another. This command can also copy directories (folders).
The syntax of this command is:
cp
[
..
.file/directory-sources
]
[
destination
]
[file/directory-sources]
specifies the sources of the files or directories you want to copy. And the
[destination]
argument specifies the location you want to copy the file to.
To understand the rest of this article, I will use this folder structure example. Let's say a directory called
DirectoryA
has two directories in it:
DirectoryA_1
and
DirectoryA_2
. These subdirectories have many files and sub directories in them.
I'll also assume you're currently in the
DirectoryA
location in the terminal, so if you aren't, make sure you are:
cd
DirectoryA
If you want to copy a file, say
README.txt
from
DirectoryA_1
to
DirectoryA_2
, you will use the
cp
command like this:
cp
./DirectoryA_1/README.txt ./DirectoryA_2
# ./DirectoryA_1/README.txt is the source file
# ./DirectoryA_2 is the destination
If you want to copy more than a file from
DirectoryA_1
to
DirectoryA_2
, you will use the
cp
command like this:
cp
./DirectoryA_1/README.txt ./DirectoryA_1/ANOTHER_FILE.txt ./DirectoryA_2
As you can see, you will put all the source files first, and the last argument will be the destination.
How to copy directories with the
cp
command
By default, the
cp
command works with files. So if you attempt to copy a directory like this:
cp
./DirectoryA_1/Folder/ ./DirectoryA_2
You will get an error stating:
./DirectoryA_1/Folder/ is a directory
To copy directories, you have to pass the
-r
flag. This flag informs the
cp
command to recursively copy a directory and its contents (which could be files or other sub directories). So for the previous command, you can add the flag before the directory sources like this:
cp
-r
./DirectoryA_1/Folder/ ./DirectoryA_2
This command will recursively copy the
Folder
directory in
./DirectoryA_1/
as well as all files and directories in the
Folder
directory.
How to copy files that match a glob pattern
A glob pattern is similar to Regex, which allows you to match multiple files with names that match a specific pattern. Learn more about the difference here:
Regex vs Glob patterns
.
For example, if you want to copy all files in
DirectoryA_1
with the
.txt
extension, you can execute this command:
cp
./DirectoryA_1/*.txt ./DirectoryA_2
./DirectoryA_1/*.txt
matches files with the
.txt
extension in their names, and the
cp
command can copy all those files to the destination.
You can check out the
glob documentation
to learn more about globbing patterns and characters you can use.
Now you know how to copy files (and directories) right from the command line. Thanks for reading!
Learn to code for free. freeCodeCamp's open source curriculum has helped more than 40,000 people get jobs as developers.
Get started |
| Markdown | [](https://www.freecodecamp.org/news/)
Menu Menu
- [Forum](https://forum.freecodecamp.org/)
- [Curriculum](https://www.freecodecamp.org/learn)
[Donate](https://www.freecodecamp.org/donate/)
[Learn to code â free 3,000-hour curriculum](https://www.freecodecamp.org/)
June 6, 2022
/ [\#Linux](https://www.freecodecamp.org/news/tag/linux/)
# The Linux cp Command â How to Copy Files in Linux

By Dillion Megida
There are a couple different ways to copy and paste content when you're working on your computer.
If you spend more time in the user interface of your device, you'll probably use your mouse to do this. You can copy files by right-clicking on the file and selecting "Copy", then going to a different directory and selecting "Paste".
For my terminal friends, you can also perform file copy-paste operations without leaving the terminal. In a Linux-based terminal, you do this using the `cp` command.
In this article, I'll explain what the `cp` command is and show you how to copy and paste files and directories in Linux using the terminal.
## What is the `cp` command?
You use the `cp` command for copying files from one location to another. This command can also copy directories (folders).
The syntax of this command is:
```
cp [...file/directory-sources] [destination]
```
`[file/directory-sources]` specifies the sources of the files or directories you want to copy. And the `[destination]` argument specifies the location you want to copy the file to.
To understand the rest of this article, I will use this folder structure example. Let's say a directory called **DirectoryA** has two directories in it: **DirectoryA\_1** and **DirectoryA\_2**. These subdirectories have many files and sub directories in them.
I'll also assume you're currently in the **DirectoryA** location in the terminal, so if you aren't, make sure you are:
```
cd DirectoryA
```
## How to copy files with the `cp` command
If you want to copy a file, say **README.txt** from **DirectoryA\_1** to **DirectoryA\_2**, you will use the `cp` command like this:
```
cp ./DirectoryA_1/README.txt ./DirectoryA_2
# ./DirectoryA_1/README.txt is the source file
# ./DirectoryA_2 is the destination
```
If you want to copy more than a file from **DirectoryA\_1** to **DirectoryA\_2**, you will use the `cp` command like this:
```
cp ./DirectoryA_1/README.txt ./DirectoryA_1/ANOTHER_FILE.txt ./DirectoryA_2
```
As you can see, you will put all the source files first, and the last argument will be the destination.
## How to copy directories with the `cp` command
By default, the `cp` command works with files. So if you attempt to copy a directory like this:
```
cp ./DirectoryA_1/Folder/ ./DirectoryA_2
```
You will get an error stating:
**./DirectoryA\_1/Folder/ is a directory**
To copy directories, you have to pass the `-r` flag. This flag informs the `cp` command to recursively copy a directory and its contents (which could be files or other sub directories). So for the previous command, you can add the flag before the directory sources like this:
```
cp -r ./DirectoryA_1/Folder/ ./DirectoryA_2
```
This command will recursively copy the **Folder** directory in **./DirectoryA\_1/** as well as all files and directories in the **Folder** directory.
## How to copy files that match a glob pattern
A glob pattern is similar to Regex, which allows you to match multiple files with names that match a specific pattern. Learn more about the difference here: [Regex vs Glob patterns](https://dillionmegida.com/p/regex-vs-glob-patterns/).
For example, if you want to copy all files in **DirectoryA\_1** with the **.txt** extension, you can execute this command:
```
cp ./DirectoryA_1/*.txt ./DirectoryA_2
```
`./DirectoryA_1/*.txt` matches files with the `.txt` extension in their names, and the `cp` command can copy all those files to the destination.
You can check out the [glob documentation](https://linux.die.net/man/7/glob) to learn more about globbing patterns and characters you can use.
Now you know how to copy files (and directories) right from the command line. Thanks for reading\!
***
If you read this far, thank the author to show them you care. Say Thanks
Learn to code for free. freeCodeCamp's open source curriculum has helped more than 40,000 people get jobs as developers. [Get started](https://www.freecodecamp.org/learn)
ADVERTISEMENT
freeCodeCamp is a donor-supported tax-exempt 501(c)(3) charity organization (United States Federal Tax Identification Number: 82-0779546)
Our mission: to help people learn to code for free. We accomplish this by creating thousands of videos, articles, and interactive coding lessons - all freely available to the public.
Donations to freeCodeCamp go toward our education initiatives, and help pay for servers, services, and staff.
You can [make a tax-deductible donation here](https://www.freecodecamp.org/donate/).
## Trending Books and Handbooks
- [REST APIs](https://www.freecodecamp.org/news/build-consume-and-document-a-rest-api/)
- [Clean Code](https://www.freecodecamp.org/news/how-to-write-clean-code/)
- [TypeScript](https://www.freecodecamp.org/news/learn-typescript-with-react-handbook/)
- [JavaScript](https://www.freecodecamp.org/news/learn-javascript-for-beginners/)
- [AI Chatbots](https://www.freecodecamp.org/news/how-to-build-an-ai-chatbot-with-redis-python-and-gpt/)
- [Command Line](https://www.freecodecamp.org/news/command-line-for-beginners/)
- [GraphQL APIs](https://www.freecodecamp.org/news/building-consuming-and-documenting-a-graphql-api/)
- [CSS Transforms](https://www.freecodecamp.org/news/complete-guide-to-css-transform-functions-and-properties/)
- [Access Control](https://www.freecodecamp.org/news/how-to-build-scalable-access-control-for-your-web-app/)
- [REST API Design](https://www.freecodecamp.org/news/rest-api-design-best-practices-build-a-rest-api/)
- [PHP](https://www.freecodecamp.org/news/the-php-handbook/)
- [Java](https://www.freecodecamp.org/news/the-java-handbook/)
- [Linux](https://www.freecodecamp.org/news/learn-linux-for-beginners-book-basic-to-advanced/)
- [React](https://www.freecodecamp.org/news/react-for-beginners-handbook/)
- [CI/CD](https://www.freecodecamp.org/news/learn-continuous-integration-delivery-and-deployment/)
- [Docker](https://www.freecodecamp.org/news/the-docker-handbook/)
- [Golang](https://www.freecodecamp.org/news/learn-golang-handbook/)
- [Python](https://www.freecodecamp.org/news/the-python-handbook/)
- [Node.js](https://www.freecodecamp.org/news/get-started-with-nodejs/)
- [Todo APIs](https://www.freecodecamp.org/news/build-crud-operations-with-dotnet-core-handbook/)
- [JavaScript Classes](https://www.freecodecamp.org/news/how-to-use-classes-in-javascript-handbook/)
- [Front-End Libraries](https://www.freecodecamp.org/news/front-end-javascript-development-react-angular-vue-compared/)
- [Express and Node.js](https://www.freecodecamp.org/news/the-express-handbook/)
- [Python Code Examples](https://www.freecodecamp.org/news/python-code-examples-sample-script-coding-tutorial-for-beginners/)
- [Clustering in Python](https://www.freecodecamp.org/news/clustering-in-python-a-machine-learning-handbook/)
- [Software Architecture](https://www.freecodecamp.org/news/an-introduction-to-software-architecture-patterns/)
- [Programming Fundamentals](https://www.freecodecamp.org/news/what-is-programming-tutorial-for-beginners/)
- [Coding Career Preparation](https://www.freecodecamp.org/news/learn-to-code-book/)
- [Full-Stack Developer Guide](https://www.freecodecamp.org/news/become-a-full-stack-developer-and-get-a-job/)
- [Python for JavaScript Devs](https://www.freecodecamp.org/news/learn-python-for-javascript-developers-handbook/)
## Mobile App
- [](https://apps.apple.com/us/app/freecodecamp/id6446908151?itsct=apps_box_link&itscg=30200)
- [](https://play.google.com/store/apps/details?id=org.freecodecamp)
## Our Charity
[Publication powered by Hashnode](https://hashnode.com/) [About](https://www.freecodecamp.org/news/about/) [Alumni Network](https://www.linkedin.com/school/free-code-camp/people/) [Open Source](https://github.com/freeCodeCamp/) [Shop](https://www.freecodecamp.org/news/shop/) [Support](https://www.freecodecamp.org/news/support/) [Sponsors](https://www.freecodecamp.org/news/sponsors/) [Academic Honesty](https://www.freecodecamp.org/news/academic-honesty-policy/) [Code of Conduct](https://www.freecodecamp.org/news/code-of-conduct/) [Privacy Policy](https://www.freecodecamp.org/news/privacy-policy/) [Terms of Service](https://www.freecodecamp.org/news/terms-of-service/) [Copyright Policy](https://www.freecodecamp.org/news/copyright-policy/) |
| Readable Markdown | 
By Dillion Megida
There are a couple different ways to copy and paste content when you're working on your computer.
If you spend more time in the user interface of your device, you'll probably use your mouse to do this. You can copy files by right-clicking on the file and selecting "Copy", then going to a different directory and selecting "Paste".
For my terminal friends, you can also perform file copy-paste operations without leaving the terminal. In a Linux-based terminal, you do this using the `cp` command.
In this article, I'll explain what the `cp` command is and show you how to copy and paste files and directories in Linux using the terminal.
## What is the `cp` command?
You use the `cp` command for copying files from one location to another. This command can also copy directories (folders).
The syntax of this command is:
```
cp [...file/directory-sources] [destination]
```
`[file/directory-sources]` specifies the sources of the files or directories you want to copy. And the `[destination]` argument specifies the location you want to copy the file to.
To understand the rest of this article, I will use this folder structure example. Let's say a directory called **DirectoryA** has two directories in it: **DirectoryA\_1** and **DirectoryA\_2**. These subdirectories have many files and sub directories in them.
I'll also assume you're currently in the **DirectoryA** location in the terminal, so if you aren't, make sure you are:
```
cd DirectoryA
```
If you want to copy a file, say **README.txt** from **DirectoryA\_1** to **DirectoryA\_2**, you will use the `cp` command like this:
```
cp ./DirectoryA_1/README.txt ./DirectoryA_2
# ./DirectoryA_1/README.txt is the source file
# ./DirectoryA_2 is the destination
```
If you want to copy more than a file from **DirectoryA\_1** to **DirectoryA\_2**, you will use the `cp` command like this:
```
cp ./DirectoryA_1/README.txt ./DirectoryA_1/ANOTHER_FILE.txt ./DirectoryA_2
```
As you can see, you will put all the source files first, and the last argument will be the destination.
## How to copy directories with the `cp` command
By default, the `cp` command works with files. So if you attempt to copy a directory like this:
```
cp ./DirectoryA_1/Folder/ ./DirectoryA_2
```
You will get an error stating:
**./DirectoryA\_1/Folder/ is a directory**
To copy directories, you have to pass the `-r` flag. This flag informs the `cp` command to recursively copy a directory and its contents (which could be files or other sub directories). So for the previous command, you can add the flag before the directory sources like this:
```
cp -r ./DirectoryA_1/Folder/ ./DirectoryA_2
```
This command will recursively copy the **Folder** directory in **./DirectoryA\_1/** as well as all files and directories in the **Folder** directory.
## How to copy files that match a glob pattern
A glob pattern is similar to Regex, which allows you to match multiple files with names that match a specific pattern. Learn more about the difference here: [Regex vs Glob patterns](https://dillionmegida.com/p/regex-vs-glob-patterns/).
For example, if you want to copy all files in **DirectoryA\_1** with the **.txt** extension, you can execute this command:
```
cp ./DirectoryA_1/*.txt ./DirectoryA_2
```
`./DirectoryA_1/*.txt` matches files with the `.txt` extension in their names, and the `cp` command can copy all those files to the destination.
You can check out the [glob documentation](https://linux.die.net/man/7/glob) to learn more about globbing patterns and characters you can use.
Now you know how to copy files (and directories) right from the command line. Thanks for reading\!
***
Learn to code for free. freeCodeCamp's open source curriculum has helped more than 40,000 people get jobs as developers. [Get started](https://www.freecodecamp.org/learn) |
| Shard | 32 (laksa) |
| Root Hash | 13723046482134587832 |
| Unparsed URL | org,freecodecamp!www,/news/the-linux-cp-command-how-to-copy-files-in-linux/ s443 |