How to Copy Files and Directories Using Linux Terminal
cpWe can use the and commands
in Linux Terminal rsyncto copy files and directories. cpThe command is generally used to copy files, while rsyncthe command is generally used to copy directories.
Use cpthe command to copy files
We use command in Linux and Unix operating systems cpto copy files and directories.
To abc.txtcopy the contents of a file into another file backup.txt, we use the following command.
cp abc.txt backup.txt
It abc.txtcopies the contents of to backup.txt. Both files must be in the current working directory.
If the destination file already exists, the contents of the destination file will be overwritten.
We can cpadd -ithe -d flag to the command to get a confirmation prompt before copying.
cp -i abc.txt backup.txt
Output:
cp: overwrite 'backup.txt'?
If we want to copy a file, we press Ythe key and press Enter.
To understand cpwhat the command does, we can use the flag cpof the command -v.
cp -v abc.txt backup.txt
Output:
'abc.txt' -> 'backup.txt'
It shows that abc.txtthe contents of are being copied into backup.txt.
Use cpthe command to copy the file to a directory
To copy a file to a directory, we use cpthe command. cpThe first argument of the command is the name of the file to be copied, and the second argument is the absolute or relative path to the directory where the file to be copied is located.
cp abc.txt ./test
abc.txtIt copies
the files in the current working directory to testa folder in the current working directory.
If we want to copy a file under a specific directory to a different directory than the parent file, we can specify the name of the file as.
cp abc.txt ./test/test.txt
It abc.txtcopies the file to the folder testwith the name test.txt.
Copy multiple files
We can cpcopy multiple files and directories into a specific directory using the command, specifying all the source files and directories and the destination directory at the end.
cp abc.txt backup.txt test backup
This will copy the files abc.txtand backup.txtand the folder testinto the folder backup.
cpCommands can also perform pattern matching.
cp *.txt backup
.txtThis copies all files with
the extension in the current working directory into backupthe folder .
Use cpthe command to copy the directory
We use the -p -ror -R-p flag along with cpthe -p command to copy a directory along with its subdirectories and files to the destination directory.
cp -r pp Project
It ppcopies the entire directory and its subdirectories and files to the target directory Project.
In this example, Projectthere will be a ppdirectory within the directory.
However, if we only want to copy files and subdirectories, we use -Tthe -p flag and -Rthe -p flag.
cp -RT pp Project
It ppcopies subdirectories and files from the directory to the target directory Project.
Use rsyncthe command to copy files and directories
rsyncIs a command line tool for synchronizing files and directories between two hosts. If both the source and destination paths represent the local host, rsyncthe command is just like the copy command.
rsync -a abc.txt backup.txt
It abc.txtcopies the contents of into backup.txt.
Here, -athe flags say to preserve symbolic links, modification times, group, ownership, and permissions while copying.
Similarly, we can also copy a directory to another directory.
rsync -a /abc /backup-abc
It abccopies the contents of directory to target directory backup-abc.
Here, if we omit the trailing slash /, the source directory will be copied to the target directory.
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
How to Move Files and Directories in Linux Using mv Command
Publish Date:2025/04/05 Views:178 Category:OPERATING SYSTEM
-
mv We can move files and directories through the Linux terminal using the command with various parameters . Use mv the command to move files and directories mv(move) The command can move files and directories from one location to another. I
Read CPU flags from Cpuinfo
Publish Date:2025/04/05 Views:78 Category:OPERATING SYSTEM
-
This article will explain how to read the information in Linux cpuinfo . Later, we will look at what are CPU flags and what they mean. Reading in Linux cpuinfo /proc/cpuinfo The file contains detailed information about the CPU in your compu
Rsync Exclude Files and Directories in Linux
Publish Date:2025/04/05 Views:92 Category:OPERATING SYSTEM
-
rsync Is a powerful command line tool for synchronizing files and directories between two sites using a remote shell. Using rsync the command, you can copy data and files between systems and make additional backups. Additionally, you can ex
使用 PowerShell 查找 CPU 和 RAM 使用情况
Publish Date:2024/03/01 Views:146 Category:编程语言
-
本教程将教你使用 PowerShell 查找 CPU 和 RAM 使用情况。
Python rsync 同步
Publish Date:2023/06/22 Views:264 Category:Python
-
本文将探讨 rsync 以及我们如何从 Python 脚本中使用它。Python同步如上所述,rsync 是一个强大的工具
使用 Python 获取 CPU 数量
Publish Date:2023/06/15 Views:399 Category:Python
-
CPU 可以包含单核或多核。 单核只处理一个进程,而多核同时处理多个进程。本篇文章将介绍使用 Python 程序查找 CPU 内核总数的不同方法。使用 multiprocessing 模块获取 Python 中的 CPU 数量
Python获取CPU温度
Publish Date:2023/06/15 Views:290 Category:Python
-
本文的主要目的是演示如何借助 Python 中的 pythonnet 库读取和显示 CPU 温度。Python获取CPU温度
MATLAB CPU 时间
Publish Date:2023/04/23 Views:196 Category:MATLAB
-
本教程将讨论在 MATLAB 中使用 tic、toc 和 cputime 命令检查 CPU 时间。
如何在 Linux 中使用 mv 命令移动文件和目录
Publish Date:2023/03/17 Views:612 Category:操作系统
-
我们可以通过 Linux 终端使用带有各种参数的 mv 命令来移动文件和目录。

