13个Cat命令管理文件实例汇总
在Linux系统中🐟,大多数🚞配置文件【jiàn】⚾、日志文【rì zhì wén】件【jiàn】⚾,甚至【shèn zhì】⬅shell脚本都使用文【shǐ yòng wén】本文件【jiàn】⚾格式【gé shì】,因此♍,Linux系统存【xì tǒng cún】在着多👍种文本编辑器🅱,但当你仅仅想要查看一下这【yī xià zhè】些文件【jiàn】⚾的内容【de nèi róng】🔃时,可使用一个简单的命令🚗-cat。
cat手册里这样描述:
cat命令读取文件【qǔ wén jiàn】内容🚔,并输出【bìng shū chū】到标准设备上【shè bèi shàng】📏面🍕。
cat是一条【shì yī tiáo】♊linux内置命令. 几乎所🚅有【yǒu】linux发行版【fā háng bǎn】都内置【dōu nèi zhì】(译注:或者说【huò zhě shuō】我从未听说过不内置💕cat命令的发行版【fā háng bǎn】)。接下来【jiē xià lái】,让我们💍开始学习如何🦁使用🔲。
1. 显示文件内容
最简单🏾的方法【de fāng fǎ】是直接【shì zhí jiē】🎐输入‘cat file_name’.
# cat /etc/issue CentOS release 5.10 (Final) Kernel \r on an \m
2. 同时显示行号【shì háng hào】🌼
当在读取内容很多的配置文【pèi zhì wén】🚥件时【jiàn shí】,如果同时显示【shí xiǎn shì】行号将😴会使操作变简单🐝,加上-n参数可【cān shù kě】😘以实现【yǐ shí xiàn】🚝.
# cat -n /etc/ntp.conf 1 # Permit time synchronization our time resource but do not 2 # permit the source to query or modify the service on this system 3 restrict default kod nomodify notrap nopeer noquery 4 restrict -6 default kod nomodify notrap nopeer noquery 5 6 # Permit all access over the loopback interface. This could be 7 # tightened as well, but to do so would effect some of the 8 # administration functions 9 restrict 127.0.0.1 10 restrict -6 ::1
3. 在非空🦓格行首👆显示行【xiǎn shì háng】号【hào】
类似于【lèi sì yú】🐑-n参数,-b也可以显示行【xiǎn shì háng】🐀号🍖。区别在于-b只在非【zhī zài fēi】空行前【kōng háng qián】显示行【xiǎn shì háng】🐀号🍖。
#cat -b /etc/ntp.conf 1 # Permit time synchronization our time resource but do not 2 # permit the source to query or modify the service on this system 3 restrict default kod nomodify notrap nopeer noquery 4 restrict -6 default kod nomodify notrap nopeer noquery 5 # Permit all access over the loopback interface. This could be 6 # tightened as well, but to do so would effect some of the 7 # administration functions 8 restrict 127.0.0.1 9 restrict -6 ::1
4. 显示【xiǎn shì】♉tab制表符
当你想【dāng nǐ xiǎng】要显示文本中【wén běn zhōng】的【de】🛵tab制表位🎞时. 可使用🔓-T参数. 它会在输入结🍟果中标【guǒ zhōng biāo】识为 ^I .
# cat -T /etc/hosts # Do not remove the following line, or various programs # that require network functionality will fail. 127.0.0.1^I^Ilocalhost.localdomain localhost ::1^I^Ilocalhost6.localdomain6 localhost6
5. 显示换【xiǎn shì huàn】🔛行符
-E参数在每行结【měi háng jié】尾使用【wěi shǐ yòng】💌 $ 表示换【biǎo shì huàn】🌒行符。如下所💎示 :
# cat -E /etc/hosts # Do not remove the following line, or various programs$ # that require network functionality will fail.$ 127.0.0.1 localhost.localdomain localhost$ ::1 localhost6.localdomain6 localhost6$
6. 同时显😡示制表符及换【fú jí huàn】💊行符【háng fú】
当你想【dāng nǐ xiǎng】要同时🌊达到【dá dào】💀-T及【jí】-E的效果🗾, 可使用-A参数.
# cat -A /etc/hosts # Do not remove the following line, or various programs$ # that require network functionality will fail.$ 127.0.0.1^I^Ilocalhost.localdomain localhost$ ::1^I^Ilocalhost6.localdomain6 localhost6$
7. 分屏显【fèn píng xiǎn】示🎸
当文件内容显示【xiǎn shì】超过了你的屏幕大🥋小, 可结合🚦cat命令与🔇其它命【qí tā mìng】〰令分屏显示【xiǎn shì】。使用管⏲道符【dào fú】 ( | )来连接【lái lián jiē】。
# cat /proc/meminfo | less # cat /proc/meminfo | more
在【zài】👯less与more显示结果的区【guǒ de qū】🏏别在【zài】👯于less参数可pageup及pagedown上下翻滚🎮。而【ér】🥣more仅能使🕑用空格【yòng kōng gé】向下翻【xiàng xià fān】屏。
8. 同时查看2个文件中的内容
位于【wèi yú】/root文件夹里有两个文件取名linux及desktop,每个文🚟件含有【jiàn hán yǒu】🍙以下内【yǐ xià nèi】🐫容 :
Linux : ubuntu, centos, redhat, mint and slackware
Desktop : gnome kde, xfce, enlightment, and cinnamon
当你想同时查🛅看两文【kàn liǎng wén】件中的内容时🚂,可按如【kě àn rú】🥃下方法【xià fāng fǎ】 :
# cat /root/linux /root/desktop ubuntu centos redhat mint slackware gnome kde xfce enlightment cinnamon
9. 排序显示【shì】👇
类似. 你也可【nǐ yě kě】以结合【yǐ jié hé】cat命令与其它命📨令来进行自定🚐义输出【yì shū chū】. 如结合💷 sort ,通过管【tōng guò guǎn】道符对🔨内容进行排序显示【xiǎn shì】。举例😇 :
# cat /root/linux | sort centos mint redhat slackware Ubuntu
10. 输入重【shū rù chóng】😪定向
你也可🦀将显示【jiāng xiǎn shì】结果输【jié guǒ shū】出重定向到屏幕或另🧕一个文件【jiàn】🔹。 只需要【zhī xū yào】使用 > 符号(大于号)即可输🌧出生成到另一【dào lìng yī】个文件【jiàn】🔹。
# cat /root/linux > /root/linuxdistro
以上命【yǐ shàng mìng】令会生【lìng huì shēng】🌈成一个【chéng yī gè】与🚧/root/linux内容一模一样🎌的叫linuxdistro的文件.
11. 新建文件
Linux下有多🗝种方法新建文【xīn jiàn wén】件【jiàn】😃。其中使【qí zhōng shǐ】用🏰cat就是方法之一.
# cat > operating_system Unix Linux Windows MacOS
当你输入【rù】cat > operatingsystem,它会生成【chéng】🍇一个operatingsystem的文件🐤。然后下【rán hòu xià】🏋面会显【miàn huì xiǎn】示空行【shì kōng háng】。此时你【cǐ shí nǐ】🏛可输入【rù】📉内容【nèi róng】。比如我【bǐ rú wǒ】们输入【rù】Unix, Linux, Windows 和 MacOS, 输入【rù】完😫成【chéng】后,按Ctrl-D存盘退出cat。此时你【cǐ shí nǐ】🏛会发现【huì fā xiàn】当前文【dāng qián wén】件夹下⌚会生成【chéng】🍇一个包含你刚才输入【rù】内容【nèi róng】的叫operating_system的文件🐤。
12.向文件中追加内容
当你使用两个 > 符时【fú shí】, 会将第【huì jiāng dì】🐧一个文【yī gè wén】件中的📬内容追【nèi róng zhuī】🦉加到第🗻二个文件的末尾。 举例 :
# cat /root/linux >> /root/desktop # cat /root/desktop
它会将【tā huì jiāng】 /root/linux的内容【de nèi róng】🙄追加到/root/desktop文件的末尾😣。
第二个文件的内容将会变成这样:
gnome kde xfce enlightment cinnamon ubuntu centos redhat mint slackware
13. 重定向🐆输入【shū rù】
你可使用📨 <命令【mìng lìng】(小于号)将文件【jiāng wén jiàn】☔输入到【shū rù dào】cat中🏔.
# cat < /root/linux
上面命令表示【lìng biǎo shì】 /root/linux中的内🏮容作为cat的输入🕘。屏幕上【píng mù shàng】显示如⏫下【xià】 :
ubuntu centos redhat mint slackware
为了更【wéi le gèng】📶清楚表示它的意义【yì yì】,我们使用以下🆔命令【mìng lìng】🎂 :
# cat < /root/linux | sort > linux-sort
此命令【cǐ mìng lìng】🗂这样理【zhè yàng lǐ】🕢解: 从/root/linux中读取🔳内容,然后排序,将结果【jiāng jié guǒ】🏞输出并生成【shēng chéng】linux-sort新文件
然后我🍋们看看linux-sort中的内【zhōng de nèi】😸容【róng】 :
centos mint redhat slackware ubuntu
以上是📷一些cat命令的日常基🌒本应用🚅. 更多相【gèng duō xiàng】关你可【guān nǐ kě】从【cóng】cat命令手♟册中学到并记【dào bìng jì】得经常🙈练习它【liàn xí tā】们.
via: http://linoxide.com/linux-command/13-cat-command-examples/
译者💒:hongchuntang 校对【xiào duì】:Caroline
关键词:Cat命令
阅读本文后您有什么感想? 已有 人给出评价!
- 0
- 0
- 0
- 0
- 0
- 0