首页 > 系统相关 >linux 操作系统下cp命令介绍及案例应用

linux 操作系统下cp命令介绍及案例应用

时间:2024-09-15 10:53:56浏览次数:3  
标签:tmp cp 操作系统 -- meng file linux txt

linux 操作系统下cp命令介绍及案例应用

cp命令是Linux操作系统中用于复制文件和目录的基本命令。它的功能强大,适用于各种文件管理任务

cp命令概述

  • 基本语法:
  • bash
  • cp [options] <source> <destination>

功能: 复制单个文件或多个文件到指定位置。 递归复制目录及其内容。 选项可以用来控制复制行为,如保留文件属性、强制覆盖等。

常用选项

  • -r 或 --recursive: 递归复制整个目录及其内容。
  • -p: 保留文件的时间戳、权限和所有者信息。
  • -f: 强制覆盖目标文件而不提示。
  • -i: 在覆盖文件前提示用户确认。
  • -v: 显示详细的复制过程信息。
  • -u: 仅在源文件比目标文件新时才进行复制。

命令介绍:

root@meng:~# which cp

/usr/bin/cp

root@meng:~# cp

cp: missing file operand

Try 'cp --help' for more information.

root@meng:~# cp --help

Usage: cp [OPTION]... [-T] SOURCE DEST

or: cp [OPTION]... SOURCE... DIRECTORY

or: cp [OPTION]... -t DIRECTORY SOURCE...

Copy SOURCE to DEST, or multiple SOURCE(s) to DIRECTORY.

Mandatory arguments to long options are mandatory for short options too.

-a, --archive same as -dR --preserve=all

--attributes-only don't copy the file data, just the attributes

--backup[=CONTROL] make a backup of each existing destination file

-b like --backup but does not accept an argument

--copy-contents copy contents of special files when recursive

-d same as --no-dereference --preserve=links

-f, --force if an existing destination file cannot be

opened, remove it and try again (this option

is ignored when the -n option is also used)

-i, --interactive prompt before overwrite (overrides a previous -n

option)

-H follow command-line symbolic links in SOURCE

-l, --link hard link files instead of copying

-L, --dereference always follow symbolic links in SOURCE

-n, --no-clobber do not overwrite an existing file (overrides

a previous -i option)

-P, --no-dereference never follow symbolic links in SOURCE

-p same as --preserve=mode,ownership,timestamps

--preserve[=ATTR_LIST] preserve the specified attributes (default:

mode,ownership,timestamps), if possible

additional attributes: context, links, xattr,

all

--no-preserve=ATTR_LIST don't preserve the specified attributes

--parents use full source file name under DIRECTORY

-R, -r, --recursive copy directories recursively

--reflink[=WHEN] control clone/CoW copies. See below

--remove-destination remove each existing destination file before

attempting to open it (contrast with --force)

--sparse=WHEN control creation of sparse files. See below

--strip-trailing-slashes remove any trailing slashes from each SOURCE

argument

-s, --symbolic-link make symbolic links instead of copying

-S, --suffix=SUFFIX override the usual backup suffix

-t, --target-directory=DIRECTORY copy all SOURCE arguments into DIRECTORY

-T, --no-target-directory treat DEST as a normal file

-u, --update copy only when the SOURCE file is newer

than the destination file or when the

destination file is missing

-v, --verbose explain what is being done

-x, --one-file-system stay on this file system

-Z set SELinux security context of destination

file to default type

--context[=CTX] like -Z, or if CTX is specified then set the

SELinux or SMACK security context to CTX

--help display this help and exit

--version output version information and exit

By default, sparse SOURCE files are detected by a crude heuristic and the

corresponding DEST file is made sparse as well. That is the behavior

selected by --sparse=auto. Specify --sparse=always to create a sparse DEST

file whenever the SOURCE file contains a long enough sequence of zero bytes.

Use --sparse=never to inhibit creation of sparse files.

When --reflink[=always] is specified, perform a lightweight copy, where the

data blocks are copied only when modified. If this is not possible the copy

fails, or if --reflink=auto is specified, fall back to a standard copy.

Use --reflink=never to ensure a standard copy is performed.

The backup suffix is '~', unless set with --suffix or SIMPLE_BACKUP_SUFFIX.

The version control method may be selected via the --backup option or through

the VERSION_CONTROL environment variable. Here are the values:

none, off never make backups (even if --backup is given)

numbered, t make numbered backups

existing, nil numbered if numbered backups exist, simple otherwise

simple, never always make simple backups

As a special case, cp makes a backup of SOURCE when the force and backup

options are given and SOURCE and DEST are the same name for an existing,

regular file.

GNU coreutils online help: <https://www.gnu.org/software/coreutils/>

Report any translation bugs to <https://translationproject.org/team/>

Full documentation <https://www.gnu.org/software/coreutils/cp>

or available locally via: info '(coreutils) cp invocation'

命令案例:

root@meng:~# ls

f1.txt.bz2 f2.txt.bz2 m1.txt m2.txt meng.txt meng.txt.bz2 rec00001f1.txt s1.txt s2.txt snap tmp

root@meng:~# cp m1.txt m1.txt.bak

root@meng:~# ls -arl m1.txt*

-rw-r--r-- 1 root root 109 Sep 9 09:06 m1.txt.bak

-rw-r--r-- 1 root root 109 Sep 2 16:01 m1.txt

