美文网首页linux常用命令
文件查看及处理命令-01-cat命令

文件查看及处理命令-01-cat命令

作者: 夏胖运维 | 来源:发表于2020-11-21 07:09 被阅读0次

1. 命令介绍

cat命令的用途是连接文件或标准输入并打印。这个命令常用来显示文件内容,或者将几个文件连接起来显示,或者从标准输入读取内容并显示,它常与重定向符号配合使用。

2. 命令格式

cat [OPTION]... [FILE]...

3. 命令功能

主要由三大功能:

  1. 一次显示整个文件内容:cat filename
  2. 从键盘创建一个文件:cat > filename 只能创建新文件,不能编辑已有文件.
  3. 将几个文件合并为一个文件:cat file1 file2 > file

4. 常用选项

选项 含义
-A 等价于 -vET
-b 对非空输出行编号
-e 等价于 -vE
-E 在每行结束处显示 $
-n 对输出的所有行编号,由1开始对所有输出的行数编号
-s 连续两行以上的空白行,就代换为一行的空白行
-t 与 -vT 等价
-T 将跳格字符(tab)显示为 ^I
-v 使用 ^ 和 M- 引用,除了 LFD 和 TAB 之外

5. 常用实例

  1. 查看 /etc/passwd 文件内容
[root@localhost ~]# cat /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:999:998:User for polkitd:/:/sbin/nologin
tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
abrt:x:173:173::/etc/abrt:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
  1. 查看 /etc/rc.local 由多少行
[root@localhost ~]# cat -n /etc/rc.local
     1  #!/bin/bash
     2  # THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES
     3  #
     4  # It is highly advisable to create own systemd services or udev rules
     5  # to run scripts during boot instead of using this file.
     6  #
     7  # In contrast to previous versions due to parallel execution during boot
     8  # this script will NOT be run after all other services.
     9  #
    10  # Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure
    11  # that this script will be executed during boot.
    12  
    13  touch /var/lock/subsys/local
  1. 查看 /etc/rc.local 非空行由多少行
[root@localhost ~]# cat -b /etc/rc.local 
     1  #!/bin/bash
     2  # THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES
     3  #
     4  # It is highly advisable to create own systemd services or udev rules
     5  # to run scripts during boot instead of using this file.
     6  #
     7  # In contrast to previous versions due to parallel execution during boot
     8  # this script will NOT be run after all other services.
     9  #
    10  # Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure
    11  # that this script will be executed during boot.

    12  touch /var/lock/subsys/local
  1. 新建 test.txt 文件并同时输入'I am studying linux.'和主机名称
[root@localhost ~]# cat > text.txt << EOF
> I am studying linux.
> $(hostname)
> $PATH
> EOF
[root@localhost ~]# 
[root@localhost ~]# ll 
total 8
-rw-------. 1 root root 1482 May 24 18:16 anaconda-ks.cfg
-rw-r--r--. 1 root root  103 Nov 21 14:28 text.txt
[root@localhost ~]# cat text.txt 
I am studying linux.
localhost.localdomain
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin

查看文件内容可以发现 $(hostname)$PATH 被执行命令后结果和变量替换掉了。

注意:EOF表示内容开始和结束字符,可以使用与输入内容不冲突的字符替代

  1. 输入文件内容并且不修改变量和执行命令
[root@localhost ~]# cat >> text.txt << 'EOF'
> I am studying linux.
> $(hostname)
> $PATH
> EOF
[root@localhost ~]# cat text.txt 
I am studying linux.
localhost.localdomain
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
I am studying linux.
$(hostname)
$PATH

扩展:tac命令,tac是cat的反向拼写,因此命令的功能为反向显示文件内容。

[root@localhost ~]# tac /etc/rc.local 
touch /var/lock/subsys/local

# that this script will be executed during boot.
# Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure
#
# this script will NOT be run after all other services.
# In contrast to previous versions due to parallel execution during boot
#
# to run scripts during boot instead of using this file.
# It is highly advisable to create own systemd services or udev rules
#
# THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES
#!/bin/bash

相关文章

网友评论

    本文标题:文件查看及处理命令-01-cat命令

    本文链接:https://www.haomeiwen.com/subject/padsiktx.html