在Linux系統中一切都是文件,而對於服務程序的配置天然也就是在編輯程序的配置文件。node
cat命令用於查看純文本文件(較短的),格式爲:「cat [選項] [文件]」。linux
Linux系統中有須要用於查看文本內容的命令,但其中每一個命令又都有本身的特點特色,好比這個cat命令就是用於查看比較精簡的文本內容的,這個實際上是最好記的命令之一,由於cat在英語中是貓的意思,小貓咪是否是總給你一種嬌小、可愛的感受呢?若是您想看文本內容時還順便顯示行號的話,不妨再追加一個「-n」參數吧:redis
[root@linuxprobe ~]# cat -n initial-setup-ks.cfg 1 #version=RHEL7 2 # X Window System configuration information 3 xconfig --startxonboot 4 5 # License agreement 6 eula --agreed 7 # System authorization information 8 auth --enableshadow --passalgo=sha512 9 # Use CDROM installation media 10 cdrom 11 # Run the Setup Agent on first boot 12 firstboot --enable 13 # Keyboard layouts 14 keyboard --vckeymap=us --xlayouts='us' 15 # System language 16 lang en_US.UTF-8 17 18 ignoredisk --only-use=sda 19 # Network information 20 network --bootproto=dhcp --device=eno16777728 --onboot=off --ipv6=auto 21 network --bootproto=dhcp --hostname=localhost.localdomain 22 # Root password 23 rootpw --iscrypted $6$pDjJf42g8C6pL069$iI.PX/yFaqpo0ENw2pa7MomkjLyoae2zjMz2UZJ7bH3UO4oWtR1.Wk/hxZ3XIGmzGJPcs/MgpYssoi8hPCt8b/ 24 # System timezone 25 timezone America/New_York --isUtc 26 user --name=linuxprobe --password=$6$a9v3InSTNbweIR7D$JegfYWbCdoOokj9sodEccdO.zLF4oSH2AZ2ss2R05B6Lz2A0v2K.RjwsBALL2FeKQVgf640oa/tok6J.7GUtO/ --iscrypted --gecos="linuxprobe" 27 # System bootloader configuration 28 bootloader --location=mbr --boot-drive=sda 29 autopart --type=lvm 30 # Partition clearing information 31 clearpart --none --initlabel 32 33 %packages 34 @base 35 @core 36 @desktop-debugging 37 @dial-up 38 @fonts 39 @gnome-desktop 40 @guest-agents 41 @guest-desktop-agents 42 @input-methods 43 @internet-browser 44 @multimedia 45 @print-client 46 @x11 47 48 %end 49
more命令用於查看純文本文件(較長的),格式爲:「more [選項] 文件」。bash
若是須要去閱讀長篇小說、或者很是長的配置文件,那麼「小貓咪」可就真的不適合了,由於一旦您用cat命令去閱讀長篇的文本內容,信息就會在您的屏幕上快速翻滾,有點像***帝國電影那樣酷酷的感受,但其實本身還沒來得及看到內容就已經消失過去了。所以對於長篇的文本內容,我們推薦使用more命令來查看,它會給您起到翻頁的效果呢:session
[root@linuxprobe ~]# more initial-setup-ks.cfg #version=RHEL7 # X Window System configuration information xconfig --startxonboot # License agreement eula --agreed # System authorization information auth --enableshadow --passalgo=sha512 # Use CDROM installation media cdrom # Run the Setup Agent on first boot firstboot --enable # Keyboard layouts keyboard --vckeymap=us --xlayouts='us' # System language lang en_US.UTF-8 ignoredisk --only-use=sda # Network information network --bootproto=dhcp --device=eno16777728 --onboot=off --ipv6=auto network --bootproto=dhcp --hostname=localhost.localdomain--More--(43%)//此處會提示您已經閱讀了百分之多少,可使用空格和回車往下翻頁。
head命令用於查看純文本文檔的前N行,格式爲:「head [選項] [文件]」。
處理文本內容時,誰都不能保證永遠「循規蹈矩」的順序往下看完,若是我們只想看文本中前20行的內容呢:dom
[root@linuxprobe ~]# head -n 20 initial-setup-ks.cfg #version=RHEL7 # X Window System configuration information xconfig --startxonboot # License agreement eula --agreed # System authorization information auth --enableshadow --passalgo=sha512 # Use CDROM installation media cdrom # Run the Setup Agent on first boot firstboot --enable # Keyboard layouts keyboard --vckeymap=us --xlayouts='us' # System language lang en_US.UTF-8 ignoredisk --only-use=sda # Network information network --bootproto=dhcp --device=eno16777728 --onboot=off --ipv6=auto [root@localhost ~]#
tail命令用於查看純文本文檔的後N行或持續刷新內容,格式爲:「tail [選項] [文件]」。
固然我們還會遇到一種更奇葩的狀況,好比須要去查看文本內容的最後20行,那麼操做方法其實跟head命令是很是類似的,只須要執行「tail -n 20 文件名」命令就能夠達到這樣的目的。而tail命令最強悍的功能是用於持續刷新一個文件的內容,尤爲是對於想要實時看到最新日誌文件的時候特別有用:ssh
[root@localhost ~]# tail -f /var/log/messages
May 4 07:56:38 localhost gnome-session: Window manager warning: Log level 16: STACK_OP_ADD: window 0x1e00001 already in stack
May 4 07:56:38 localhost gnome-session: Window manager warning: Log level 16: STACK_OP_ADD: window 0x1e00001 already in stack
May 4 07:56:38 localhost vmusr[12982]: [ warning] [Gtk] gtk_disable_setlocale() must be called before gtk_init()
May 4 07:56:50 localhost systemd-logind: Removed session c1.
Aug 1 01:05:31 localhost systemd: Time has been changed
Aug 1 01:05:31 localhost systemd: Started LSB: Bring up/down networking.
Aug 1 01:08:56 localhost dbus-daemon: dbus[1124]: [system] Activating service name='com.redhat.SubscriptionManager' (using servicehelper)
Aug 1 01:08:56 localhost dbus[1124]: [system] Activating service name='com.redhat.SubscriptionManager' (using servicehelper)
Aug 1 01:08:57 localhost dbus-daemon: dbus[1124]: [system] Successfully activated service 'com.redhat.SubscriptionManager'
Aug 1 01:08:57 localhost dbus[1124]: [system] Successfully activated service 'com.redhat.SubscriptionManager'
//該日誌內容會實時刷新,同時按下「<strong>ctrl+c</strong>」健可結束命令。tcp
tr命令用於替換文本文件中的字符,格式爲:「tr [原始字符] [目標字符]」。ide
不少時候我們想要快速的替換文本內容中的一些詞彙,又或者將整個文本內容都進行替換,手工逐個替換真的太累了,並且對於處理大批量的內容很是不現實。此時我們即可以先使用cat命令讀取待處理的文本內容,而後經過管道符(第三章將爲您細緻講解)將這些數據傳遞給tr命令作替換操做便可,例如我們試試將文本內容完整替換成大寫英文吧:post
[root@linuxprobe ~]# cat anaconda-ks.cfg | tr [a-z] [A-Z] #VERSION=RHEL7 # SYSTEM AUTHORIZATION INFORMATION AUTH --ENABLESHADOW --PASSALGO=SHA512 # USE CDROM INSTALLATION MEDIA CDROM # RUN THE SETUP AGENT ON FIRST BOOT FIRSTBOOT --ENABLE IGNOREDISK --ONLY-USE=SDA # KEYBOARD LAYOUTS KEYBOARD --VCKEYMAP=US --XLAYOUTS='US' # SYSTEM LANGUAGE LANG EN_US.UTF-8 # NETWORK INFORMATION NETWORK --BOOTPROTO=DHCP --DEVICE=ENO16777728 --ONBOOT=OFF --IPV6=AUTO NETWORK --HOSTNAME=LOCALHOST.LOCALDOMAIN # ROOT PASSWORD ROOTPW --ISCRYPTED $6$PDJJF42G8C6PL069$II.PX/YFAQPO0ENW2PA7MOMKJLYOAE2ZJMZ2UZJ7BH3UO4OWTR1.WK/HXZ3XIGMZGJPCS/MGPYSSOI8HPCT8B/ # SYSTEM TIMEZONE TIMEZONE AMERICA/NEW_YORK --ISUTC USER --NAME=LINUXPROBE --PASSWORD=$6$A9V3INSTNBWEIR7D$JEGFYWBCDOOOKJ9SODECCDO.ZLF4OSH2AZ2SS2R05B6LZ2A0V2K.RJWSBALL2FEKQVGF640OA/TOK6J.7GUTO/ --ISCRYPTED --GECOS="LINUXPROBE" # X WINDOW SYSTEM CONFIGURATION INFORMATION XCONFIG --STARTXONBOOT # SYSTEM BOOTLOADER CONFIGURATION BOOTLOADER --LOCATION=MBR --BOOT-DRIVE=SDA AUTOPART --TYPE=LVM # PARTITION CLEARING INFORMATION CLEARPART --NONE --INITLABEL %PACKAGES @BASE @CORE @DESKTOP-DEBUGGING @DIAL-UP @FONTS @GNOME-DESKTOP @GUEST-AGENTS @GUEST-DESKTOP-AGENTS @INPUT-METHODS @INTERNET-BROWSER @MULTIMEDIA @PRINT-CLIENT @X11 %END
wc命令用於統計指定文本的行數、字數、字節數,格式爲「wc [參數] 文本」。
每當我講課提到這個命令的時候,總有同窗聯想到一些建築,其實二者是毫無關係的。Linux系統中的wc用於統計文本的行數、字數、字節數等文本內容的命令,若是爲了方便您去記憶,其實也能夠聯想到上廁所時真的好無聊,無聊到居然數完了整張報紙上有多少行字。
參數 | 做用 |
-l | 只顯示行數 |
-w | 只顯示單詞數 |
-c | 只顯示字節數 |
我們使用「-l」參數來統計行數,而passwd是用於保存系統賬戶信息的文件,所以下面的命令就是用於統計當前系統中有多少個用戶的做用啦,感受是否是很神奇:
[root@linuxprobe ~]# wc -l /etc/passwd 38 /etc/passwd
stat命令用於查看文件的具體存儲信息和時間等信息,格式「stat 文件名稱」。
使用stat命令能夠看到文件的存儲信息和時間等信息,下面會顯示出文件的三種時間狀態(已加粗):Access、Modify、Change,我們將在下面的touch命令中單獨爲您講解:
[root@localhost ~]# stat anaconda-ks.cfg File: ‘anaconda-ks.cfg’ Size: 1213 Blocks: 8 IO Block: 4096 regular file Device: fd00h/64768d Inode: 68912908 Links: 1 Access: (0600/-rw-------) Uid: ( 0/ root) Gid: ( 0/ root) Context: system_u:object_r:admin_home_t:s0Access: 2016-07-14 01:46:18.721255659 -0400Modify: 2016-05-04 15:44:36.916027026 -0400Change: 2016-05-04 15:44:36.916027026 -0400 Birth: -
cut命令用於按「列」來提取文本字符,格式爲:「cut [參數] 文本」。
如何準確的提取出最想要的數據,這也是我們在研究的技術方向,按基於「行」的方式來提取是比較簡單的,只須要設置好匹配項目和行數便可,可是按列搜索的話不只要使用「-f」參數來設置須要看的列數,還必須使用「-d」參數來設置間隔符號,由於passwd是用於保存用戶信息數據的文件,而每一項值都是經過冒號來間隔(見下面head命令的輸出演示),所以我們來嘗試下提取出passwd文件中的用戶名信息吧:
[root@linuxprobe ~]# head -n 2 /etc/passwdroot:x:0:0:root:/root:/bin/bashbin:x:1:1:bin:/bin:/sbin/nologin [root@linuxprobe ~]# cut -d: -f1 /etc/passwd root bin daemon adm lp sync shutdown halt mail operator games ftp nobody dbus polkitd unbound colord usbmuxd avahi avahi-autoipd libstoragemgmt saslauth qemu rpc rpcuser nfsnobody rtkit radvd ntp chrony abrt pulse gdm gnome-initial-setup postfix sshd tcpdump linuxprobe
diff命令用於比較多個文本文件的差別,格式爲:"diff [參數] 文件"。
我們不只可使用「--brief」參數來僅僅確認兩個文件是否不一樣,還可使用「-c」參數來詳細比較出多個文件的差別之處,這絕對是判斷文件是否被篡改的有力神器。例如先查看下兩個文件的內容,而後進行比較:
[root@linuxprobe ~]# cat diff_A.txt Welcome to linuxprobe.com Red Hat certified Free Linux Lessons Professional guidance Linux Course [root@linuxprobe ~]# cat diff_B.txt Welcome tooo linuxprobe.com Red Hat certified Free Linux LeSSonS ////////.....//////// Professional guidance Linux Course
僅僅顯示比較後的結果,判斷文件是否相同:
[root@linuxprobe ~]# diff --brief diff_A.txt diff_B.txt Files diff_A.txt and diff_B.txt differ
使用詳細的上下文輸出格式來描述文件內容具體的不一樣:
[root@linuxprobe ~]# diff -c diff_A.txt diff_B.txt *** diff_A.txt 2015-08-30 18:07:45.230864626 +0800 --- diff_B.txt 2015-08-30 18:08:52.203860389 +0800 *************** *** 1,5 **** ! Welcome to linuxprobe.com Red Hat certified ! Free Linux Lessons Professional guidance Linux Course --- 1,7 ---- ! Welcome tooo linuxprobe.com ! Red Hat certified ! Free Linux LeSSonS ! ////////.....//////// Professional guidance Linux Course