ℹ️ 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 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://phoenixnap.com/kb/how-to-copy-files-directories-linux |
| Last Crawled | 2026-04-06 08:48:27 (21 hours ago) |
| First Indexed | 2019-05-17 20:31:09 (6 years ago) |
| HTTP Status Code | 200 |
| Meta Title | How to Copy Files and Directories in Linux (With Examples) |
| Meta Description | Find out how to copy files and directories from the Linux command line. A straightforward guide with simple command examples. |
| Meta Canonical | null |
| Boilerpipe Text | Home
»
KB
»
SysAdmin
»
How to Copy Files and Directories in Linux
Topics:
Copying
files
and
directories
in
Linux
is an essential administrative task. Copies are instrumental for backing up important files or transferring them to another location, for example, a
data storage
device.
The
cp command
is useful for basic copying needs, while
rsync
excels at complex tasks, such as selectively copying specific files or creating system-wide backups.
Learn how to copy files and directories in Linux using the
cp
and
rsync
commands.
Prerequisites
A Linux system.
Access to a
command line
/terminal window.
How to Copy Files in Linux Using cp Command
The
cp
command is the primary method for copying files and directories within a local Linux
filesystem
. The basic command syntax is:
cp [option] [source] [destination]
The
source
is the file or directory you want to copy, while the
destination
represents the location where the copy should be placed. The
destination
can be a directory path or a new filename if the file needs to be
renamed
.
Use the listed
cp
command options below to specify different behaviors:
OPTION
DESCRIPTION
-v
Displays additional information about the copying process.
-p
Forces the system to preserve source file attributes (modification time, access time, user ID (UID), group ID (GID), file flags, file mode,
access control lists (ACLs)
, and extended attributes (EAs)).
-f
If the destination file/directory exists, replaces it with the source.
-i
Prompts for confirmation before overwriting a file.
-r
Copies all files in a directory.
-u
Replaces files only if they satisfy the update condition. For example, when the destination file does not exist or when the source file is newer than the destination file.
How to Copy a Single File
Enter the following command to copy a single file within the current working directory:
cp my_file.txt my_file2.txt
The system does not provide an output. Use the
ls command
to confirm that the file has been copied.
By default, the
cp
command runs in the same directory you are working in. However, the same file cannot exist twice in the same directory.
You must rename the target file to copy it to the same location. You can add
_old
to the name, or add a number or change the extension (e.g.,
.bak
instead of
.txt
).
Note:
The
cp
command in Linux does not provide a warning before overwriting existing files. Use the
–i
option for the system to prompt you before overwiting a file.
Enter the following command to copy a file from the current working directory to a different directory:
cp -v my_file.txt path/to/destination
Replace
path/to/destination
with the actual directory path. The
-v
option displays additional information about the copying process.
The command fails if the destination directory does not exist becauseÂ
cp
 does not create directories.
