每天2个Linux命令 cp nl


2017-07-18 每天2个Linux命令 cp命令

cp命令用来将一个或多个源文件或者目录复制到指定的目的文件或目录。

它可以将单个源文件复制成一个指定文件名的具体的文件或一个已经存在的目录下。

cp命令还支持同时复制多个文件,当一次复制多个文件时,目标文件参数必须是一个已经存在的目录,否则将出现错误。


(1)用法:

用法: cp [选项]... [-T]   源文件    目标文件

  或: cp  [选项]...         源文件...  目录

  或: cp  [选项]... -t      目录         源文件...

(2)功能:

将源文件复制至目标文件,或将多个源文件复制至目标目录。

(3)选项参数:

1) -f:                         强行复制文件或目录,不论目标文件或目录是否已存在

2) -i:                         覆盖既有文件之前先询问用户

3) -s:                        对源文件建立符号连接,而非复制文件

4) -b:                        覆盖已存在的文件目标前将目标文件备份

5) -v:                        详细显示命令执行的操作。

6) -f:                         强行复制文件或目录,不论目标文件或目录是否已存在

7) -i:                         覆盖既有文件之前先询问用户;

8) -a:                        等于-dR --preserve=all

9) -R, -r, --recursive               递归复制目录及其子目录内的所有内容

  10) -d                                     等于--no-dereference --preserve=links   

(4)实例:

1)[root@localhost Document]# cp mytext cpDir                  将文档拷贝到另一个文件夹下,该文件夹下没有同名文档

复制代码
[root@localhost Document]# mkdir cpDir
[root@localhost Document]# ll
总用量 0
drwxr-xr-x. 2 root root 6 5月   6 21:20 cpDir
[root@localhost Document]# cat >mytext <<EOF
> this is mytext!
> EOF
[root@localhost Document]# ll
总用量 4
drwxr-xr-x. 2 root root  6 5月   6 21:20 cpDir
-rw-r--r--. 1 root root 16 5月   6 21:21 mytext
[root@localhost Document]# cp mytext cpDir
[root@localhost Document]# ls -l ./cpDir
总用量 4
-rw-r--r--. 1 root root 16 5月   6 21:22 mytext
[root@localhost Document]# cat mytext
this is mytext!
[root@localhost Document]# ll
总用量 4
drwxr-xr-x. 2 root root 19 5月   6 21:22 cpDir
-rw-r--r--. 1 root root 16 5月   6 21:21 mytext

2)[root@localhost Document]# cp mytext cpDir  将文档拷贝到另一个文件夹下,该文件夹下有同名文档,因此会提醒是否覆盖。

复制代码
[root@localhost Document]# ll
总用量 4
drwxr-xr-x. 2 root root 19 5月   6 21:22 cpDir
-rw-r--r--. 1 root root 16 5月   6 21:21 mytext
[root@localhost Document]# cat >mytext <<EOF
> Modify the text !
> EOF
[root@localhost Document]# cp mytext cpDir
cp:是否覆盖"cpDir/mytext"? y
[root@localhost Document]# cat ./cpDir/mytext
Modify the text !

3)[root@localhost Document]# cp newDir Dir1  将newDir拷贝一份到Dir1目录下(当Dir1文件夹存在时);
将newDir下的所有东西拷贝一份到新建的Dir2目录(Dir2不存在)

复制代码
[root@localhost Document]# ll
总用量 0
[root@localhost Document]# mkdir newDir
[root@localhost Document]# touch ./newDir/{text1.txt,text2.txt}
[root@localhost Document]# ll
总用量 0
drwxr-xr-x. 2 root root 38 5月   6 21:36 newDir
[root@localhost Document]# mkdir Dir1
[root@localhost Document]# cp newDir Dir1     //没加-a参数会报错
cp: 略过目录"newDir"
[root@localhost Document]# cp -a newDir Dir1  //Dir1存在 
[root@localhost Document]# cp -a newDir Dir2  //Dir2不存在
[root@localhost Document]# ll
总用量 0
drwxr-xr-x. 3 root root 19 5月   6 21:37 Dir1
drwxr-xr-x. 2 root root 38 5月   6 21:36 Dir2
drwxr-xr-x. 2 root root 38 5月   6 21:36 newDir
[root@localhost Document]# ls ./Dir1        //存在的效果
newDir
[root@localhost Document]# ls ./Dir2        //不存在的效果
text1.txt  text2.txt

