Linux目錄和文件高級操作精講

博文目錄

一、Linux目錄結構

二、Linux查看文件內容基礎命令

三、壓縮和解壓縮文件

四、vi文本編輯器

一、Linux目錄結構

Linux目錄結構採用樹形目錄結構,包含根目錄和子目錄。
Linux目錄和文件高級操作精講

1、根目錄

所有分區、目錄、文件等的位置起點,整個樹形目錄結構中,使用獨立的一個“/”表示。

2、子目錄

常見的子目錄如/root、/bin、/boot、/dev、/etc、/home、/var、/usr、/sbin。

3、子目錄的作用

Linux目錄和文件高級操作精講

二、Linux查看文件內容基礎命令

1、cat——查看文件內容

cat用於一次性顯示文件全部內容。基本語法格式如下:
Linux目錄和文件高級操作精講

應用舉例:

[root@centos01 ~]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
[root@centos01 ~]# cat /etc/sysconfig/network
# Created by anaconda
[root@centos01 ~]# cat /etc/sysconfig/network /etc/hosts
# Created by anaconda
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6

2、more——查看文件內容

more用於全屏模式分頁顯示文件內容。基本語法格式如下:
Linux目錄和文件高級操作精講

交互操作方法:

  • 按Enter鍵向下逐行滾動;

  • 按空格鍵向下翻一屏;

  • 按q鍵退出;

應用舉例:

[root@centos01 ~]# more /etc/httpd/conf/httpd.conf 
#
# This is the main Apache HTTP server configuration file.  It contains the
# configuration directives that give the server its instructions.
# See <URL:http://httpd.apache.org/docs/2.4/> for detailed information.
# In particular, see 
# <URL:http://httpd.apache.org/docs/2.4/mod/directives.html>
--More--(2%)

3、less——查看文件內容

less命令的作用與more命令相同,但擴展功能更多。基本語法格式如下:
Linux目錄和文件高級操作精講

交互操作方法:

  • Page Up鍵:向上翻頁;

  • Page Down鍵:向下翻頁;

  • “/”鍵:查找關鍵內容;

  • “n”:查找下一個關鍵內容;

  • “N”:查找上一個關鍵內容;

其他功能與more命令基本類似;

4、head、tail——查看文件內容

head、tail命令的基本語法格式如下:
Linux目錄和文件高級操作精講

Linux目錄和文件高級操作精講

  • head:查看文件開頭的一部分內容(默認爲10行);

  • tail:查看文件結尾的一部分內容(默認爲10行);

應用舉例:

[root@centos01 ~]# head -2 /var/log/messages <!--顯示文件的開始2行內容-->
Jan 10 20:20:01 centos01 systemd: Started Session 9 of user root.
Jan 10 20:20:01 centos01 systemd: Starting Session 9 of user root.
[root@centos01 ~]# 
[root@centos01 ~]# tail -3 /var/log/messages <!--顯示文件的最後3行內容-->
Jan 10 23:10:01 centos01 systemd: Starting Session 30 of user root.
Jan 10 23:20:01 centos01 systemd: Started Session 31 of user root.
Jan 10 23:20:01 centos01 systemd: Starting Session 31 of user root.
[root@centos01 ~]# 
[root@centos01 ~]# tail -f /var/log/messages 
     <!--動態跟蹤文件尾部內容,便於實時監控文件內容的變化
(按Ctrl+c組合鍵終止)-->
Jan 10 23:01:01 centos01 systemd: Starting Session 29 of user root.
Jan 10 23:03:19 centos01 yum[11583]: Installed: httpd-tools-2.4.6-67.el7.centos.x86_64
Jan 10 23:03:19 centos01 yum[11583]: Installed: mailcap-2.1.41-2.el7.noarch
Jan 10 23:03:20 centos01 systemd: Reloading.
Jan 10 23:03:20 centos01 yum[11583]: Installed: httpd-2.4.6-67.el7.centos.x86_64
Jan 10 23:03:20 centos01 journal: g_dbus_interface_skeleton_unexport: assertion 'interface_->priv->connections != NULL' failed
Jan 10 23:10:01 centos01 systemd: Started Session 30 of user root.
Jan 10 23:10:01 centos01 systemd: Starting Session 30 of user root.
Jan 10 23:20:01 centos01 systemd: Started Session 31 of user root.
Jan 10 23:20:01 centos01 systemd: Starting Session 31 of user root.

5、wc——統計文件內容

