溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Linux如何用命令操作目錄和文件

發(fā)布時(shí)間:2020-06-06 10:42:35 來(lái)源:億速云 閱讀:338 作者:Leah 欄目:系統(tǒng)運(yùn)維

這篇文章主要為大家分享Linux操作目錄和文件的常用命令。文中還介紹了linux的目錄結(jié)構(gòu)以及vi文本編輯器的使用方法,希望大家通過(guò)這篇文章能有所收獲。

一、Linux目錄結(jié)構(gòu)

Linux目錄結(jié)構(gòu)采用樹形目錄結(jié)構(gòu),包含根目錄和子目錄。
Linux如何用命令操作目錄和文件

1、根目錄

所有分區(qū)、目錄、文件等的位置起點(diǎn),整個(gè)樹形目錄結(jié)構(gòu)中,使用獨(dú)立的一個(gè)“/”表示。

2、子目錄

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

3、子目錄的作用

Linux如何用命令操作目錄和文件

二、Linux查看文件內(nèi)容基礎(chǔ)命令

1、cat——查看文件內(nèi)容

cat用于一次性顯示文件全部?jī)?nèi)容?;菊Z(yǔ)法格式如下:
Linux如何用命令操作目錄和文件

應(yīng)用舉例:

[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——查看文件內(nèi)容

more用于全屏模式分頁(yè)顯示文件內(nèi)容?;菊Z(yǔ)法格式如下:
Linux如何用命令操作目錄和文件

交互操作方法:

  • 按Enter鍵向下逐行滾動(dòng);

  • 按空格鍵向下翻一屏;

  • 按q鍵退出;

應(yīng)用舉例:

[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——查看文件內(nèi)容

less命令的作用與more命令相同,但擴(kuò)展功能更多?;菊Z(yǔ)法格式如下:
Linux如何用命令操作目錄和文件

交互操作方法:

  • Page Up鍵:向上翻頁(yè);

  • Page Down鍵:向下翻頁(yè);

  • “/”鍵:查找關(guān)鍵內(nèi)容;

  • “n”:查找下一個(gè)關(guān)鍵內(nèi)容;

  • “N”:查找上一個(gè)關(guān)鍵內(nèi)容;

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

4、head、tail——查看文件內(nèi)容

head、tail命令的基本語(yǔ)法格式如下:
Linux如何用命令操作目錄和文件

Linux如何用命令操作目錄和文件

  • head:查看文件開頭的一部分內(nèi)容(默認(rèn)為10行);

  • tail:查看文件結(jié)尾的一部分內(nèi)容(默認(rèn)為10行);

應(yīng)用舉例:

[root@centos01 ~]# head -2 /var/log/messages <!--顯示文件的開始2行內(nèi)容-->
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行內(nèi)容-->
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 
     <!--動(dòng)態(tài)跟蹤文件尾部?jī)?nèi)容,便于實(shí)時(shí)監(jiān)控文件內(nèi)容的變化