4)[root@localhost newDir]# cp -s text1.txt t1_link       建立一个指向text1.txt的快捷方式 :t1_link

复制代码
[root@localhost Document]# cp -s newDir newDir_link                //貌似文件夹的快捷方式只用-s参数不能创建
cp: 略过目录"newDir"
[root@localhost Document]# ll
总用量 0
drwxr-xr-x. 3 root root 19 5月   6 21:37 Dir1
drwxr-xr-x. 2 root root 38 5月   6 21:36 Dir2
drwxr-xr-x. 2 root root 38 5月   6 21:36 newDir
[root@localhost Document]# cd newDir
[root@localhost newDir]# ll
总用量 0
-rw-r--r--. 1 root root 0 5月   6 21:36 text1.txt
-rw-r--r--. 1 root root 0 5月   6 21:36 text2.txt
[root@localhost newDir]# cp -s text1.txt t1_link
[root@localhost newDir]# ll
总用量 0
lrwxrwxrwx. 1 root root 9 5月   6 21:43 t1_link -> text1.txt
-rw-r--r--. 1 root root 0 5月   6 21:36 text1.txt
-rw-r--r--. 1 root root 0 5月   6 21:36 text2.txt

5)[root@localhost Document]# cp -as newDir newDir_link        创建文件夹的快捷方式

复制代码
[root@localhost Document]# cp -as  newDir newDir_link     //a参数指定所有的newDir包括其内的内容创建快捷方式,但这里貌似有点限制
cp: "newDir_link/text1.txt":只能于当前目录中创建相对的符号链接
cp: "newDir_link/text2.txt":只能于当前目录中创建相对的符号链接
cp: "newDir_link/t1_link":只能于当前目录中创建相对的符号链接
[root@localhost Document]# ll
总用量 0
drwxr-xr-x. 3 root root 19 5月   6 21:37 Dir1
drwxr-xr-x. 2 root root 38 5月   6 21:36 Dir2
drwxr-xr-x. 2 root root 52 5月   6 21:43 newDir
drwxr-xr-x. 2 root root  6 5月   6 21:43 newDir_link     //文件夹快捷方式创建成功

6)[root@localhost Document]# cp -a newDir text1与[root@localhost Document]# cp -r newDir text2

复制代码
[root@localhost Document]# ll
总用量 0
[root@localhost Document]# mkdir newDir
[root@localhost Document]# touch ./newDir/{t1,t2}
[root@localhost Document]# mkdir text1 text2
[root@localhost Document]# cp -a newDir text1   //text1用-a参数
[root@localhost Document]# cp -r newDir text2   //text2用-r参数
[root@localhost Document]# ls -l text1/*
总用量 0
-rw-r--r--. 1 root root 0 5月   6 22:02 t1
-rw-r--r--. 1 root root 0 5月   6 22:02 t2
[root@localhost Document]# ls -l text2/*
总用量 0
-rw-r--r--. 1 root root 0 5月   6 22:03 t1
-rw-r--r--. 1 root root 0 5月   6 22:03 t2
[root@localhost Document]# cp -a newDir text3
[root@localhost Document]# cp -r newDir text4
[root@localhost Document]# ls -l text3/*
-rw-r--r--. 1 root root 0 5月   6 22:02 text3/t1
-rw-r--r--. 1 root root 0 5月   6 22:02 text3/t2
[root@localhost Document]# ls -l text4/*
-rw-r--r--. 1 root root 0 5月   6 22:04 text4/t1
-rw-r--r--. 1 root root 0 5月   6 22:04 text4/t2
[root@localhost Document]# ll
总用量 0
drwxr-xr-x. 2 root root 30 5月   6 22:02 newDir
drwxr-xr-x. 3 root root 19 5月   6 22:03 text1
drwxr-xr-x. 3 root root 19 5月   6 22:03 text2
drwxr-xr-x. 2 root root 30 5月   6 22:02 text3
drwxr-xr-x. 2 root root 30 5月   6 22:04 text4
[root@localhost Document]# cd text1
[root@localhost text1]# ll
总用量 0
drwxr-xr-x. 2 root root 30 5月   6 22:02 newDir
[root@localhost text1]# cd ../text2
[root@localhost text2]# ll
总用量 0
drwxr-xr-x. 2 root root 30 5月   6 22:03 newDir
[root@localhost text2]# cd ../text3
[root@localhost text3]# ll
总用量 0
-rw-r--r--. 1 root root 0 5月   6 22:02 t1
-rw-r--r--. 1 root root 0 5月   6 22:02 t2
[root@localhost text3]# cd ../text4
[root@localhost text4]# ll
总用量 0
-rw-r--r--. 1 root root 0 5月   6 22:04 t1
-rw-r--r--. 1 root root 0 5月   6 22:04 t2  //-a和-r没有差别,cp命令的差别主要集中在第二个参数是否存在上,
与此同时,ls -l命令的效果也有所差别

7)[root@localhost Document]# cp mytext{,.txt}   复制一份加上后缀存起来

复制代码
[root@localhost Document]# touch mytext
[root@localhost Document]# cp mytext{,.txt}
[root@localhost Document]# ll
总用量 0
-rw-r--r--. 1 root root  0 5月   6 22:15 mytext
-rw-r--r--. 1 root root  0 5月   6 22:15 mytext.txt
drwxr-xr-x. 2 root root 30 5月   6 22:02 newDir
drwxr-xr-x. 3 root root 19 5月   6 22:03 text1
drwxr-xr-x. 3 root root 19 5月   6 22:03 text2
drwxr-xr-x. 2 root root 30 5月   6 22:02 text3
drwxr-xr-x. 2 root root 30 5月   6 22:04 text4

8)[root@localhost Document]# cp --help

复制代码
[root@localhost Document]# cp --help
用法:cp [选项]... [-T] 源文件 目标文件
 或:cp [选项]... 源文件... 目录
 或:cp [选项]... -t 目录 源文件...
Copy SOURCE to DEST, or multiple SOURCE(s) to DIRECTORY.

Mandatory arguments to long options are mandatory for short options too.
  -a, --archive            等于-dR --preserve=all
      --attributes-only    仅复制属性而不复制数据      --backup[=CONTROL        为每个已存在的目标文件创建备份
  -b                类似--backup 但不接受参数
      --copy-contents        在递归处理是复制特殊文件内容
  -d                等于--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        不要覆盖已存在的文件(使前面的 -i 选项失效)
  -P, --no-dereference        不跟随源文件中的符号链接
  -p                等于--preserve=模式,所有权,时间戳
      --preserve[=属性列表    保持指定的属性(默认:模式,所有权,时间戳),如果
                    可能保持附加属性:环境、链接、xattr 等
  -c                           deprecated, same as --preserve=context
      --sno-preserve=属性列表    不保留指定的文件属性
      --parents            复制前在目标目录创建来源文件路径中的所有目录
  -R, -r, --recursive        递归复制目录及其子目录内的所有内容
      --reflink[=WHEN]        控制克隆/CoW 副本。请查看下面的内如。
      --remove-destination    尝试打开目标文件前先删除已存在的目的地
                    文件 (相对于 --force 选项)
      --sparse=WHEN        控制创建稀疏文件的方式
      --strip-trailing-slashes    删除参数中所有源文件/目录末端的斜杠
  -s, --symbolic-link        只创建符号链接而不复制文件
  -S, --suffix=后缀        自行指定备份文件的后缀
  -t,  --target-directory=目录    将所有参数指定的源文件/目录
                                           复制至目标目录
  -T, --no-target-directory    将目标目录视作普通文件
  -u, --update            只在源文件比目标文件新,或目标文件
                    不存在时才进行复制
  -v, --verbose        显示详细的进行步骤
  -x, --one-file-system    不跨越文件系统进行操作
  -Z, --context[=CTX]          set SELinux security context of destination
                                 file to default type, or to CTX if specified
      --help        显示此帮助信息并退出
      --version        显示版本信息并退出

默认情况下,源文件的稀疏性仅仅通过简单的方法判断,对应的目标文件目标文件也
被为稀疏。这是因为默认情况下使用了--sparse=auto 参数。如果明确使用
--sparse=always 参数则不论源文件是否包含足够长的0 序列也将目标文件创文
建为稀疏件。
使用--sparse=never 参数禁止创建稀疏文件。

当指定了--reflink[=always] 参数时执行轻量化的复制,即只在数据块被修改的
情况下才复制。如果复制失败或者同时指定了--reflink=auto,则返回标准复制模式。

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       不进行备份(即使使用了--backup 选项)
  numbered, t     备份文件加上数字进行排序
  existing, nil   若有数字的备份文件已经存在则使用数字,否则使用普通方式备份
  simple, never   永远使用普通方式备份

有一个特别情况:如果同时指定--force 和--backup 选项,而源文件和目标文件
是同一个已存在的一般文件的话,cp 会将源文件备份。

GNU coreutils online help: <http://www.gnu.org/software/coreutils/>
请向<http://translationproject.org/team/zh_CN.html> 报告cp 的翻译错误
要获取完整文档,请运行:info coreutils 'cp invocation'    

9)[root@localhost Document]# cp --version

复制代码
[root@localhost Document]# cp --version
cp (GNU coreutils) 8.22
Copyright (C) 2013 Free Software Foundation, Inc.
许可证:GPLv3+:GNU 通用公共许可证第3 版或更新版本<http://gnu.org/licenses/gpl.html>。
本软件是自由软件:您可以自由修改和重新发布它。
在法律范围内没有其他保证。

由Torbjörn Granlund、David MacKenzie 和Jim Meyering 编写。

(5)其他;

注意:   

一般情况下,shell会设置一个别名,在命令行下复制文件时,如果目标文件已经存在,<br/>
就会询问是否覆盖,不管你是否使用-i参数。但是如果是在shell脚本中执行cp时,没有-i参数时不会询问是否覆盖。<br/>
这说明命令行和shell脚本的执行方式有些不同。<br/>

命令行与shell脚本:<br/>

shell命令就是你说的终端的命令,shell翻译成壳的意思,它是包裹在linux内核外层的,<br/>
一个可通过一系列的linux命令对操作系统发出相关指令的人机界面。shell可以通过其条件语句和循环语句等,<br/>
把一系列linux命令结合在一起,形成一个相当于面向过程的程序,shell script,来实现一些较为复杂的功能。 <br/>
总括,shell是linux命令集的概称,是属于命令行的人机界面。linux的shell script 是由命令加一些条件组合起来的。<br/>

shell命令就是终端命令,shell编程其实和windows的批处理差不多,区别的是,shell的语言功能比批处理强大<br/>

2017-07-18 每天2个Linux命令 nl命令

nl命令读取 file 参数(缺省情况下标准输入),计算输入中的行号,将计算过的行号写入标准输出。

其默认的结果与cat -n有点不太一样, nl 可以将行号做比较多的显示设计,包括位数与是否自动补齐0等等的功能。


(1)用法:

用法:    nl  [选项]...  [文件]...

(2)功能:

功能:   nl命令在linux系统中用来计算文件中行号。nl 可以将输出的文件内容自动的加上行号!

(3)选项参数:

  1) -b:                                            指定行号指定的方式,主要有两种:

           -b a:                                     表示不论是否为空行,也同样列出行号(类似 cat -n)

           -b t:                                     如果有空行,空的那一行不要列出行号(默认值) 

  2) -n:                                           列出行号表示的方法,主要有三种:

          -n ln:                                    行号在萤幕的最左方显示

          -n rn:                                    行号在自己栏位的最右方显示,且不加 0

          -n rz:                                    行号在自己栏位的最右方显示,且加 0

  3) -w:                                           行号栏位的占用的位数

  4) -p:                                           在逻辑定界符处不重新开始计算