wc用於統計文件中的單詞數量、(Word Count)、行數、字節數等。基本語法格式如下:
Linux目錄和文件高級操作精講

wc常用選項如下:

  • -l:統計行數;

  • -w:統計單詞個數;

  • -c:統計字節數;

應用舉例:

[root@centos01 ~]# wc -l /etc/passwd   <!--統計文件行數-->
41 /etc/passwd
[root@centos01 ~]# wc -w /etc/passwd <!--統計文件中單詞個數-->
81 /etc/passwd
[root@centos01 ~]# wc -c /etc/passwd  <!--統計文件中字節數-->
2104 /etc/passwd
[root@centos01 ~]# wc /etc/passwd   
   <!--不加選項統計順序依次是43行,85個單詞,2223個字節-->
  43   85 2223 /etc/passwd
[root@centos01 ~]# find /etc -name "*.conf" | wc -l  
     <!--統計/etc下有多少個以*.conf爲後綴的文件數量-->
437 

6、grep——檢索和過濾文件內容

grep命令用於在文件中查找並顯示包含指定字符串的行。基本語法格式如下:
Linux目錄和文件高級操作精講

grep命令常用選項如下:

  • -i:查找時忽略大小寫;

  • -v:反轉查找,輸出與條件不相符的行;

grep查找條件設置:

  • 要查找的字符串以雙引號括起來;

  • “^......”:表示查找以......開頭的字符串;

  • “......$”:表示查找以......結尾的字符串;

  • “^$”:表示查找空行;

應用舉例:

[root@centos01 ~]# grep -i "SSHD" /etc/passwd  
          <!--查詢sshd用戶忽略大小寫顯示出來-->
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
[root@centos01 ~]# grep "ftp" /etc/passwd   
                <!--查詢ftp用戶顯示出來-->
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
[root@centos01 ~]# grep -v "^#" /etc/yum.conf | grep -v "^$"  
                <!--過濾/etc/yum.conf中的註釋行和空行-->
[main]
cachedir=/var/cache/yum/$basearch/$releasever
keepcache=0
debuglevel=2
logfile=/var/log/yum.log
exactarch=1
obsoletes=1
gpgcheck=1
plugins=1
installonly_limit=5
bugtracker_url=http://bugs.centos.org/set_project.php?project_id=23&ref=http://bugs.centos.org/bug_report_page.php?category=yum
distroverpkg=centos-release

三、壓縮和解壓縮文件

1、gzip、gunzip——壓縮和解壓縮

Linux目錄和文件高級操作精講

應用舉例:

<!--壓縮和解壓縮方法一(“-9”選項代表高壓縮)-->
[root@centos01 ~]# ls 
anaconda-ks.cfg  CentOS-7.4-x86_64-1708.iso  initial-setup-ks.cfg
[root@centos01 ~]# gzip -9 initial-setup-ks.cfg 
[root@centos01 ~]# ls
anaconda-ks.cfg  CentOS-7.4-x86_64-1708.iso  initial-setup-ks.cfg.gz
[root@centos01 ~]# gzip -d initial-setup-ks.cfg.gz 
[root@centos01 ~]# ls
anaconda-ks.cfg  CentOS-7.4-x86_64-1708.iso  initial-setup-ks.cfg

<!--壓縮和解壓縮方法二(不加選項進行壓縮,解壓縮使用gunzip命令)-->
[root@centos01 ~]# ls
anaconda-ks.cfg  CentOS-7.4-x86_64-1708.iso  initial-setup-ks.cfg
[root@centos01 ~]# gzip initial-setup-ks.cfg 
[root@centos01 ~]# ls
anaconda-ks.cfg  CentOS-7.4-x86_64-1708.iso  initial-setup-ks.cfg.gz
[root@centos01 ~]# gunzip initial-setup-ks.cfg.gz 
[root@centos01 ~]# ls
anaconda-ks.cfg  CentOS-7.4-x86_64-1708.iso  initial-setup-ks.cfg

2、bzip2、bunzip2——壓縮和解壓縮

Linux目錄和文件高級操作精講

應用舉例:

<!--方法一-->
[root@centos01 ~]# ls
anaconda-ks.cfg  CentOS-7.4-x86_64-1708.iso  initial-setup-ks.cfg
[root@centos01 ~]# bzip2 -9 initial-setup-ks.cfg    <!--高速壓縮-->
[root@centos01 ~]# ls
anaconda-ks.cfg  CentOS-7.4-x86_64-1708.iso  initial-setup-ks.cfg.bz2
[root@centos01 ~]# bzip2 -d initial-setup-ks.cfg.bz2 <!--解壓縮-->
[root@centos01 ~]# ls
anaconda-ks.cfg  CentOS-7.4-x86_64-1708.iso  initial-setup-ks.cfg

