cat命令用法

cat命令用法#

显示文件内容

$ cat push.sh
#!/bin/bash
TIMESTAMP=$(date +%s)
rm -rf resources/ public/ .hugo_build.lock
git add .
git commit -m "update: ${TIMESTAMP}"
git push
sleep 3s
echo "Done! Commit${TIMESTAMP}"
sleep 3s

附带显示行号

$ cat -n push.sh
     1  #!/bin/bash
     2  TIMESTAMP=$(date +%s)
     3  rm -rf resources/ public/ .hugo_build.lock
     4  git add .
     5  git commit -m "update: ${TIMESTAMP}"
     6  git push
     7  sleep 3s
     8  echo "Done! Commit${TIMESTAMP}"
     9  sleep 3s

显示非打印字符,比如换行、TAB等

$ cat push.sh -v
#!/bin/bash^M
TIMESTAMP=$(date +%s)^M
^M
rm -rf resources/ public/ .hugo_build.lock^M
git add .^M
git commit -m "update: ${TIMESTAMP}"^M
git push^M
sleep 3s^M
echo "Done! Commit${TIMESTAMP}"^M
sleep 3s^M

cat不带任何选项,意思将标准输入(显示器)输出到标准输出(显示器),如下:

GNU核心工具

GNU核心工具#

GNU Core Utilities(GNU核心工具)包含一系列基础文件和工具,包括shell和文本操作工具。这些是GNU系统的最核心工具。

输出文件内容#

  • cat: 输出文件内容,默认输出到当前屏幕
  • tac: 倒序输出文件内容
  • nl: 输出文件内容并显示行号,等同于cat -n
  • od: 以八进制方式输出文件内容
  • base32: 32位编码解码文件
  • base64: 64位编码解码文件
  • basenc: 多种格式编码解码文件

格式化文件内容#

  • fmt:重新格式化段落文本
  • pr:将文件分页或分栏以便打印
  • fold:折行处理输入内容,使其适应指定宽度

输出文件部分内容#

  • head:输出文件的前部分
  • tail:输出文件的后部分
  • split:将文件拆分成多个部分
  • csplit:将文件拆分成按内容确定的部分

https://www.gnu.org/software/coreutils/

tac命令用法

tac命令用法#

tac默认倒序显示文件内容

$ tac push.sh
sleep 3s
echo "Done!"
sleep 3s
git push
git commit -m "update"
git add .
rm -rf resources/ public/ .hugo_build.lock

对比下cat的输出

$ cat push.sh
rm -rf resources/ public/ .hugo_build.lock
git add .
git commit -m "update"
git push
sleep 3s
echo "Done!"
sleep 3s

可以使用-r -t来使用正则表达式改变输出规则,如下逆序翻转所有字符

$ tac -r -s 'x\|[^x]' push.sh

s3 peels
"!enoD" ohce
s3 peels
hsup tig
"etadpu" m- timmoc tig
. dda tig
kcol.dliub_oguh. /cilbup /secruoser fr- mr