(4)实例:

 1)[root@localhost Documents]# nl nl_text1                     用nl列出文档中的内容,文件中的空白行不会加上行号

复制代码
[root@localhost Documents]# ll 
总用量 0
[root@localhost Documents]# cat >nl_text1 <<EOF
> I am studing orders of Linux!
> I am MenAngel!
> 
> I am 19 years old!
> 
> 
> I am from AnHui HeFei!
> EOF
[root@localhost Documents]# nl nl_text1
     1    I am studing orders of Linux!
     2    I am MenAngel!

     3    I am 19 years old!


     4    I am from AnHui HeFei!
复制代码
复制代码
[root@localhost Documents]# cat -b nl_text1          //效果等于cat -b
     1    I am studing orders of Linux!
     2    I am MenAngel!

     3    I am 19 years old!


     4    I am from AnHui HeFei!
复制代码

2)[root@localhost Documents]# nl -b a nl_text1                                     用nl命令打开输出文档内容,空行也输出行号!

复制代码
[root@localhost Documents]# nl -b a nl_text1
     1    I am studing orders of Linux!
     2    I am MenAngel!
     3    
     4    I am 19 years old!
     5    
     6    
     7    I am from AnHui HeFei!
[root@localhost Documents]# cat -n nl_text1                               //与cat -n具有相同的效果
     1    I am studing orders of Linux!
     2    I am MenAngel!
     3    
     4    I am 19 years old!
     5    
     6    
     7    I am from AnHui HeFei!

