如何使用Linux/Unix上使用CAT和CP命令

时间:2020-02-23 14:39:05  来源:igfitidea点击:

cat 命令

根据其man 的页面,CAT连接文件并打印标准输出。
它的简单语法就像:

cat [OPTION]… [FILE]…

我们可以使用的各种选项如下:

  • -A,–show-all可以显示制表符,使用$的行尾和非打印
  • -b,–number-非空数字非空输出行,覆盖-n
  • -e相当于-vE
  • -E,–show-ends在每行末尾显示$符号
  • -n,–number编号所有输出线
  • -s,–squeeze-blank抑制重复的空输出行
  • -t相当于-vT
  • -T,–show-tabs将TAB字符显示为^ I
  • -v,–show-nonprinting

cat 示例

$cat -A text.txt  ## shows Tabs, end of lines using $and non-printing
A test-preparation routine proven to help you pass Red HatM-bM-^@M-^Ys tough certification exams$
Clearly defined chapter learning objectives covering all RHCSA (EX200) and RHCE (EX300) exam topics$
Chapter-ending review questions to help you drill on key concepts you must know thoroughly$
Chapter-ending labs showing what real exam tasks and assignments look like$
$cat -b rsync1.txt  ## Numbers all non-blank lines
      1  Features of Rsync include:
      
      2  Support for copying links, devices, owners, groups, and permissions
      
      3  Exclude and exclude-from options similar to GNU tar
      
      4  A CVS exclude mode for ignoring the same files that CVS would ignore
      
      5  Can use any transparent remote shell, including ssh or rsh
      6  Does not require super-user privileges
      7  Pipelining of file transfers to minimize latency costs
      8  Support for anonymous or authenticated rsync daemons (ideal for mirroring)
$cat -n rsync1.txt  ## Numbers all lines whether empty or not
      1  Features of Rsync include:
      2
      3  Support for copying links, devices, owners, groups, and permissions
      4
      5  Exclude and exclude-from options similar to GNU tar
      6
      7  A CVS exclude mode for ignoring the same files that CVS would ignore
      8
      9  Can use any transparent remote shell, including ssh or rsh
     10  Does not require super-user privileges
     11  Pipelining of file transfers to minimize latency costs
     12  Support for anonymous or authenticated rsync daemons (ideal for mirroring)

CAT也可用于将两个或者多个文件组合成一个。

cat file1.txt file2.txt >> file3.txt

file1.txt和file2.txt的内容将组合为file3.txt

使用Linux/UNIX CP命令

CP最简单的解释是用于复制文件和目录的Linux命令行工具。
它将文件从源复制到DEST或者多个源代码到目录。

语法

cp [source-file] [destination-file]
cp [source-file] [destination-directory]
cp [source-directory] [destination-directory]

CP最常见的选项

-n,-no-clobber不要覆盖现有的文件-i, - 在覆盖-l之前, - 链接硬链接文件而不是复制-r,-r,-recuraive复制目录递归 - s,-symbolic-link使符号链接而不是复制-p与-preserve = mode,所有权,timestamps-u,oply ockdate oply ockdate oply,只有目标文件或者目标文件丢失 - v, - 鼠标显示输出det-z将目标文件的selinux安全上下文设置为默认类型

与CP命令一起使用有吨的其他选项。
请查看其man 的页面。

行动中的CP的例子

1 ..将文件复制到目录

cp /home/tech/django/index.html /home/pench/backup/

2 ..将文件复制到同一目录

cp /home/tech/django/index.html index2.html

3 ..递归地将目录复制到另一个目录并显示详细输出

cp -Rv /home/tech/django//home/pench/backup/same as 
cp -rv /home/tech/django//home/pench/backup/

4 ..将文件复制到目录,并确保仅复制新文件,并且其他更新

cp -uv file1.txt file2.txt file3.txt /home/pench/backup/

5 ..导致CP提示并询问我们是否确定复制

cp -i /etc/httpd/conf/httpd.conf /home/pench/backup/

6 ..将文件复制到目录并确保不覆盖具有相同文件名的文件

cp -n file1.txt file2.txt file3.txt /home/pench/backup/

7 ..将文件复制到目录并保留其所有权和时间戳

cp /home/tech/django/index.html /home/pench/backup/

8 ..使用详细输出在index2.html中创建索引.html的符号链接

cp -sv /home/tech/django/index.html index2.html

9 ..使用详细输出创建index2.html的index.html的硬链接

cp -lv /home/tech/django/index.html index2.html