(按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——統(tǒng)計(jì)文件內(nèi)容

wc用于統(tǒng)計(jì)文件中的單詞數(shù)量、(Word Count)、行數(shù)、字節(jié)數(shù)等?;菊Z(yǔ)法格式如下:
Linux如何用命令操作目錄和文件

wc常用選項(xiàng)如下:

  • -l:統(tǒng)計(jì)行數(shù);

  • -w:統(tǒng)計(jì)單詞個(gè)數(shù);

  • -c:統(tǒng)計(jì)字節(jié)數(shù);

應(yīng)用舉例:

[root@centos01 ~]# wc -l /etc/passwd   <!--統(tǒng)計(jì)文件行數(shù)-->
41 /etc/passwd
[root@centos01 ~]# wc -w /etc/passwd <!--統(tǒng)計(jì)文件中單詞個(gè)數(shù)-->
81 /etc/passwd
[root@centos01 ~]# wc -c /etc/passwd  <!--統(tǒng)計(jì)文件中字節(jié)數(shù)-->
2104 /etc/passwd
[root@centos01 ~]# wc /etc/passwd   
   <!--不加選項(xiàng)統(tǒng)計(jì)順序依次是43行,85個(gè)單詞,2223個(gè)字節(jié)-->
  43   85 2223 /etc/passwd
[root@centos01 ~]# find /etc -name "*.conf" | wc -l  
     <!--統(tǒng)計(jì)/etc下有多少個(gè)以*.conf為后綴的文件數(shù)量-->
437 

6、grep——檢索和過(guò)濾文件內(nèi)容

grep命令用于在文件中查找并顯示包含指定字符串的行?;菊Z(yǔ)法格式如下:
Linux如何用命令操作目錄和文件

grep命令常用選項(xiàng)如下:

  • -i:查找時(shí)忽略大小寫;

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

grep查找條件設(shè)置:

  • 要查找的字符串以雙引號(hào)括起來(lái);

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

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

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

應(yīng)用舉例:

[root@centos01 ~]# grep -i "SSHD" /etc/passwd  
          <!--查詢sshd用戶忽略大小寫顯示出來(lái)-->
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
[root@centos01 ~]# grep "ftp" /etc/passwd   
                <!--查詢ftp用戶顯示出來(lái)-->
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
[root@centos01 ~]# grep -v "^#" /etc/yum.conf | grep -v "^$"  
                <!--過(guò)濾/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如何用命令操作目錄和文件

應(yīng)用舉例:

<!--壓縮和解壓縮方法一(“-9”選項(xiàng)代表高壓縮)-->
[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

<!--壓縮和解壓縮方法二(不加選項(xiàng)進(jìn)行壓縮,解壓縮使用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如何用命令操作目錄和文件

應(yīng)用舉例:

<!--方法一-->
[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——?dú)w檔命令

tar命令制作歸檔文件、釋放歸檔文件?;菊Z(yǔ)法格式如下:
Linux如何用命令操作目錄和文件

tar命令常用選項(xiàng)如下:

  • -c:創(chuàng)建.tar格式的包文件;

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

  • -v:輸出詳細(xì)信息;

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

  • -p:打包時(shí)保留原始文件及目錄的權(quán)限;

  • -t:列表查看包內(nèi)的文件;

  • -C:解包時(shí)指定釋放的目標(biāo)文件夾;

  • -z:調(diào)用gzip程序進(jìn)行壓縮或解壓;

  • -j:調(diào)用bzip2程序進(jìn)行壓縮或解壓;

應(yīng)用舉例:

[root@centos01 ~]# tar zcvf yun.gz yun/ <!--使用tar命令調(diào)用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命令調(diào)用bzip將yun目錄數(shù)據(jù)進(jìn)行壓縮-->
[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命令壓縮與備份

選項(xiàng)和參數(shù):

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

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

  • bs:規(guī)劃的一個(gè)block(塊)的大小,若未指定則默認(rèn)是512Bytes(字節(jié));

  • count:多少塊的意思。

應(yīng)用舉例:

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

四、vi文本編輯器

1、文本編輯器的作用

創(chuàng)建或者修改文本文件,維護(hù)Linux系統(tǒng)中的各種配置文件。

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

  • vi:類Unix系統(tǒng)中默認(rèn)的文本編輯器;

  • vim:vi編輯器的增強(qiáng)版本,習(xí)慣上也成為vi;

3、vi編輯器的工作模式

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

4、命令模式的常用操作

1)光標(biāo)移動(dòng)

Linux如何用命令操作目錄和文件

2)復(fù)制、粘貼、刪除

Linux如何用命令操作目錄和文件

3)文件內(nèi)容查找

Linux如何用命令操作目錄和文件

4)撤銷編輯及保存退出

Linux如何用命令操作目錄和文件

5、末行模式的基本操作

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

Linux如何用命令操作目錄和文件

2)打開新文件或讀入其他文件內(nèi)容

Linux如何用命令操作目錄和文件

3)文件內(nèi)容替換

Linux如何用命令操作目錄和文件

看完上述內(nèi)容,你們對(duì)linux的常用命令有進(jìn)一步的了解嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI