linux命令第一天--cp

linux 命令

文件类命令

cp

目录

linux 命令

文件类命令

cp

--version

--h

-a

-b

-f

-i

-l

-R -r

-u


 man cp
 ​
 cp --h
 ​
 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
   -c                           deprecated, same as --preserve=context
       --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.
 ​
 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: <http://www.gnu.org/software/coreutils/>
 For complete documentation, run: info coreutils 'cp invocation'
 ​

 

--version

 查看版本
 ​
 cp --version

--h

 查看简介使用
 ​
 cp --h

-a

 cp -a a/ i/
 ​
 之前的目录情况:
 .
 ├── a
 │   ├── abc
 │   └── b
 │       └── c
 └── i
  └── e
      └── f
 ​
 ​
 执行后的目录情况:
 .
 ├── a
 │   ├── abc
 │   └── b
 │       └── c
 └── i
  ├── a
  │   ├── abc
  │   └── b
  │       └── c
  └── e
      └── f
 ​
 将a整个目录已经下面的全部递归到i目录下面
 ​
 ​
 cp -a a/b/ i/
 ​
 会将b以及b下面的复制到i目录下面
 复制前:
 .
 ├── a
 │   ├── abc
 │   └── b
 │       └── c
 └── i
  └── e
      └── f
 ​
 复制后:
 .
 ├── a
 │   ├── abc
 │   └── b
 │       └── c
 └── i
  ├── b
  │   └── c
  └── e
      └── f
 ​

-b

 cp -b fileA  fileB
 如果fileB已经存在,将会对fileB进行一个备份 备份名为fileB~
 ​
 ​

-f

 强制操作 很多命令的这个选项都是force

-i

 -i 交互 覆盖的时候进行询问 需要确认之后才会覆盖

 

-l

 硬链接(inode相同的两个文件) 相对软链接(相当于一个链接)
 cp -l abc edf
 ​
 [root@pdemo a]# stat abc edf
   File: ‘abc’
   Size: 4           Blocks: 8          IO Block: 4096   regular file
 Device: fd00h/64768d    Inode: 50400245    Links: 2
 ​
  Birth: -
   File: ‘edf’
   Size: 4           Blocks: 8          IO Block: 4096   regular file
 Device: fd00h/64768d    Inode: 50400245    Links: 2
 ​

-R -r

 cp -r a i
 ​
 复制前:
 .
 ├── a
 │   ├── abc
 │   └── b
 │       └── c
 └── i
     └── e
         └── f
 ​
 ​
 复制后:
 .
 ├── a
 │   ├── abc
 │   └── b
 │       └── c
 └── i
     └── e
         └── f
 ​

-u

 更新
 ​
 进当source 比des 文件更新的时候会更新
 [root@pdemo cp-test]# cp -u a/abc  i
 [root@pdemo cp-test]# ll i/abc 
 -rw-r--r-- 1 root root 4 Mar 29 20:49 i/abc
 [root@pdemo cp-test]# 
 [root@pdemo cp-test]# 
 [root@pdemo cp-test]# 
 [root@pdemo cp-test]# 
 [root@pdemo cp-test]# date
 Sun Mar 29 20:50:00 CST 2020
 ​
 再次执行 源文件没有更新的话 不执行
 [root@pdemo cp-test]# cp -u a/abc  i
 [root@pdemo cp-test]# 
 [root@pdemo cp-test]# 
 [root@pdemo cp-test]# ll i/abc 
 -rw-r--r-- 1 root root 4 Mar 29 20:49 i/abc
 ​
 ​
 修改源文件 再次测试
 [root@pdemo cp-test]# date
 Sun Mar 29 20:51:03 CST 2020
 [root@pdemo cp-test]# 
 [root@pdemo cp-test]# echo hello >> a/abc 
 [root@pdemo cp-test]# 
 [root@pdemo cp-test]# 
 [root@pdemo cp-test]# cp -u a/abc  i
 cp: overwrite ‘i/abc’? 
 ​
 此处因为有默认的alias cp=“cp -i” 导致覆盖的时候会出现交互式询问
 ​
引用中提到了cp命令的一些用法。在Linux中,cp命令用于复制文件或文件夹。可以有以下几种用法: 1. 复制单个文件到目标文件夹:cp 文件名 目标文件夹 例如:cp 111.txt /usr/local/test2 这个命令会将文件111.txt复制到目标文件夹/usr/local/test2中。 2. 复制多个文件到目标文件夹:cp 文件1 文件2 ... 目标文件夹 例如:cp 111.txt 1111.txt 111111.txt /usr/local/test2 这个命令会将文件111.txt、1111.txt和111111.txt一次性复制到目标文件夹/usr/local/test2中。 3. 使用通配符复制多个文件到目标文件夹:cp *.txt 目标文件夹 例如:cp *.txt /usr/local/test2 这个命令会将当前文件夹下所有以.txt结尾的文件都复制到目标文件夹/usr/local/test2中。 4. 复制文件夹及文件夹下所有内容到目标文件夹:cp -r 源文件夹 目标文件夹 例如:cp -r /folder1 /folder2 这个命令会将源文件夹/folder1及其下所有内容复制到目标文件夹/folder2中。 需要注意的是,当目标文件夹下已经存在同名文件时,系统会询问是否覆盖已有文件,可以选择y表示覆盖,n表示不覆盖。也可以使用cp命令的-i选项来在覆盖时先询问是否执行操作。 总结一下,cp命令用于文件和文件夹的复制,可以复制单个文件、多个文件、使用通配符复制多个文件,以及复制文件夹及其下所有内容。在复制时可以选择是否覆盖已有文件。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [日更第2天:Linux常用命令cp用法](https://blog.csdn.net/weixin_43980975/article/details/121846417)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* [Linux常用命令cp详解](https://blog.csdn.net/qq_44840148/article/details/105544590)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值