linux中使用 head,tail和cat命令高效处理文件
原创 入门小站 入门小站 2022-07-29 22:28 发表于湖北 收录于合集#Linux478个1. head
命令
head 命令读取任何给定文件名的前十行。head 命令的基本语法是:
head [options] [file(s)]
例如,以下命令将显示名为 ' 的文件的前十行
/etc/passwd
。
# head /etc/passwd
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/bin/sh
bin:x:2:2:bin:/bin:/bin/sh
sys:x:3:3:sys:/dev:/bin/sh
sync:x:4:65534:sync:/bin:/bin/sync
games:x:5:60:games:/usr/games:/bin/sh
man:x:6:12:man:/var/cache/man:/bin/sh
lp:x:7:7:lp:/var/spool/lpd:/bin/sh
mail:x:8:8:mail:/var/mail:/bin/sh
news:x:9:9:news:/var/spool/news:/bin/sh
如果给出了多个文件,head 将分别显示每个文件的前十行。例如,以下命令将显示每个文件的十行。
# head /etc/passwd /etc/shadow
==> /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 uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin ==> /etc/shadow <==
root:$6$85e1:15740:0:99999:7:::
bin:*:15513:0:99999:7:::
daemon:*:15513:0:99999:7:::
adm:*:15513:0:99999:7:::
lp:*:15513:0:99999:7:::
sync:*:15513:0:99999:7:::
shutdown:*:15513:0:99999:7:::
halt:*:15513:0:99999:7:::
mail:*:15513:0:99999:7:::
uucp:*:15513:0:99999:7:::
如果希望检索的行数多于默认的 10 行,则
-n
选项与一个整数一起使用,告诉要检索的行数。比如下面的命令会先显示5
文件中的行/var/log/yum.log
文件。
# head -n5 /var/log/yum.log
Jan 10 00:06:49 Updated: openssl-1.0.1e-16.el6_5.4.i686
Jan 10 00:06:56 Updated: openssl-devel-1.0.1e-16.el6_5.4.i686
Jan 10 00:11:42 Installed: perl-Net-SSLeay-1.35-9.el6.i686
Jan 13 22:13:31 Installed: python-configobj-4.6.0-3.el6.noarch
Jan 13 22:13:36 Installed: terminator-0.95-3.el6.rf.noarch
事实上,没有必要使用
-n
选项。只需连字符并指定不带空格的整数即可获得与上述命令相同的结果。
# head -5 /var/log/yum.log
Jan 10 00:06:49 Updated: openssl-1.0.1e-16.el6_5.4.i686
Jan 10 00:06:56 Updated: openssl-devel-1.0.1e-16.el6_5.4.i686
Jan 10 00:11:42 Installed: perl-Net-SSLeay-1.35-9.el6.i686
Jan 13 22:13:31 Installed: python-configobj-4.6.0-3.el6.noarch
Jan 13 22:13:36 Installed: terminator-0.95-3.el6.rf.noarch
head 命令还可以使用 ' 显示任何所需的字节数
-c
选项后跟要显示的字节数。例如,以下命令将显示第一个45
给定文件的字节数。
# head -c 45 /var/log/yum.log
Jan 10 00:06:49 Updated: openssl-1.0.1e-16.el
2. tail
命令
tail 命令允许您显示任何文本文件的最后十行。与上面的 head 命令类似,tail 命令也支持 options
n
行数和n
字符数。
tail 命令的基本语法是:
# tail [options] [filenames]
例如,以下命令将打印名为 ' 的文件的最后十行
access.log
。
# tail access.log
123.172.16.152 - - [12/Aug/2021:22:37:10 +0800] "GET /jwt-decode/ HTTP/1.1" 200 1959 "-" "Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36"
123.172.16.152 - - [12/Aug/2021:22:37:10 +0800] "GET /assets/css/common.min.css?v=1.3 HTTP/1.1" 200 4430 "https://tooltt.com/jwt-decode/" "Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36"
123.172.16.152 - - [12/Aug/2021:22:37:10 +0800] "GET /assets/vendor/foundation/6.4.2/css/foundation.min.css?v=2 HTTP/1.1" 200 43202 "https://tooltt.com/jwt-decode/" "Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36"
123.172.16.152 - - [12/Aug/2021:22:37:10 +0800] "GET /assets/require.common.lib.js?v=2 HTTP/1.1" 200 24179 "https://tooltt.com/jwt-decode/" "Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36"
123.172.16.152 - - [12/Aug/2021:22:37:10 +0800] "GET /assets/js/tools/jwt-decode.js HTTP/1.1" 200 8200 "https://tooltt.com/jwt-decode/" "Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36"
123.172.16.152 - - [12/Aug/2021:22:37:10 +0800] "GET /assets/js/tooltt.js?v=1.0 HTTP/1.1" 200 253 "https://tooltt.com/jwt-decode/" "Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36"
123.172.16.152 - - [12/Aug/2021:22:37:10 +0800] "GET /assets/images/icon/jwt-decode.jpg HTTP/1.1" 200 9303 "https://tooltt.com/jwt-decode/" "Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36"
如果提供了多个文件,tail 将打印每个文件的最后十行,如下所示。
# tail access.log error.log
tail access.log error.log
==> access.log <==
54.36.149.17 - - [12/Aug/2021:22:43:35 +0800] "GET /java-topic/learningpaths/gson/index.html HTTP/1.1" 200 6119 "-" "Mozilla/5.0 (compatible; AhrefsBot/7.0; +http://ahrefs.com/robot/)"
203.208.60.72 - - [12/Aug/2021:22:43:37 +0800] "GET /java-topic/spring-restful/request-body-parameter-validation/index.html HTTP/1.1" 200 6798 "-" "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)"
64.64.102.212 - - [12/Aug/2021:22:43:38 +0800] "GET /java-topic/java/date-time/java8-datetimeformatter-example/index.html HTTP/1.1" 200 5525 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11"
183.210.180.35 - - [12/Aug/2021:22:43:44 +0800] "GET /static/cimg/swx.png HTTP/1.1" 200 57159 "https://iowiki.com/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36 Edg/92.0.902.67"
106.11.156.112 - - [12/Aug/2021:22:43:55 +0800] "GET /static/css/bootstrap.css HTTP/1.1" 200 29603 "https://www.rumenz.com/charsets/ref_utf_block.html" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.81 YisouSpider/5.0 Safari/537.36"
106.11.157.62 - - [12/Aug/2021:22:43:56 +0800] "GET /static/css/use.css?v=2.2 HTTP/1.1" 200 2546 "https://www.rumenz.com/charsets/ref_utf_block.html" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.81 YisouSpider/5.0 Safari/537.36"
106.11.155.36 - - [12/Aug/2021:22:43:57 +0800] "GET /static/foundation-icons/foundation-icons.css HTTP/1.1" 200 3974 "https://www.rumenz.com/charsets/ref_utf_block.html" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.81 YisouSpider/5.0 Safari/537.36"
106.11.158.35 - - [12/Aug/2021:22:43:58 +0800] "GET /static/css/ionicons.min.css?v=3.1 HTTP/1.1" 200 9722 "https://www.rumenz.com/charsets/ref_utf_block.html" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.81 YisouSpider/5.0 Safari/537.36"
111.225.149.60 - - [12/Aug/2021:22:44:14 +0800] "GET /robots.txt HTTP/1.1" 404 967 "-" "Mozilla/5.0 (Linux; Android 5.0) AppleWebKit/537.36 (KHTML, like Gecko) Mobile Safari/537.36 (compatible; Bytespider; https://zhanzhang.toutiao.com/)"
54.36.148.211 - - [12/Aug/2021:22:44:17 +0800] "GET /static/cimg/svg/rect1.svg HTTP/1.1" 200 300 "-" "Mozilla/5.0 (compatible; AhrefsBot/7.0; +http://ahrefs.com/robot/)"
==> error.log <==
2021/08/12 21:57:27 [error] 2098#0: *6440701 attempt to set status 404 via ngx.exit after sending out the response status 200, client: 116.9.31.6, server: rumenz.com, request: "GET /examples/node?page=25 HTTP/1.1", host: "rumenz.com"
2021/08/12 22:01:23 [error] 2099#0: *6440873 attempt to set status 404 via ngx.exit after sending out the response status 200, client: 207.46.13.53, server: rumenz.com, request: "GET /nos-conseils-estimation/estimation-appartement-1/comment-estimer-son-appartement-15 HTTP/1.1", host: "rumenz.com"
2021/08/12 22:02:25 [error] 2099#0: *6440956 attempt to set status 404 via ngx.exit after sending out the response status 200, client: 171.109.217.133, server: rumenz.com, request: "GET /examples/cpp11?page=17 HTTP/1.1", host: "rumenz.com"
2021/08/12 22:06:51 [error] 2099#0: *6441129 attempt to set status 404 via ngx.exit after sending out the response status 200, client: 171.109.216.145, server: rumenz.com, request: "GET /examples/php?page=66 HTTP/1.1", host: "rumenz.com"
2021/08/12 22:19:01 [error] 2099#0: *6441640 attempt to set status 404 via ngx.exit after sending out the response status 200, client: 223.111.134.182, server: rumenz.com, request: "GET /data/admin/allowurl.txt HTTP/1.1", host: "rumenz.com"
2021/08/12 22:30:57 [error] 2099#0: *6442107 attempt to set status 404 via ngx.exit after sending out the response status 200, client: 106.11.153.34, server: rumenz.com, request: "GET /try/showrazor.css HTTP/1.1", host: "www.rumenz.com", referrer: "https://www.rumenz.com/demo/iframe_try_razor_cs_005.html"
2021/08/12 22:33:32 [error] 2099#0: *6442239 attempt to set status 404 via ngx.exit after sending out the response status 200, client: 106.11.159.51, server: rumenz.com, request: "GET /try/showrazor.css HTTP/1.1", host: "www.rumenz.com", referrer: "https://www.rumenz.com/demo/iframe_try_razor_cs_007.html"
2021/08/12 22:34:15 [error] 2098#0: *6442282 attempt to set status 404 via ngx.exit after sending out the response status 200, client: 171.109.217.97, server: rumenz.com, request: "GET /examples/React?page=25 HTTP/1.1", host: "rumenz.com"
2021/08/12 22:37:25 [error] 2098#0: *6442447 attempt to set status 404 via ngx.exit after sending out the response status 200, client: 106.11.152.70, server: rumenz.com, request: "GET /try/showrazor.css HTTP/1.1", host: "www.rumenz.com", referrer: "https://www.rumenz.com/demo/iframe_try_razor_vb_011.html"
2021/08/12 22:40:59 [error] 2099#0: *6442601 attempt to set status 404 via ngx.exit after sending out the response status 200, client: 106.11.158.44, server: rumenz.com, request: "GET /try/showrazor.css HTTP/1.1", host: "www.rumenz.com", referrer: "https://www.rumenz.com/demo/iframe_try_razor_vb_013.html"
您还可以使用 ' 打印字符数
-c’
参数如下图。
# tail -c5 access.log
ymantec.com:443 - NONE/- text/html
3. cat 命令
这 cat
命令是使用最广泛的通用工具。它将标准输入复制到标准输出。如果文本文件不适合当前屏幕,该命令支持滚动。
cat 命令的基本语法是:
# cat [options] [filenames] [-] [filenames]
cat 最常用的用途是读取文件的内容。打开文件进行读取所需要的只是键入 cat 后跟一个空格和文件名。
# cat /etc/passwd
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/bin/sh
bin:x:2:2:bin:/bin:/bin/sh
sys:x:3:3:sys:/dev:/bin/sh
sync:x:4:65534:sync:/bin:/bin/sync
games:x:5:60:games:/usr/games:/bin/sh
man:x:6:12:man:/var/cache/man:/bin/sh
lp:x:7:7:lp:/var/spool/lpd:/bin/sh
…
cat 命令还用于将多个文件连接在一起。
# echo '入门' > 1
# echo '小站' > 2
# echo 'rumenz.com' > 3
# echo '入门小站' > 4
# cat 1 2 3 4 > 5
# cat 5
入门
小站
rumenz.com
入门小站
它还可以用于创建文件。它是通过执行 cat 后跟输出重定向运算符和要创建的文件名来实现的。
# cat > rumenz.txt
rumenz.com
我们可以为
cat
命令定制末端制造商。这里实现了。
# cat > test.txt << end
入门小站
rumenz.com
入门
end
# cat test.txt
入门小站
rumenz.com
入门
cat可以复制文件。
# cat rumenz.txt
入门小站
# cat rumenz.txt > rumenz1.txt
# cat rumenz1.txt
入门小站
tac
是 Linux 下的命令,tac命令用于将文件已行为单位的反序输出,即第一行最后显示,最后一行先显示。下面是一个简单案例
> seq 10 > rumenz.txt
[rumenz@local ~]# cat rumenz.txt
1
2
3
4
5
6
7
8
9
10
[rumenz@local ~]# tac rumenz.txt
10
9
8
7
6
5
4
3
2
1
相关文章
近期热文
入门小站 全栈入门知识 169篇原创内容 公众号 收录于合集 #Linux 478个 上一篇我常用Linux网络命令总结回顾下一篇划重点计算机网络知识点总结 阅读原文 阅读 1113 写下你的留言 标签:head,rumenz,HTTP,22,cat,12,2021,linux,com From: https://www.cnblogs.com/qiangfenger/p/16586111.html