Note:
Find out how to create directories in Linux with the
mkdir command
.
Use the following command to copy a single file to a different directory and rename it:
cp my_file.txt path/to/destination/my_file2.txt
To copy a file without having to change directories, specify a path for the source file and the destination directory:
cp -v ~/Documents/my_file.txt path/to/destination
Replace the example path in the command with the actual path on your system.
In this example, the
-v
option was used to verify that the file was copied successfully.
How to Copy Multiple Files
To copy more than one file at a time, list each file to be copied before entering the destination directory:
cp my_file.txt my_file2.txt my_file3.txt path/to/destination
The system creates a copy of all three files in the destination folder.
The wildcard (
*
) character allows users to specify files that share a string of characters. The following command finds all the files with the
.jpg
extension in the
pictures
directory and copies them into the
destination
folder:
cp ~/pictures/*.jpg path/to/destination
If one of the files or directories requires
root
or higher-level permissions, use the
sudo command
to elevate privileges temporarily.
How to Copy Directories in Linux Using cp Command
To
copy an
entire directory and its
subdirectories
and files
, use the
–r
option. The following command copies all files and directories from the
Documents
directory to the
new_destination
directory:
cp -r ~/Documents path/to/new_destination
The
-r
option stands for recursive, which means "everything in that location."
Copying Files Using rsync Command
The
rsync command
in Linux synchronizes or transfers data between two locations. Usage is similar to the
cp
command, but while
cp
only copies files locally. To transfer files and directories to remote systems, you can use
rsync over SSH
. The tool saves
bandwidth
and transfers files faster by
compressing
them and only transferring new or updated files.
In addition,
rsync
creates a new directory if the final part of the destination path specifies a non-existent location.
How to Copy a Single File Using rsync
To copy one file to another directory on a local machine, type the source file's full path, followed by the destination directory:
rsync -av ~/Documents/my_file.txt ~/destination/backups/
Replace the source path and
/destination/backups/
destination from the example with your system's actual path and destination directory.
The
-a
option instructs
rsync
to preserve file
metadata
and other attributes such as
permissions
and timestamps. The
-v
option provides detailed output about the file transfer process.
How to Copy a Directory Using rsync
In this example,
rsync
is used to copy the contents of the
/home/phoenixnap/anaconda3/doc/config/
directory to
/home/phoenixnap/backups/anaconda3/
:
rsync -av /home/phoenixnap/anaconda3/doc/config/ /home/phoenixnap/backups/anaconda/
The trailing slash (
/
) at the end of the source and destination paths ensures you only copy the content of the source directory. Omitting the slash on the source directory creates a new subdirectory within the destination directory.
To prevent specific files from being copied, check out our guide on how to use
rsync to exclude files and directories
from a data transfer.
Conclusion
You now know how to copy files in a local Linux filesystem. The
cp
and
rsync
commands are versatile and powerful tools for managing and backing up files, especially when combined with other
essential Linux commands
.
Was this article helpful?
Yes
No |
| Markdown | - [Call](https://phoenixnap.com/kb/how-to-copy-files-directories-linux)
- [Support](tel:1-855-330-1509)
- [Sales](tel:1-877-588-5918)
- [Login](https://phoenixnap.com/kb/how-to-copy-files-directories-linux)
- [Bare Metal Cloud](https://bmc.phoenixnap.com/)
- [Admin Hub](https://admin.phoenixnap.com/)
- [Partners](https://phoenixnap.com/partners)
- [COLOCATION](https://phoenixnap.com/colocation)
- [Colocation Premier Carrier Hotel](https://phoenixnap.com/colocation)
- [Data Center as a Service Solutions for Digital Transformation](https://phoenixnap.com/colocation/data-center-as-a-service)
- [Data Center as a Service Overview](https://phoenixnap.com/colocation/data-center-as-a-service)
- [Hardware as a Service Flexible Hardware Leasing](https://phoenixnap.com/colocation/hardware-as-a-service)
- [Bare Metal Cloud API-Driven Dedicated Servers](https://phoenixnap.com/bare-metal-cloud)
- [Object Storage S3 API Compatible Storage Service](https://phoenixnap.com/object-storage)
- [Meet-Me Room The Interconnectivity Hub](https://phoenixnap.com/colocation/meet-me-room)
- [Meet-Me Room Overview](https://phoenixnap.com/colocation/meet-me-room)
- [AWS Direct Connect Dedicated Link to Amazon Cloud](https://phoenixnap.com/colocation/aws-direct-connect)
- [Google Cloud Interconnect Private Connectivity to Google Cloud](https://phoenixnap.com/google-cloud-interconnect)
- [Megaport Cloud Router Simplified Multi-Cloud Connections](https://phoenixnap.com/offers/megaport-cloud-router)
- [All Carriers Global Interconnectivity Options](https://phoenixnap.com/colocation/all-carriers)
- [Schedule a Tour Guided Virtual Data Center Tour](https://phoenixnap.com/phoenix-data-center-virtual-tour)
- [Data Center Locations Global Data Center Footprint](https://phoenixnap.com/colocation/data-center-locations)
- [Data Center Locations Overview](https://phoenixnap.com/colocation/data-center-locations)
- [Phoenix, AZ The Largest Fiber Backbone in the U.S.](https://phoenixnap.com/colocation/phoenix)
- [Amsterdam, NL The Connectivity Hub of Europe](https://phoenixnap.com/colocation/amsterdam)
- [BARE METAL CLOUD](https://phoenixnap.com/bare-metal-cloud)
- [Platform API-Driven Dedicated Servers](https://phoenixnap.com/bare-metal-cloud)
- [Platform Overview](https://phoenixnap.com/bare-metal-cloud)
- [Infrastructure As Code DevOps Integrations](https://phoenixnap.com/bare-metal-cloud/infrastructure-as-code)
- [BMC vs. Dedicated Servers Choose the Best Option](https://phoenixnap.com/bare-metal-cloud-vs-dedicated-servers)
- [Pricing Instance pricing and cost estimation](https://phoenixnap.com/kb/how-to-copy-files-directories-linux)
- [Instance Pricing See All Configurations](https://phoenixnap.com/bare-metal-cloud/instances)
- [Pricing Calculator Get an Estimate](https://phoenixnap.com/cloud-pricing-calculator)
- [Network/IP Pricing Flexible IP Pricing](https://phoenixnap.com/bare-metal-cloud/ip-pricing)
- [Kubernetes Solutions Streamlined Kubernetes Management](https://phoenixnap.com/kb/how-to-copy-files-directories-linux)
- [Rancher Deployment One-Click Kubernetes Deployment](https://phoenixnap.com/bare-metal-cloud/rancher-deployment)
- [CPUs Next Gen Intel Processors](https://phoenixnap.com/kb/how-to-copy-files-directories-linux)
- [Xeon 6 6700-series with E-Cores Supercharge Cloud-Native Workloads](https://phoenixnap.com/bare-metal-cloud/intel-xeon-6-processors-with-e-cores)
- [Intel® Core™ i9 14900K Ideal for Gaming, Streaming, and Content Creation](https://phoenixnap.com/bare-metal-cloud/intel-core-i9-14900k)
- [5th Gen Intel Xeon Scalable CPUs Boost Data-Intensive Workloads](https://phoenixnap.com/bare-metal-cloud/5th-gen-intel-xeon-scalable-processors)
- [HPE Ampere Altra Q80-30 HPE® ProLiant RL300 as a service](https://phoenixnap.com/bare-metal-cloud/hpe-proliant-rl300)
- [Alliances Technology Partnerships](https://phoenixnap.com/bare-metal-cloud/technology-ecosystem)
- [Ecosystem Underlying Technologies](https://phoenixnap.com/bare-metal-cloud/technology-ecosystem)
- [NetrisOS VPC networking on bare metal](https://phoenixnap.com/bare-metal-cloud/netris)
- [Storage Options Flexible Storage Solutions](https://phoenixnap.com/kb/how-to-copy-files-directories-linux)
- [Object Storage S3-Compatible Storage Solution](https://phoenixnap.com/object-storage)
- [Network File Storage All-Flash Scale-Out Storage Resources](https://phoenixnap.com/network-file-storage)
- [GPU Servers For AI/ML and HPC workloads](https://phoenixnap.com/kb/how-to-copy-files-directories-linux)
- [Intel Max 1100 GPUs 1 system. 2 GPUs. Amazing performance.](https://phoenixnap.com/bare-metal-cloud/gpu-servers)
- [HYBRID CLOUD](https://phoenixnap.com/security/data-security-cloud)
- [Hybrid Cloud Overview](https://phoenixnap.com/cloud-services/hybrid-cloud-solutions)
- [Object Storage S3 Compatible Storage Solution](https://phoenixnap.com/object-storage)
- [Bare Metal Cloud API-Driven Dedicated Servers](https://phoenixnap.com/bare-metal-cloud)
- [Alternative Cloud Provider Overcome Public Cloud Limitations](https://phoenixnap.com/cloud-services/alternative-cloud-provider)
- [Backup Solutions Veeam-Powered Services](https://phoenixnap.com/backup-restore)
- [Disaster Recovery VMware, Veeam, Zerto](https://phoenixnap.com/disaster-recovery-as-a-service-draas)
- [Veeam Cloud Connect Backup and Replication](https://phoenixnap.com/backup-restore/veeam-cloud-connect)
- [Managed Backup for Microsoft 365 Veeam-Powered Service](https://phoenixnap.com/backup-restore/microsoft-365-backup)
- [NETWORK](https://phoenixnap.com/network)
- [Network Overview Global Network Footprint](https://phoenixnap.com/network)
- [Network Locations U.S., Europe, APAC, LATAM](https://phoenixnap.com/network/locations)
- [Speed Test Download Speed Test](https://phoenixnap.com/network/speed-test)
- [LEARN](https://phoenixnap.com/kb)
- [Blog IT Tips and Tricks](https://phoenixnap.com/blog/)
- [Resource Library Knowledge Resources](https://phoenixnap.com/company/resource-library)
- [Glossary IT Terms and Definitions](https://phoenixnap.com/glossary/)
- [Events Let's Meet\!](https://phoenixnap.com/company/it-events)
- [Newsroom Media Library](https://phoenixnap.com/company/press)
- [Developers Development Resources Portal](https://developers.phoenixnap.com/)
- [APIs Access Our Public APIs](https://developers.phoenixnap.com/apis)
- [GitHub Public Code Repositories](https://github.com/phoenixnap)
[Home](https://phoenixnap.com/) » [KB](https://phoenixnap.com/kb/) » [SysAdmin](https://phoenixnap.com/kb/category/sysadmin) » How to Copy Files and Directories in Linux
# How to Copy Files and Directories in Linux


By
[Vladimir Kaplarevic](https://phoenixnap.com/kb/author/vladimirk)
Published:
December 19, 2025
Topics:
[commands](https://phoenixnap.com/kb/tag/commands), [linux](https://phoenixnap.com/kb/tag/linux)
Copying [files](https://phoenixnap.com/glossary/what-is-a-file) and [directories](https://phoenixnap.com/glossary/what-is-a-directory) in [Linux](https://phoenixnap.com/kb/what-is-linux) is an essential administrative task. Copies are instrumental for backing up important files or transferring them to another location, for example, a [data storage](https://phoenixnap.com/kb/what-is-data-storage) device.
The [cp command](https://phoenixnap.com/kb/cp-command) is useful for basic copying needs, while [rsync](https://phoenixnap.com/kb/rsync-command-linux-examples) excels at complex tasks, such as selectively copying specific files or creating system-wide backups.
Learn how to copy files and directories in Linux using the **`cp`** and **`rsync`** commands.


Prerequisites
- A Linux system.
- Access to a [command line](https://phoenixnap.com/glossary/command-line-interface-cli)/terminal window.
## How to Copy Files in Linux Using cp Command
The **`cp`** command is the primary method for copying files and directories within a local Linux [filesystem](https://phoenixnap.com/glossary/filesystem). The basic command syntax is:
```
cp [option] [source] [destination]
```
The `source` is the file or directory you want to copy, while the **`destination`** represents the location where the copy should be placed. The `destination` can be a directory path or a new filename if the file needs to be [renamed](https://phoenixnap.com/kb/rename-file-linux).
Use the listed **`cp`** command options below to specify different behaviors:
| OPTION | DESCRIPTION |
|---|---|
| **\-v** | Displays additional information about the copying process. |
| **\-p** | Forces the system to preserve source file attributes (modification time, access time, user ID (UID), group ID (GID), file flags, file mode, [access control lists (ACLs)](https://phoenixnap.com/kb/acl-network), and extended attributes (EAs)). |
| **\-f** | If the destination file/directory exists, replaces it with the source. |
| **\-i** | Prompts for confirmation before overwriting a file. |
| **\-r** | Copies all files in a directory. |
| ****\-u**** | Replaces files only if they satisfy the update condition. For example, when the destination file does not exist or when the source file is newer than the destination file. |
### How to Copy a Single File
Enter the following command to copy a single file within the current working directory:
```
cp my_file.txt my_file2.txt
```
The system does not provide an output. Use the [ls command](https://phoenixnap.com/kb/linux-ls-commands) to confirm that the file has been copied.


By default, the **`cp`** command runs in the same directory you are working in. However, the same file cannot exist twice in the same directory.
**Note:** Learn how to [check or change the working directory](https://phoenixnap.com/kb/python-change-directory) using the [cd command](https://phoenixnap.com/kb/linux-cd-command).
You must rename the target file to copy it to the same location. You can add *\_old* to the name, or add a number or change the extension (e.g., **.bak** instead of **.txt**).
**Note:** The **`cp`** command in Linux does not provide a warning before overwriting existing files. Use the **`–i`** option for the system to prompt you before overwiting a file.
Enter the following command to copy a file from the current working directory to a different directory:
```
cp -v my_file.txt path/to/destination
```
Replace *path/to/destination* with the actual directory path. The **`-v`** option displays additional information about the copying process.


The command fails if the destination directory does not exist because **cp** does not create directories.


**Note:** Find out how to create directories in Linux with the [mkdir command](https://phoenixnap.com/kb/create-directory-linux-mkdir-command).
Use the following command to copy a single file to a different directory and rename it:
```
cp my_file.txt path/to/destination/my_file2.txt
```
To copy a file without having to change directories, specify a path for the source file and the destination directory:
```
cp -v ~/Documents/my_file.txt path/to/destination
```
Replace the example path in the command with the actual path on your system.


In this example, the **`-v`** option was used to verify that the file was copied successfully.
### How to Copy Multiple Files
To copy more than one file at a time, list each file to be copied before entering the destination directory:
```
cp my_file.txt my_file2.txt my_file3.txt path/to/destination
```
The system creates a copy of all three files in the destination folder.
The wildcard (**`*`**) character allows users to specify files that share a string of characters. The following command finds all the files with the *.jpg* extension in the **`pictures`** directory and copies them into the **`destination`** folder:
```
cp ~/pictures/*.jpg path/to/destination
```
If one of the files or directories requires [root](https://phoenixnap.com/glossary/what-is-root-access) or higher-level permissions, use the [sudo command](https://phoenixnap.com/kb/linux-sudo-command) to elevate privileges temporarily.
**Note:** Learn how to transfer files [between two Linux systems](https://phoenixnap.com/kb/linux-to-linux).
## How to Copy Directories in Linux Using cp Command
To **copy an** **entire directory and its** [subdirectories](https://phoenixnap.com/glossary/what-is-a-subdirectory) **and files**, use the **`–r`** option. The following command copies all files and directories from the **`Documents`** directory to the **`new_destination`** directory:
```
cp -r ~/Documents path/to/new_destination
```
The **`-r`** option stands for recursive, which means "everything in that location."
**Note:** As an alternative to copying directories, learn how to [move directories in Linux](https://phoenixnap.com/kb/move-directory-linux).
## Copying Files Using rsync Command
The [rsync command](https://phoenixnap.com/kb/rsync-back-up-data) in Linux synchronizes or transfers data between two locations. Usage is similar to the **`cp`** command, but while **`cp`** only copies files locally. To transfer files and directories to remote systems, you can use [rsync over SSH](https://phoenixnap.com/kb/how-to-rsync-over-ssh). The tool saves [bandwidth](https://phoenixnap.com/glossary/what-is-bandwidth) and transfers files faster by [compressing](https://phoenixnap.com/glossary/file-compression) them and only transferring new or updated files.
In addition, **`rsync`** creates a new directory if the final part of the destination path specifies a non-existent location.
### How to Copy a Single File Using rsync
To copy one file to another directory on a local machine, type the source file's full path, followed by the destination directory:
```
rsync -av ~/Documents/my_file.txt ~/destination/backups/
```
Replace the source path and */destination/backups/* destination from the example with your system's actual path and destination directory.


The **`-a`** option instructs **`rsync`** to preserve file [metadata](https://phoenixnap.com/glossary/metadata-definition) and other attributes such as [permissions](https://phoenixnap.com/kb/linux-file-permissions) and timestamps. The **`-v`** option provides detailed output about the file transfer process.
### How to Copy a Directory Using rsync
In this example, **`rsync`** is used to copy the contents of the */home/phoenixnap/anaconda3/doc/config/* directory to */home/phoenixnap/backups/anaconda3/*:
```
rsync -av /home/phoenixnap/anaconda3/doc/config/ /home/phoenixnap/backups/anaconda/
```
The trailing slash (**`/`**) at the end of the source and destination paths ensures you only copy the content of the source directory. Omitting the slash on the source directory creates a new subdirectory within the destination directory.


To prevent specific files from being copied, check out our guide on how to use [rsync to exclude files and directories](https://phoenixnap.com/kb/rsync-exclude-files-and-directories) from a data transfer.
Conclusion
You now know how to copy files in a local Linux filesystem. The **`cp`** and **`rsync`** commands are versatile and powerful tools for managing and backing up files, especially when combined with other [essential Linux commands](https://phoenixnap.com/kb/linux-commands).
Was this article helpful?
YesNo
Contents
- [How to Copy Files in Linux Using cp Command](https://phoenixnap.com/kb/how-to-copy-files-directories-linux#How_to_Copy_Files_in_Linux_Using_cp_Command)
- [How to Copy a Single File](https://phoenixnap.com/kb/how-to-copy-files-directories-linux#How_to_Copy_a_Single_File)
- [How to Copy Multiple Files](https://phoenixnap.com/kb/how-to-copy-files-directories-linux#How_to_Copy_Multiple_Files)
- [How to Copy Directories in Linux Using cp Command](https://phoenixnap.com/kb/how-to-copy-files-directories-linux#How_to_Copy_Directories_in_Linux_Using_cp_Command)
- [Copying Files Using rsync Command](https://phoenixnap.com/kb/how-to-copy-files-directories-linux#Copying_Files_Using_rsync_Command)
- [How to Copy a Single File Using rsync](https://phoenixnap.com/kb/how-to-copy-files-directories-linux#How_to_Copy_a_Single_File_Using_rsync)
- [How to Copy a Directory Using rsync](https://phoenixnap.com/kb/how-to-copy-files-directories-linux#How_to_Copy_a_Directory_Using_rsync)
Subscribe to our newsletter
[SUBSCRIBE](https://phoenixnap.com/developers-monthly-newsletter)
Next you should read
[](https://phoenixnap.com/kb/how-to-remove-files-directories-linux-command-line)
[SysAdmin](https://phoenixnap.com/kb/category/sysadmin)
[How to Remove (Delete) a File or Directory in Linux](https://phoenixnap.com/kb/how-to-remove-files-directories-linux-command-line)
[](https://phoenixnap.com/kb/how-to-create-a-file-in-linux)
[SysAdmin](https://phoenixnap.com/kb/category/sysadmin) [Web Servers](https://phoenixnap.com/kb/category/web-servers)
[How to Create a File in Linux Using Terminal/Command Line](https://phoenixnap.com/kb/how-to-create-a-file-in-linux)
[](https://phoenixnap.com/kb/grep-command-linux-unix-examples)
[SysAdmin](https://phoenixnap.com/kb/category/sysadmin)
[How To Use grep Command In Linux/UNIX](https://phoenixnap.com/kb/grep-command-linux-unix-examples)
[](https://phoenixnap.com/kb/linux-scp-command)
[SysAdmin](https://phoenixnap.com/kb/category/sysadmin)
[SCP Command in Linux {13 Examples}](https://phoenixnap.com/kb/linux-scp-command)
CONTACT US
[Get a Quote](https://phoenixnap.com/contact-us)
[Support (1-855-330-1509)](tel:1-855-330-1509)
[Sales (1-877-588-5918)](tel:1-877-588-5918)
- [Colocation](https://phoenixnap.com/colocation)
- [Phoenix](https://phoenixnap.com/colocation/phoenix)
- [Ashburn](https://phoenixnap.com/colocation/ashburn)
- [Amsterdam](https://phoenixnap.com/colocation/amsterdam)
- [Atlanta](https://phoenixnap.com/colocation/atlanta)
- [Belgrade](https://phoenixnap.com/colocation/belgrade)
- [Singapore](https://phoenixnap.com/colocation/singapore)
- [Shipping Instructions](https://phoenixnap.com/colocation/shipping-instructions)
RECENT POSTS
["Invalid Reference Format" Error in Docker: How to Fix](https://phoenixnap.com/kb/docker-invalid-reference-format)
[Python dict() Function Explained](https://phoenixnap.com/kb/python-dict)
[Ansible get\_url Module: Download Files from URL](https://phoenixnap.com/kb/ansible-get-url)
[Docker Desktop: Unexpected WSL Error](https://phoenixnap.com/kb/docker-desktop-unexpected-wsl-error)
[How to Check if a File Exists in Python](https://phoenixnap.com/kb/check-if-file-exist-python)
- [Servers](https://phoenixnap.com/servers/dedicated)
- [Bare Metal Cloud](https://phoenixnap.com/bare-metal-cloud)
- [Dedicated Servers](https://phoenixnap.com/servers/dedicated)
- [Database Servers](https://phoenixnap.com/servers/database)
- [Virtualization Servers](https://phoenixnap.com/servers/virtualization)
- [High Performance Computing (HPC) Servers](https://phoenixnap.com/servers/high-performance-computing-hpc)
- [Dedicated Streaming Servers](https://phoenixnap.com/servers/streaming)
- [Dedicated Game Servers](https://phoenixnap.com/servers/dedicated-game-servers)
- [Dedicated Storage Servers](https://phoenixnap.com/servers/storage)
- [SQL Server Hosting](https://phoenixnap.com/servers/sql-server-hosting)
- [Dedicated Servers in Amsterdam](https://phoenixnap.com/dedicated-servers-amsterdam-netherlands)
- [Cloud Servers in Europe](https://phoenixnap.com/cloud-services/cloud-servers-europe)
- [Big Memory Infrastructure](https://phoenixnap.com/bare-metal-cloud/big-memory-infrastructure)
- [Buy Now](https://admin.phoenixnap.com/wap-pncpadmin-shell/orderForm?bmbPath=/order-form?currencyCode=usd)
- [BMC portal](https://signup.bmc.phoenixnap.com/)
CLOUD SERVICES
- [Data Security Cloud](https://phoenixnap.com/security/data-security-cloud)
- [Managed Private Cloud](https://phoenixnap.com/private)
- [Object Storage](https://phoenixnap.com/object-storage)
- [Solutions](https://phoenixnap.com/infrastructure-solutions)
- [Disaster Recovery](https://phoenixnap.com/infrastructure-solutions/disaster-recovery-services)
- [Web Hosting Reseller](https://phoenixnap.com/reseller-hosting)
- [SaaS Hosting](https://phoenixnap.com/infrastructure-solutions/saas-hosting)
COMPLIANCE
- [HIPAA Ready Hosting](https://phoenixnap.com/compliance/hipaa-compliant-hosting)
- [PCI Compliant Hosting](https://phoenixnap.com/compliance/pci-compliant-hosting)
- [Privacy Center](https://phoenixnap.com/)
- [Do not sell or share my personal information](https://phoenixnap.com/)
NEEDS
- [Disaster Recovery Solutions](https://phoenixnap.com/infrastructure-solutions/disaster-recovery-services)
- [High Availability Solutions](https://phoenixnap.com/infrastructure-solutions/high-availability-solutions)
- [Cloud Evaluation](https://phoenixnap.com/offers/cloud-evaluation-demo)
INDUSTRIES
- [Web Hosting Providers](https://phoenixnap.com/reseller-hosting)
- [Legal](https://phoenixnap.com/infrastructure-solutions/legal-it)
- [MSPs & VARs](https://phoenixnap.com/infrastructure-solutions/it-partners)
- [Media Hosting](https://phoenixnap.com/infrastructure-solutions/media-hosting)
- [Online Gaming](https://phoenixnap.com/infrastructure-solutions/online-gaming)
- [SaaS Hosting Solutions](https://phoenixnap.com/infrastructure-solutions/saas-hosting)
- [Ecommerce Hosting Solutions](https://phoenixnap.com/infrastructure-solutions/ecommerce-hosting)
COMPANY
- [About phoenixNAP](https://phoenixnap.com/about)
- [IaaS Solutions](https://phoenixnap.com/company/iaas-provider)
- [Customer Experience](https://phoenixnap.com/company/customer-experience)
- [Platform](https://phoenixnap.com/cloud-services/platform)
- [Schedule Virtual Tour](https://phoenixnap.com/phoenix-data-center-virtual-tour)
- [Open Source Community](https://phoenixnap.com/company/open-source-community)
- [Resource Library](https://phoenixnap.com/company/resource-library)
- [Press](https://phoenixnap.com/company/press)
- [Events](https://phoenixnap.com/company/it-events)
- [Careers](https://techjobs.dev/jobs/)
- [Promotions](https://phoenixnap.com/promotions)
- [Contact Us](https://phoenixnap.com/contact-us)
- [Legal](https://phoenixnap.com/cs/legal/)
- [Privacy Policy](https://phoenixnap.com/infrastructure-solutions/legal-it/eu-u-s-data-privacy-framework-program-eu-u-s-dpf-compliant-privacy-policy)
- [Terms of Use](https://phoenixnap.com/cs/legal/aup.html)
- [DMCA](https://phoenixnap.com/cs/legal/dmca.html)
- [GDPR](https://phoenixnap.com/gdpr)
- [Sitemap](https://phoenixnap.com/sitemap)
- [Blog](https://phoenixnap.com/blog)
- [Resources](https://phoenixnap.com/company/resource-library)
- [Knowledge Base](https://phoenixnap.com/kb)
- [IT Glossary](https://phoenixnap.com/glossary)
- [GitHub](https://github.com/phoenixnap)
- [RFP Template](https://phoenixnap.com/company/it-resources/data-center-rfp-template)
© 2025 phoenixNAP \| Global IT Services. All Rights Reserved. |
| Readable Markdown | [Home](https://phoenixnap.com/) » [KB](https://phoenixnap.com/kb/) » [SysAdmin](https://phoenixnap.com/kb/category/sysadmin) » How to Copy Files and Directories in Linux

Topics:
Copying [files](https://phoenixnap.com/glossary/what-is-a-file) and [directories](https://phoenixnap.com/glossary/what-is-a-directory) in [Linux](https://phoenixnap.com/kb/what-is-linux) is an essential administrative task. Copies are instrumental for backing up important files or transferring them to another location, for example, a [data storage](https://phoenixnap.com/kb/what-is-data-storage) device. The [cp command](https://phoenixnap.com/kb/cp-command) is useful for basic copying needs, while [rsync](https://phoenixnap.com/kb/rsync-command-linux-examples) excels at complex tasks, such as selectively copying specific files or creating system-wide backups. Learn how to copy files and directories in Linux using the **`cp`** and **`rsync`** commands.  Prerequisites A Linux system. Access to a [command line](https://phoenixnap.com/glossary/command-line-interface-cli)/terminal window. How to Copy Files in Linux Using cp Command The **`cp`** command is the primary method for copying files and directories within a local Linux [filesystem](https://phoenixnap.com/glossary/filesystem). The basic command syntax is: The `source` is the file or directory you want to copy, while the **`destination`** represents the location where the copy should be placed. The `destination` can be a directory path or a new filename if the file needs to be [renamed](https://phoenixnap.com/kb/rename-file-linux). Use the listed **`cp`** command options below to specify different behaviors: OPTION DESCRIPTION **\-v** Displays additional information about the copying process. **\-p** Forces the system to preserve source file attributes (modification time, access time, user ID (UID), group ID (GID), file flags, file mode, [access control lists (ACLs)](https://phoenixnap.com/kb/acl-network), and extended attributes (EAs)). **\-f** If the destination file/directory exists, replaces it with the source. **\-i** Prompts for confirmation before overwriting a file. **\-r** Copies all files in a directory. ****\-u**** Replaces files only if they satisfy the update condition. For example, when the destination file does not exist or when the source file is newer than the destination file. How to Copy a Single File Enter the following command to copy a single file within the current working directory: The system does not provide an output. Use the [ls command](https://phoenixnap.com/kb/linux-ls-commands) to confirm that the file has been copied.  By default, the **`cp`** command runs in the same directory you are working in. However, the same file cannot exist twice in the same directory. You must rename the target file to copy it to the same location. You can add *\_old* to the name, or add a number or change the extension (e.g., **.bak** instead of **.txt**). **Note:** The **`cp`** command in Linux does not provide a warning before overwriting existing files. Use the **`–i`** option for the system to prompt you before overwiting a file. Enter the following command to copy a file from the current working directory to a different directory: Replace *path/to/destination* with the actual directory path. The **`-v`** option displays additional information about the copying process.  The command fails if the destination directory does not exist because **cp** does not create directories.  **Note:** Find out how to create directories in Linux with the [mkdir command](https://phoenixnap.com/kb/create-directory-linux-mkdir-command). Use the following command to copy a single file to a different directory and rename it: To copy a file without having to change directories, specify a path for the source file and the destination directory: Replace the example path in the command with the actual path on your system.  In this example, the **`-v`** option was used to verify that the file was copied successfully. How to Copy Multiple Files To copy more than one file at a time, list each file to be copied before entering the destination directory: The system creates a copy of all three files in the destination folder. The wildcard (**`*`**) character allows users to specify files that share a string of characters. The following command finds all the files with the *.jpg* extension in the **`pictures`** directory and copies them into the **`destination`** folder: If one of the files or directories requires [root](https://phoenixnap.com/glossary/what-is-root-access) or higher-level permissions, use the [sudo command](https://phoenixnap.com/kb/linux-sudo-command) to elevate privileges temporarily. How to Copy Directories in Linux Using cp Command To **copy an** **entire directory and its** [subdirectories](https://phoenixnap.com/glossary/what-is-a-subdirectory) **and files**, use the **`–r`** option. The following command copies all files and directories from the **`Documents`** directory to the **`new_destination`** directory: The **`-r`** option stands for recursive, which means "everything in that location." Copying Files Using rsync Command The [rsync command](https://phoenixnap.com/kb/rsync-back-up-data) in Linux synchronizes or transfers data between two locations. Usage is similar to the **`cp`** command, but while **`cp`** only copies files locally. To transfer files and directories to remote systems, you can use [rsync over SSH](https://phoenixnap.com/kb/how-to-rsync-over-ssh). The tool saves [bandwidth](https://phoenixnap.com/glossary/what-is-bandwidth) and transfers files faster by [compressing](https://phoenixnap.com/glossary/file-compression) them and only transferring new or updated files. In addition, **`rsync`** creates a new directory if the final part of the destination path specifies a non-existent location. How to Copy a Single File Using rsync To copy one file to another directory on a local machine, type the source file's full path, followed by the destination directory: Replace the source path and */destination/backups/* destination from the example with your system's actual path and destination directory.  The **`-a`** option instructs **`rsync`** to preserve file [metadata](https://phoenixnap.com/glossary/metadata-definition) and other attributes such as [permissions](https://phoenixnap.com/kb/linux-file-permissions) and timestamps. The **`-v`** option provides detailed output about the file transfer process. How to Copy a Directory Using rsync In this example, **`rsync`** is used to copy the contents of the */home/phoenixnap/anaconda3/doc/config/* directory to */home/phoenixnap/backups/anaconda3/*: The trailing slash (**`/`**) at the end of the source and destination paths ensures you only copy the content of the source directory. Omitting the slash on the source directory creates a new subdirectory within the destination directory.  To prevent specific files from being copied, check out our guide on how to use [rsync to exclude files and directories](https://phoenixnap.com/kb/rsync-exclude-files-and-directories) from a data transfer. Conclusion You now know how to copy files in a local Linux filesystem. The **`cp`** and **`rsync`** commands are versatile and powerful tools for managing and backing up files, especially when combined with other [essential Linux commands](https://phoenixnap.com/kb/linux-commands). Was this article helpful? YesNo |
| Shard | 39 (laksa) |
| Root Hash | 8557101678125738839 |
| Unparsed URL | com,phoenixnap!/kb/how-to-copy-files-directories-linux s443 |