cat 命令
1.查看文件内容 例如:cat file.txt,这是cat最基本的功能之一 2.把多个文件合并成一个 例如:cat filel.txt file2.txt > newfile.txt 3.创建编辑新文件 例如:输入cat>filel.txt,后面接要编辑的内容,使用快捷键Ctrl+d或Ctrl+c可结束编辑,此功能应用不多,了解即可 4.非交互式的编辑或追加内容到文件尾部 这是生产工作中最重要的一个应用了,所以必须要熟练掌握,这里先给一个命令格式: cat>>file.txt<<EOF I love linux EOF 5.清空文件内容 例如:使用cat /dev/null>file.txt命令就可以把文件内容清空,但是文件还是存在的, 这个功能生产工作中也会用到
cat命令的参数选项及说明
cat >cat.txt<<EOF welcome to learn linux love linux forever EOF #<==这里要按回车才能结束,另外,EOF必须成对出现,但也可以用别的成对标签来替换。例如:lewen字符标签,默认情况下,结尾的EOE必须要顶格写。 cat >cat.txt<<-EOF #<==如果cat后面使用了-符号,则结尾的EOF可以不用顶格,但要用tab缩进。 welcome to learn linux love linux forever,my blog\'s site is http://www.wenyule.top EOF 执行过程 [root@cs6-mini ~]# cat >cat.txt<<EOF > > > welcome to learn linux > > love linux forever,my blog\'s site is http://www.wenyule.top > > >bye > EOF
[root@cs6-mini ~]# cat cat.txt welcome to learn linux love linux forever,my blog\'s site is http://www.wenyule.top bye
[root@cs6-mini ~]# cat -n cat.txt 1 2 3 welcome to learn linux 4 5 love linux forever,my blog\'s site is http://www.wenyule.top 6 7 8 bye [root@cs6-mini ~]# cat -b cat.txt 1 welcome to learn linux 2 love linux forever,my blog\'s site is http://www.wenyule.top 3 bye
执行cat命令,带-E选项
[root@cs6-mini ~]# cat -E cat.txt $ $ welcome to learn linux$ $ love linux forever,my blog\'s site is http://www.wenyule.top$ $ $ bye$ #<=说明:从上面的例子可以看出,-E选项就是把文件结尾的隐藏结束标识符$符号显示出来。即使是空行,结尾也是有结束标识符的,这一点大家要注意
[root@cs6-mini ~]# cat -A cat.txt $ $ welcome to learn linux$ $ love linux forever,my blog\'s site is http://www.wenyule.top$ $ $ bye$ [root@cs6-mini ~]# cat -e cat.txt $ $ welcome to learn linux$ $ love linux forever,my blog\'s site is http://www.wenyule.top$ $ $ bye$
执行cat 命令,带-s选项
[root@cs6-mini ~]# cat -s cat.txt welcome to learn linux love linux forever,my blog\'s site is http://www.wenyule.top #由于-s选项的原因,变成一个空行了。 bye
[root@cs6-mini ~]# grep -v "^$" cat.txt welcome to learn linux love linux forever,my blog\'s site is http://www.wenyule.top bye
执行cat命令编辑新文件
[root@cs6-mini ~]# cat > cat.txt hi,here is linux os [root@cs6-mini ~]# cat cat.txt hi,here is linux os
执行cat命令连接并显示多个文件
[root@cs6-mini ~]# cat test1.txt test2.txt test1 file test2 file
利用cat实现一键优化Linux系统脚本
#!/bin/bash /bin/cp /etc/sysctl.conf /etc/sysctl.conf.'date +%F_%T' #<==备份 sysctl.conf cat>>/etc/sysctl.conf<<EOF #<修改sysctl.conf #added by lewen at 2019 start net.ipv4.tcp_fin_timeout = 2 net.ipv4.tcp_tw_reuse = 1 net.ipv4.tcp_tw_recycle = 1 net.ipv4.tcp_syncookies = 1 net.ipv4.tcp_keepalive_time = 600 net.ipv4.ip_local_port_range = 4000 65000 net.ipv4.tcp_max_syn_backlog = 16384 net.ipv4.tcp_max_tw_buckets = 36000 net.ipv4.route.gc_timeout = 100 net.ipv4.tcp_syn_retries = 1 net.ipv4.tcp_synack_retries = 1 net.core.somaxconn = 16384 net.core.netdev_max_backlog = 16384 net.ipv4.tcp_max_orphans = 16384 #end EOF sysctl -p &>/dev/null #<==加载sysctl.conf [root@cs6-mini ~]# vim opt_sysctl.sh [root@cs6-mini ~]# sh opt_sysctl.sh [root@cs6-mini ~]# tail -16 opt_sysctl.sh net.ipv4.tcp_tw_reuse = 1 net.ipv4.tcp_tw_recycle = 1 net.ipv4.tcp_syncookies = 1 net.ipv4.tcp_keepalive_time = 600 net.ipv4.ip_local_port_range = 4000 65000 net.ipv4.tcp_max_syn_backlog = 16384 net.ipv4.tcp_max_tw_buckets = 36000 net.ipv4.route.gc_timeout = 100 net.ipv4.tcp_syn_retries = 1 net.ipv4.tcp_synack_retries = 1 net.core.somaxconn = 16384 net.core.netdev_max_backlog = 16384 net.ipv4.tcp_max_orphans = 16384 #end EOF sysctl -p &>/dev/null #<==加载sysctl.conf
利用cat在脚本中显示帮助菜单
[root@cs6-mini scripts]# cat exportfs_usage.sh #!/bin/bash exportfs_usage() { cat <<END USAGE:$0 {start|stop|monitor|status|validate-all} END } exportfs_usage [root@cs6-mini scripts]# sh exportfs_usage.sh USAGE:exportfs_usage.sh {start|stop|monitor|status|validate-all}
利用cat在脚本中显示内容选择菜单
[root@cs6-mini scripts]# cat menu.sh #!/bin/bash menu(){ cat <<END 1.[apple] 2.[pear] 3.[banana] 4.[cherry] 5.[orange] please select one that you like: END } menu [root@cs6-mini scripts]# sh menu.sh 1.[apple] 2.[pear] 3.[banana] 4.[cherry] 5.[orange] please select one that you like:
[root@cs6-mini scripts]# cat menu.sh #!/bin/bash menu(){ cat <<-END 1.[apple] 2.[pear] 3.[banana] 4.[cherry] 5.[orange] please select one that you like: END } menu [root@cs6-mini scripts]# sh menu.sh 1.[apple] 2.[pear] 3.[banana] 4.[cherry] 5.[orange] please select one that you like:
利用cat连接多文件合并Web集群日志
cat web01_access20130522.log log_web02access_20130522.log > web_access20130522.log cat web_access20130522.log
利用cat 命令实现一键MySQL主从同步
cat |mysql -uroot -p'oldboy'<<EOF CHANGE MASTER TO MASTER_HOST='10.0.0.16' MASTER_PORT=3306, MASTER_USER='lewenrep', MASTER_PASSWORD='lewenrep', MASTER_LOG_file='mysql-bin.000025', MASTER_LOG_POS=4269; EOF