zoukankan      html  css  js  c++  java
  • Linux常用基本命令(cat)

    cat命令

    作用:连接多个文件并且打印到屏幕输出,或者重定向到其他文件,也可以用来查看显示单个文件,或者多个文件。

    格式:

    cat [option] [file]

    1,最简单的用法,直接跟文件名称,查看文件内容

    ghostwu@dev:~/linux/cat$ ls
    ghostwu@dev:~/linux/cat$ echo 'hello,my name is ghostwu, how are you?' > test.txt
    ghostwu@dev:~/linux/cat$ cat test.txt 
    hello,my name is ghostwu, how are you?
    ghostwu@dev:~/linux/cat$ 

    2,也可以使用如下方式,向文件写入或者追加内容

    ghostwu@dev:~/linux/cat$ ls
    test.txt
    ghostwu@dev:~/linux/cat$ cat >> test.txt << EOF
    > 这是我新增的内容
    > 这是第三行内容
    > EOF
    ghostwu@dev:~/linux/cat$ cat test.txt 
    hello,my name is ghostwu, how are you?
    这是我新增的内容
    这是第三行内容

    3,-n与-b   都是对文件进行编号,-b不会对空行编号

    ghostwu@dev:~/linux/cat$ cat test.txt 
    hello,my name is ghostwu, how are you?
    这是我新增的内容
    这是第三行内容
    ghostwu@dev:~/linux/cat$ cat >> test.txt << EOF
    > 
    > 
    > 上面加入了两个空行
    > EOF
    ghostwu@dev:~/linux/cat$ cat test.txt 
    hello,my name is ghostwu, how are you?
    这是我新增的内容
    这是第三行内容
    
    
    上面加入了两个空行
    ghostwu@dev:~/linux/cat$ cat -n test.txt 
         1    hello,my name is ghostwu, how are you?
         2    这是我新增的内容
         3    这是第三行内容
         4    
         5    
         6    上面加入了两个空行
    ghostwu@dev:~/linux/cat$ cat -b test.txt 
         1    hello,my name is ghostwu, how are you?
         2    这是我新增的内容
         3    这是第三行内容
    
    
         4    上面加入了两个空行
    ghostwu@dev:~/linux/cat$ 

    4,-E 在每一行的行尾显示美元符号

    ghostwu@dev:~/linux/cat$ cat -E test.txt 
    hello,my name is ghostwu, how are you?$
    这是我新增的内容$
    这是第三行内容$
    $
    $
    上面加入了两个空行$

    5,-s: 把两个以上连续的空行,变成一个

    ghostwu@dev:~/linux/cat$ cat -n test.txt 
         1    hello,my name is ghostwu, how are you?
         2    这是我新增的内容
         3    这是第三行内容
         4    
         5    
         6    
         7    
         8    上面加入了两个空行
         9    
        10    上面加入了一个空行
    ghostwu@dev:~/linux/cat$ cat -ns test.txt 
         1    hello,my name is ghostwu, how are you?
         2    这是我新增的内容
         3    这是第三行内容
         4    
         5    上面加入了两个空行
         6    
         7    上面加入了一个空行

    6,利用/dev/null 删除文件内容

    ghostwu@dev:~/linux/cat$ cat test.txt 
    hello,my name is ghostwu, how are you?
    这是我新增的内容
    这是第三行内容
    
    
    
    
    上面加入了两个空行
    
    上面加入了一个空行
    ghostwu@dev:~/linux/cat$ cat /dev/null > test.txt
    ghostwu@dev:~/linux/cat$ cat test.txt 

    7,利用重定向写入内容

    ghostwu@dev:~/linux/cat$ cat test.txt 
    ghostwu@dev:~/linux/cat$ cat > test.txt
    this is ghostwu 
    how are you
    ghostwu@dev:~/linux/cat$ cat test.txt 
    this is ghostwu
    how are you

    内容输入完毕,用ctrl+d或者ctrl+c中断输入

    8,显示多个文件内容

    ghostwu@dev:~/linux/cat$ cat > abc.txt
    this is abc.txt
    ghostwu@dev:~/linux/cat$ cat test.txt abc.txt 
    this is ghostwu
    how are you
    this is abc.txt
  • 相关阅读:
    测试
    Python 学习笔记【15】文件操作
    Python 学习笔记【14】集合
    Python 学习笔记【13】练习:三级菜单
    Python 学习笔记【12】字典
    Python 学习笔记【11】字符串操作
    Python 学习笔记【10】练习:购物车程序
    Python 学习笔记【09】列表、元组
    Python 学习笔记【08】数据类型、数据运算、进制转换
    Python 学习笔记【07】PEP 8 中英对照
  • 原文地址:https://www.cnblogs.com/ghostwu/p/9049119.html
Copyright © 2011-2022 走看看