3)[root@localhost Documents]# nl -b a -n rz nl_text1                            让行号前面自动补上0,统一输出格式

复制代码
[root@localhost Documents]# nl -b a -n rz nl_text1
000001    I am studing orders of Linux!
000002    I am MenAngel!
000003    
000004    I am 19 years old!
000005    
000006    
000007    I am from AnHui HeFei!

4)[root@localhost Documents]# nl -b a -n rz -w 3 nl_text1  nl -b a -n rz命令行号默认为六位,
要调整位数可以加上参数-w 3调整为3位。

复制代码
[root@localhost Documents]# nl -b a -n rz -w 3 nl_text1
001    I am studing orders of Linux!
002    I am MenAngel!
003    
004    I am 19 years old!
005    
006    
007    I am from AnHui HeFei!
复制代码

5)[root@localhost Documents]# nl --help

复制代码
[root@localhost Documents]# nl --help
用法:nl [选项]... [文件]...
Write each FILE to standard output, with line numbers added.
With no FILE, or when FILE is -, read standard input.

Mandatory arguments to long options are mandatory for short options too.
  -b, --body-numbering=样式    使用指定样式编号文件的正文行目
  -d, --section-delimiter=CC    使用指定的CC 分割逻辑页数
  -f, --footer-numbering=样式    使用指定样式编号文件的页脚行目
  -h, --header-numbering=样式    使用指定样式编号文件的页眉行目
  -i, --page-increment=数值    设置每一行遍历后的自动递增值
  -l, --join-blank-lines=数值    设置数值为多少的若干空行被视作一行
  -n, --number-format=格式    根据指定格式插入行号
  -p, --no-renumber        在逻辑页数切换时不将行号值复位
  -s, --number-separator=字符串    可能的话在行号后添加字符串
  -v, --starting-line-number=数字    每个逻辑页上的第一行的行号
  -w, --number-width=数字    为行号使用指定的栏数
      --help        显示此帮助信息并退出
      --version        显示版本信息并退出

默认的选项设置是-v1 -i1 -l1 -sTAB -w6 -nrn -hn -bt -fn。CC 是用于分隔
逻辑页数的两个分界符,其中缺失的第二个字符暗含了":",如果您要指定"\",
请输入"\\"。可用的样式如下:

  a    对所有行编号
  t    对非空行编号
  n    不编行号
  pBRE    只对符合正则表达式BRE 的行编号

FORMAT 是下列之一:

  ln    左对齐,空格不用0 填充
  rn     右对齐,空格不用0 填充
  rz     右对齐,空格用0 填充


GNU coreutils online help: <http://www.gnu.org/software/coreutils/>
请向<http://translationproject.org/team/zh_CN.html> 报告nl 的翻译错误
要获取完整文档,请运行:info coreutils 'nl invocation'
复制代码

6)[root@localhost Documents]# nl --version

复制代码
[root@localhost Documents]# nl --version
nl (GNU coreutils) 8.22
Copyright (C) 2013 Free Software Foundation, Inc.
许可证:GPLv3+:GNU 通用公共许可证第3 版或更新版本<http://gnu.org/licenses/gpl.html>。
本软件是自由软件:您可以自由修改和重新发布它。
在法律范围内没有其他保证。

由Scott Bartram 和David MacKenzie 编写。

(5)其他:

功能的详细介绍:

在输出中,nl 命令根据您在命令行中指定的标志来计算左边的行。 输入文本必须写在逻辑页中。
每个逻辑页有头、主体和页脚节(可以有空节)。 除非使用 -p 标志,nl 命令在每个逻辑页开始的地方重新设置行号。

可以单独为头、主体和页脚节设置行计算标志(例如,头和页脚行可以被计算然而文本行不能)。

文章作者: 邓滔
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 邓滔 !
评论
  目录