自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

13個(gè)Cat命令管理文件實(shí)例匯總

系統(tǒng) Linux
在Linux系統(tǒng)中,大多數(shù)配置文件、日志文件,甚至shell腳本都使用文本文件格式,因此,Linux系統(tǒng)存在著多種文本編輯器,但當(dāng)你僅僅想要查看一下這些文件的內(nèi)容時(shí),可使用一個(gè)簡(jiǎn)單的命令-cat。本文是13個(gè)Cat命令管理文件實(shí)例匯總,希望可以給您帶來(lái)幫助。

 [[91632]]

在Linux系統(tǒng)中,大多數(shù)配置文件、日志文件,甚至shell腳本都使用文本文件格式,因此,Linux系統(tǒng)存在著多種文本編輯器,但當(dāng)你僅僅想要查看一下這些文件的內(nèi)容時(shí),可使用一個(gè)簡(jiǎn)單的命令-cat。

cat手冊(cè)里這樣描述:

cat命令讀取文件內(nèi)容,并輸出到標(biāo)準(zhǔn)設(shè)備上面。

cat是一條linux內(nèi)置命令. 幾乎所有l(wèi)inux發(fā)行版都內(nèi)置(譯注:或者說(shuō)我從未聽(tīng)說(shuō)過(guò)不內(nèi)置cat命令的發(fā)行版)。接下來(lái),讓我們開(kāi)始學(xué)習(xí)如何使用。

1. 顯示文件內(nèi)容

最簡(jiǎn)單的方法是直接輸入‘cat file_name’.

# cat /etc/issue

CentOS release 5.10 (Final)
Kernel \r on an \m

2. 同時(shí)顯示行號(hào)

