這篇文章主要為大家分享Linux操作目錄和文件的常用命令。文中還介紹了linux的目錄結(jié)構(gòu)以及vi文本編輯器的使用方法,希望大家通過這篇文章能有所收獲。
Linux目錄結(jié)構(gòu)采用樹形目錄結(jié)構(gòu),包含根目錄和子目錄。
所有分區(qū)、目錄、文件等的位置起點,整個樹形目錄結(jié)構(gòu)中,使用獨立的一個“/”表示。
常見的子目錄如/root、/bin、/boot、/dev、/etc、/home、/var、/usr、/sbin。
cat用于一次性顯示文件全部內(nèi)容。基本語法格式如下:
應(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
more用于全屏模式分頁顯示文件內(nèi)容?;菊Z法格式如下:
交互操作方法:
按Enter鍵向下逐行滾動;
按空格鍵向下翻一屏;
- 按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%)
less命令的作用與more命令相同,但擴展功能更多?;菊Z法格式如下:
交互操作方法:
Page Up鍵:向上翻頁;
Page Down鍵:向下翻頁;
“/”鍵:查找關(guān)鍵內(nèi)容;
“n”:查找下一個關(guān)鍵內(nèi)容;
- “N”:查找上一個關(guān)鍵內(nèi)容;
其他功能與more命令基本類似;
head、tail命令的基本語法格式如下:
head:查看文件開頭的一部分內(nèi)容(默認為10行);
- tail:查看文件結(jié)尾的一部分內(nèi)容(默認為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
<!--動態(tài)跟蹤文件尾部內(nèi)容,便于實時監(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.
wc用于統(tǒng)計文件中的單詞數(shù)量、(Word Count)、行數(shù)、字節(jié)數(shù)等?;菊Z法格式如下:
wc常用選項如下:
-l:統(tǒng)計行數(shù);
-w:統(tǒng)計單詞個數(shù);
- -c:統(tǒng)計字節(jié)數(shù);
應(yīng)用舉例:
[root@centos01 ~]# wc -l /etc/passwd <!--統(tǒng)計文件行數(shù)-->
41 /etc/passwd
[root@centos01 ~]# wc -w /etc/passwd <!--統(tǒng)計文件中單詞個數(shù)-->
81 /etc/passwd
[root@centos01 ~]# wc -c /etc/passwd <!--統(tǒng)計文件中字節(jié)數(shù)-->
2104 /etc/passwd
[root@centos01 ~]# wc /etc/passwd
<!--不加選項統(tǒng)計順序依次是43行,85個單詞,2223個字節(jié)-->
43 85 2223 /etc/passwd
[root@centos01 ~]# find /etc -name "*.conf" | wc -l
<!--統(tǒng)計/etc下有多少個以*.conf為后綴的文件數(shù)量-->
437
grep命令用于在文件中查找并顯示包含指定字符串的行?;菊Z法格式如下:
grep命令常用選項如下:
-i:查找時忽略大小寫;
- -v:反轉(zhuǎn)查找,輸出與條件不相符的行;
grep查找條件設(shè)置:
要查找的字符串以雙引號括起來;
“^......”:表示查找以......開頭的字符串;
“......$”:表示查找以......結(jié)尾的字符串;
- “^$”:表示查找空行;
應(yīng)用舉例:
[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
應(yīng)用舉例:
<!--壓縮和解壓縮方法一(“-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
應(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
tar命令制作歸檔文件、釋放歸檔文件?;菊Z法格式如下:
tar命令常用選項如下:
-c:創(chuàng)建.tar格式的包文件;
-x:解開.tar格式的包文件;
-v:輸出詳細信息;
-f:表示使用歸檔文件;
-p:打包時保留原始文件及目錄的權(quán)限;
-t:列表查看包內(nèi)的文件;
-C:解包時指定釋放的目標文件夾;
-z:調(diào)用gzip程序進行壓縮或解壓;
- -j:調(diào)用bzip2程序進行壓縮或解壓;
應(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ù)進行壓縮-->
[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
選項和參數(shù):
if:input file(原文件)也可以是設(shè)備;
of:output file(備份后的文件)也可以是設(shè)備;
bs:規(guī)劃的一個block(塊)的大小,若未指定則默認是512Bytes(字節(jié));
- count:多少塊的意思。
應(yīng)用舉例:
[root@centos01 ~]# dd if=/dev/zero of=/usr/src/1.iso bs=30M count=10
<!--將/dev/zero文件中的信息復制到/usr/src目錄下創(chuàng)建一個1.iso的文件,一次30M,10次-->
記錄了10+0 的讀入
記錄了10+0 的寫出
314572800字節(jié)(315 MB)已復制,0.212995 秒,1.5 GB/秒
[root@centos01 ~]# cd /usr/src/
[root@centos01 src]# ls
1.iso debug kernels
創(chuàng)建或者修改文本文件,維護Linux系統(tǒng)中的各種配置文件。
vi:類Unix系統(tǒng)中默認的文本編輯器;
命令模式、輸入模式、末行模式。不同模式之間的切換如下:
看完上述內(nèi)容,你們對linux的常用命令有進一步的了解嗎?如果還想學到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀。
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)cdcxhl.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機、免備案服務(wù)器”等云主機租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。
當前文章:Linux如何用命令操作目錄和文件-創(chuàng)新互聯(lián)
瀏覽地址:http://jinyejixie.com/article44/heehe.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站改版、網(wǎng)站建設(shè)、App設(shè)計、App開發(fā)、企業(yè)網(wǎng)站制作、網(wǎng)站設(shè)計
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容