Linux命令-文件管理篇-cat
1.cat 说明
cat 是一个文本文件查看和连接工具。查看一个文件的内容,用cat比较简单,就是cat 后面直接接文件名。
2.使用权限
所有使用者
<!– more –>
3.cat 语法
- 语法格式
cat [-AbeEnstTuv] [--help] [--version] fileName
- 参数说明
-n 或 –number:由 1 开始对所有输出的行数编号。
-b 或 –number-nonblank:和 -n 相似,只不过对于空白行不编号。
-s 或 –squeeze-blank:当遇到有连续两行以上的空白行,就代换为一行的空白行。
-v 或 –show-nonprinting:使用 ^ 和 M- 符号,除了 LFD 和 TAB 之外。
-E 或 –show-ends : 在每行结束处显示 $。
-T 或 –show-tabs: 将 TAB 字符显示为 ^I。
-A, –show-all:等价于 -vET。
-e:等价于”-vE”选项;
-t:等价于”-vT”选项;
4.cat 实例
- 查看/etc/bashrc的内容
[root@elk1 ~]# cat /etc/bashrc
- 查看/etc/目录下的bashrc内容,并且对非空白行进行编号,行号从1开始
[root@elk1 ~]# cat -b /etc/bashrc ... 70 SHELL=/bin/bash 71 # Only display echos from profile.d scripts if we are no login shell 72 # and interactive - otherwise just process them to set envvars 73 for i in /etc/profile.d/*.sh; do 74 if [ -r "$i" ]; then 75 if [ "$PS1" ]; then 76 . "$i" 77 else 78 . "$i" >/dev/null 79 fi 80 fi 81 done 82 unset i 83 unset -f pathmunge 84 fi 85 # vim:ts=4:sw=4
- 对/etc目录中的profile的所有的行(包括空白行)进行编号输出显示
[root@elk1 ~]# cat -n /etc/bashrc ... 65 66 # By default, we want umask to get set. This sets it for non-login shell. 67 # Current threshold for system reserved uid/gids is 200 68 # You could check uidgid reservation validity in 69 # /usr/share/doc/setup-*/uidgid file 70 if [ $UID -gt 199 ] && [ "`/usr/bin/id -gn`" = "`/usr/bin/id -un`" ]; then 71 umask 002 72 else 73 umask 022 74 fi 75 76 SHELL=/bin/bash 77 # Only display echos from profile.d scripts if we are no login shell 78 # and interactive - otherwise just process them to set envvars 79 for i in /etc/profile.d/*.sh; do 80 if [ -r "$i" ]; then 81 if [ "$PS1" ]; then 82 . "$i" 83 else 84 . "$i" >/dev/null 85 fi 86 fi 87 done 88 89 unset i 90 unset -f pathmunge 91 fi 92 # vim:ts=4:sw=4
- 查看/etc/下的bashrc内容,并且在每行的结尾处附加$符号
[root@elk1 ~]# cat -E /etc/bashrc ... esac$ }$ $ # By default, we want umask to get set. This sets it for non-login shell.$ # Current threshold for system reserved uid/gids is 200$ # You could check uidgid reservation validity in$ # /usr/share/doc/setup-*/uidgid file$ if [ $UID -gt 199 ] && [ "`/usr/bin/id -gn`" = "`/usr/bin/id -un`" ]; then$ umask 002$ else$ umask 022$ fi$ $ SHELL=/bin/bash$ # Only display echos from profile.d scripts if we are no login shell$ # and interactive - otherwise just process them to set envvars$ for i in /etc/profile.d/*.sh; do$ if [ -r "$i" ]; then$ if [ "$PS1" ]; then$ . "$i"$ else$ . "$i" >/dev/null$ fi$ fi$ done$ $ unset i$ unset -f pathmunge$ fi$ # vim:ts=4:sw=4$
- cat 加参数-n 和nl工具差不多,文件内容输出的同时,都会在每行前面加上行号
[root@elk1 ~]# cat -n /etc/bashrc [root@elk1 ~]# nl /etc/bashrc
- cat 可以同时显示多个文件的内容,比如我们可以在一个cat命令上同时显示两个文件的内容;
[root@elk1 ~]# cat /etc/fstab /etc/bashrc
- cat 对于内容极大的文件来说,可以通过管道|传送到more 工具,然后一页一页的查看;
[root@elk1 ~]# cat /etc/fstab /etc/bashrc | more
5.cat 创建、连接文件功能
- cat 有创建文件的功能,创建文件后,要以EOF或STOP结束;
[root@localhost ~]# cat > ng-sec.txt << EOF [root@elk1 ~]# cat >ng-sec.txt <<EOF > hello > this is cat test > EOF #说明:创建ng-sec.txt文件 [root@elk1 ~]# cat ng-sec.txt hello this is cat test
- 为刚刚创建的ng-sec.txt追加内容
[root@elk1 ~]# cat >>ng-sec.txt <<EOF > This is new contents for test2 > EOF [root@elk1 ~]# cat ng-sec.txt hello this is cat test This is new contents for test2 # 说明:>>可以追加信息到文档,可以看到已经追加了一行信息
- 通过cat 连接多个文件的内容并且输出到一个新文件中
假设我们有ng-sec01.txt、ng-sec02.txt,需要将他们文件内容合并后输出到ng-sec03.txt
注意:其原理是把两个文件的内容连接起来,然后创建ng-sec.txt文件,并且把几个文件的内容同时写入ng-sec03.txt中。特别注意的是,如果您输入到一个已经存在的ng-sec03.txt文件,会把该文件内容清空。
[root@localhost ~]# cat sir01.txt sir02.txt sir03.txt > sir04.txt [root@elk1 ~]# cat ng-sec01.txt ng-sec02.txt > ng-sec03.txt [root@elk1 ~]# cat ng-sec03.txt i am ng-sec01.txt i am ng-sec02.txt
原文地址:https://segmentfault.com/a/1190000017766866