root@meng:~# cp -ivrf *.txt tmp/

'm1.txt' -> 'tmp/m1.txt'

'm2.txt' -> 'tmp/m2.txt'

'meng.txt' -> 'tmp/meng.txt'

'rec00001f1.txt' -> 'tmp/rec00001f1.txt'

's1.txt' -> 'tmp/s1.txt'

's2.txt' -> 'tmp/s2.txt'

root@meng:~# ls tmp/

bin m1.txt m2.txt meng.sh meng.txt rec00001f1.txt s1.txt s2.txt

root@meng:~# cp -ivrf *.txt tmp/

cp: overwrite 'tmp/m1.txt'? y

'm1.txt' -> 'tmp/m1.txt'

cp: overwrite 'tmp/m2.txt'? y

'm2.txt' -> 'tmp/m2.txt'

cp: overwrite 'tmp/meng.txt'? y

'meng.txt' -> 'tmp/meng.txt'

cp: overwrite 'tmp/rec00001f1.txt'? y

'rec00001f1.txt' -> 'tmp/rec00001f1.txt'

cp: overwrite 'tmp/s1.txt'? y

's1.txt' -> 'tmp/s1.txt'

cp: overwrite 'tmp/s2.txt'? y

's2.txt' -> 'tmp/s2.txt'

root@meng:~#

标签:tmp,cp,操作系统,--,meng,file,linux,txt
From: https://blog.csdn.net/lisanmengmeng/article/details/142045117

相关文章

  • Linux内存管理方式
    Linux内存管理涉及到多个方面,包括物理内存的分配和回收、虚拟内存的管理、页面置换、内存保护等。以下是Linux内存管理的主要方式和机制:1.虚拟内存(VirtualMemory)虚拟地址空间:每个进程拥有自己的虚拟地址空间,虚拟地址空间与物理内存是分开的。进程在运行时使用虚拟地......
  • 1Panel:一个现代化、开源的 Linux 服务器运维管理面板
    前言之前有小伙伴问:Linux服务器运维管理除了宝塔,还有其他值得推荐的管理软件吗?,今天大姚给大家分享一个现代化、开源的Linux服务器运维管理面板:1Panel。项目介绍1Panel是新一代的Linux服务器运维管理面板,旨在通过现代化的Web界面帮助用户轻松管理Linux服务器。该项目是开......
  • 1Panel:一个现代化、开源的 Linux 服务器运维管理面板
    前言之前有小伙伴问:Linux服务器运维管理除了宝塔,还有其他值得推荐的管理软件吗?,今天大姚给大家分享一个现代化、开源的Linux服务器运维管理面板:1Panel。项目介绍1Panel是新一代的Linux服务器运维管理面板,旨在通过现代化的Web界面帮助用户轻松管理Linux服务器。该项目......
  • 3.linux进程管理
    目录一.进程管理1.进程概念2.进程生命周期和状态3.进程管理process3.1ps-静态查看进程3.2 top-动态查看进程3.3.使用信号控制进程3.4nice-优先级二.作业控制jobs三.虚拟文件系统proc四.线程(可看可不看)1.状态2.调度算法3.通信方式4.线程和进程的区别5.线......
  • USB总线-Linux内核USB3.0 Hub驱动分析(十四)
    1.概述USBHub提供了连接USB主机和USB设备的电气接口。USBHub拥有一个上行口,至少一个下行口,上行口连接上一级的Hub的下行口或者USB主机,连接主机的为RootHub,下行口连接下一级Hub的上行口或者USB设备。经过Hub的扩展,一个USB主机可以和多个USB设备通信。USBHub有如下特性:良好的......
  • Linux相关命令的使用
    一、压缩和归档的命令1.1压缩和解压缩操作对象是单个文件压缩方式:gzip--->.gzbzip--->.bz2xz--->.xz上是三种压缩方式:压缩率依次递减,压缩速率依次递减。gzipfile//将文件file按gzip格式进行压缩//压缩之后原文件就不存在了,只留下名为file.gz的压缩后......
  • Linux相关命令及shell命令
    一、链接文件1.1软链接(符号链接)ls-l能看到类型为链接文件的都是软连接软连接类似于windows的快捷方式创建软连接的方式:ln-s被链接的文件文件名要生成的文件名注意:1.我们一般创建软连接时最好使用绝对路径否则一旦相对位置发生变化链接就会失效。2.如果被链......
  • (Linux)嵌入式打卡第十一天
    指针数组简介概念:指针数组的本质是一个数组,只不过数组里面的每一个元素都是指针;定义格式:数组类型*数组名[成员个数];eg://定义一个数组,数组里面的每一个元素都是int*类型int*arr[5];指针数组使用#include<stdio.h>intmain(int......
  • Linux下载软件及卸载
    Linux发展史汤姆森·肯---UNIX之父斯托曼发起GNU计划莱纳斯linux之父现在主流的基于Linux的系统的发行版本centos、Ubuntu、Deepin(国产)、RedHat、...查看Ubuntu版本:cat/etc/issue查看内核版本:uname-a嵌入式层次结构一、用户空间命令脚本(命令的集合)app----......
  • linux 模拟题
    一,填空题linux主要是基于各种       来完成各种操作。打印当前所在的位置的命令是       linux的选项是哪两种        当前登录的用户tom,切换至用户家目录        想要使用命令本身的作用       ......