<!--方法二-->
[root@centos01 ~]# ls
anaconda-ks.cfg  CentOS-7.4-x86_64-1708.iso  initial-setup-ks.cfg
[root@centos01 ~]# bzip2 initial-setup-ks.cfg  <!--壓縮-->
[root@centos01 ~]# ls
anaconda-ks.cfg  CentOS-7.4-x86_64-1708.iso  initial-setup-ks.cfg.bz2
[root@centos01 ~]# bunzip2 initial-setup-ks.cfg.bz2  <!--解壓縮-->
[root@centos01 ~]# ls
anaconda-ks.cfg  CentOS-7.4-x86_64-1708.iso  initial-setup-ks.cfg

3、tar——歸檔命令

tar命令製作歸檔文件、釋放歸檔文件。基本語法格式如下:
Linux目錄和文件高級操作精講

tar命令常用選項如下:

  • -c:創建.tar格式的包文件;

  • -x:解開.tar格式的包文件;

  • -v:輸出詳細信息;

  • -f:表示使用歸檔文件;

  • -p:打包時保留原始文件及目錄的權限;

  • -t:列表查看包內的文件;

  • -C:解包時指定釋放的目標文件夾;

  • -z:調用gzip程序進行壓縮或解壓;

  • -j:調用bzip2程序進行壓縮或解壓;

應用舉例:

[root@centos01 ~]# tar zcvf yun.gz yun/ <!--使用tar命令調用gzip將yun歸檔爲yun.gz-->
yun/
[root@centos01 ~]# ls
1.txt  anaconda-ks.cfg  initial-setup-ks.cfg  www  yun  yun.gz
[root@centos01 ~]# tar zxvf yun.gz -C /usr/src/   
         <!--將壓縮文件yun.gz解壓縮到/usr/src目錄中-->
yun/
[root@centos01 ~]# cd /usr/src/
[root@centos01 src]# ls
debug  kernels  yun
[root@centos01 ~]# tar jcvf yun.bz2 ./yun   
      <!--使用tar命令調用bzip將yun目錄數據進行壓縮-->
[root@centos01 ~]# tar jxvf yun.bz2 -C /usr/src/ 
    <!--將yun.bz2壓縮文件解壓縮到/usr/src/目錄中-->
./yun/
[root@centos01 ~]# cd /usr/src/
[root@centos01 src]# ls
debug  kernels  yun

4、dd命令壓縮與備份

選項和參數:

  • if:input file(原文件)也可以是設備;

  • of:output file(備份後的文件)也可以是設備;

  • bs:規劃的一個block(塊)的大小,若未指定則默認是512Bytes(字節);

  • count:多少塊的意思。

應用舉例:

[root@centos01 ~]# dd if=/dev/zero of=/usr/src/1.iso bs=30M count=10
<!--將/dev/zero文件中的信息複製到/usr/src目錄下創建一個1.iso的文件,一次30M,10次-->
記錄了10+0 的讀入
記錄了10+0 的寫出
314572800字節(315 MB)已複製,0.212995 秒,1.5 GB/秒
[root@centos01 ~]# cd /usr/src/
[root@centos01 src]# ls
1.iso  debug  kernels

四、vi文本編輯器

1、文本編輯器的作用

創建或者修改文本文件,維護Linux系統中的各種配置文件。

2、Linux中最常用的文本編輯器

  • vi:類Unix系統中默認的文本編輯器;

  • vim:vi編輯器的增強版本,習慣上也成爲vi;

3、vi編輯器的工作模式

命令模式、輸入模式、末行模式。不同模式之間的切換如下:
Linux目錄和文件高級操作精講

4、命令模式的常用操作

1)光標移動

Linux目錄和文件高級操作精講

2)複製、粘貼、刪除

Linux目錄和文件高級操作精講

3)文件內容查找

Linux目錄和文件高級操作精講

4)撤銷編輯及保存退出

Linux目錄和文件高級操作精講

5、末行模式的基本操作

1)保存文件及退出vi編輯器

Linux目錄和文件高級操作精講

2)打開新文件或讀入其他文件內容

Linux目錄和文件高級操作精講

3)文件內容替換

Linux目錄和文件高級操作精講

———————— 本文至此結束,感謝閱讀 ————————

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章