當(dāng)在讀取內(nèi)容很多的配置文件時(shí),如果同時(shí)顯示行號(hào)將會(huì)使操作變簡(jiǎn)單,加上-n參數(shù)可以實(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. 在非空格行首顯示行號(hào)

類(lèi)似于-n參數(shù),-b也可以顯示行號(hào)。區(qū)別在于-b只在非空行前顯示行號(hào)。

#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. 顯示tab制表符

當(dāng)你想要顯示文本中的tab制表位時(shí). 可使用-T參數(shù). 它會(huì)在輸入結(jié)果中標(biāo)識(shí)為 ^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. 顯示換行符

-E參數(shù)在每行結(jié)尾使用 $ 表示換行符。如下所示 :

# 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. 同時(shí)顯示制表符及換行符

當(dāng)你想要同時(shí)達(dá)到-T及-E的效果, 可使用-A參數(shù).

# 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. 分屏顯示

當(dāng)文件內(nèi)容顯示超過(guò)了你的屏幕大小, 可結(jié)合cat命令與其它命令分屏顯示。使用管道符 ( | )來(lái)連接。

# cat /proc/meminfo | less

# cat /proc/meminfo | more

在less與more顯示結(jié)果的區(qū)別在于less參數(shù)可pageup及pagedown上下翻滾。而more僅能使用空格向下翻屏。

8. 同時(shí)查看2個(gè)文件中的內(nèi)容

位于/root文件夾里有兩個(gè)文件取名linux及desktop,每個(gè)文件含有以下內(nèi)容 :

Linux : ubuntu, centos, redhat, mint and slackware

Desktop : gnome kde, xfce, enlightment, and cinnamon

當(dāng)你想同時(shí)查看兩文件中的內(nèi)容時(shí),可按如下方法 :

# cat /root/linux /root/desktop

ubuntu
centos
redhat
mint
slackware
gnome
kde
xfce
enlightment
cinnamon

9. 排序顯示

類(lèi)似. 你也可以結(jié)合cat命令與其它命令來(lái)進(jìn)行自定義輸出. 如結(jié)合 sort ,通過(guò)管道符對(duì)內(nèi)容進(jìn)行排序顯示。舉例 :

# cat /root/linux | sort

centos
mint
redhat
slackware
Ubuntu

10. 輸入重定向

你也可將顯示結(jié)果輸出重定向到屏幕或另一個(gè)文件。 只需要使用 > 符號(hào)(大于號(hào))即可輸出生成到另一個(gè)文件。

# cat /root/linux > /root/linuxdistro

以上命令會(huì)生成一個(gè)與/root/linux內(nèi)容一模一樣的叫l(wèi)inuxdistro的文件.

11. 新建文件

Linux下有多種方法新建文件。其中使用cat就是方法之一.

# cat > operating_system

Unix
Linux
Windows
MacOS

當(dāng)你輸入cat > operatingsystem,它會(huì)生成一個(gè)operatingsystem的文件。然后下面會(huì)顯示空行。此時(shí)你可輸入內(nèi)容。比如我們輸入U(xiǎn)nix, Linux, Windows 和 MacOS, 輸入完成后,按Ctrl-D存盤(pán)退出cat。此時(shí)你會(huì)發(fā)現(xiàn)當(dāng)前文件夾下會(huì)生成一個(gè)包含你剛才輸入內(nèi)容的叫operating_system的文件。

12.向文件中追加內(nèi)容

當(dāng)你使用兩個(gè) > 符時(shí), 會(huì)將第一個(gè)文件中的內(nèi)容追加到第二個(gè)文件的末尾。 舉例 :

# cat /root/linux >> /root/desktop

# cat /root/desktop

它會(huì)將 /root/linux的內(nèi)容追加到/root/desktop文件的末尾。

第二個(gè)文件的內(nèi)容將會(huì)變成這樣:

gnome
kde
xfce
enlightment
cinnamon
ubuntu
centos
redhat
mint
slackware

13. 重定向輸入

你可使用 <命令(小于號(hào))將文件輸入到cat中.

# cat < /root/linux

上面命令表示 /root/linux中的內(nèi)容作為cat的輸入。屏幕上顯示如下 :

ubuntu
centos
redhat
mint
slackware

為了更清楚表示它的意義,我們使用以下命令 :

# cat < /root/linux | sort > linux-sort

此命令這樣理解: 從/root/linux中讀取內(nèi)容,然后排序,將結(jié)果輸出并生成linux-sort新文件

然后我們看看linux-sort中的內(nèi)容 :

centos
mint
redhat
slackware
ubuntu

以上是一些cat命令的日?;緫?yīng)用. 更多相關(guān)你可從cat命令手冊(cè)中學(xué)到并記得經(jīng)常練習(xí)它們.


via: http://linoxide.com/linux-command/13-cat-command-examples/

譯者:hongchuntang 校對(duì):Caroline

責(zé)任編輯:黃丹 來(lái)源: Linux中國(guó)
相關(guān)推薦

2017-03-01 16:08:50

Linux文件管理命令

2010-06-24 14:12:20

Linux Cat命令

2010-01-15 19:37:36

Linux命令

2010-01-05 16:49:34

2023-07-23 19:26:18

Linuxcat 命令

2010-06-24 11:16:17

Linux Cat命令詳解

2010-06-24 14:08:25

Linux Cat命令

2022-04-20 12:17:50

命令Batcat

2011-04-12 10:42:41

Oracle日志文件管理

2013-12-17 10:39:24

命令top

2016-12-07 18:42:31

Androidadb shell命令

2010-06-24 14:38:48

Linux Cat命令

2010-06-24 14:49:00

Linux Cat詳解

2010-01-05 11:07:09

Ubuntu文件管理

2022-08-10 13:12:04

Linuxcat命令

2022-01-10 10:23:00

云計(jì)算云成本工具

2010-06-24 14:24:39

Linux Cat命令

2024-05-06 12:49:15

Linux文件系統(tǒng)

2010-06-24 14:16:31

Linux Cat命令

2011-04-14 17:03:50

Linuxsplitcat
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)