Linux文件系統中的文件和目錄被組織成一個單根倒置樹結構,文件系統從根目錄下開始,使用"/"表示,並且Linux文件系統(CentOS6.10默認是ext4,CentOS7.6默認是xfs)的文件名區分大小寫,可是有個須要注意的地方,若是CentOS系統上掛載了個U盤,而它的格式一般是VFAT,訪問這個U盤的內容不區分大小寫。所以是否區分大小寫,和文件系統有關,和操做系統無關。html
要想查看磁盤的文件系統類型,可使用lsblk -o +FSTYPE 命令查看node
CentOS7.6磁盤的文件系統查看linux
[root@centos7 ~]#lsblk -o +FSTYPE NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT FSTYPE sda 8:0 0 200G 0 disk ├─sda1 8:1 0 500M 0 part /boot xfs ├─sda2 8:2 0 50G 0 part / xfs ├─sda3 8:3 0 30G 0 part /data xfs ├─sda4 8:4 0 1K 0 part └─sda5 8:5 0 4G 0 part [SWAP] swap sr0 11:0 1 10G 0 rom /run/media/root/CentOS 7 x86_64 iso9660
CentOS6.10磁盤的文件系統查看git
[root@centOS6 cd]#lsblk -o +FSTYPE NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT FSTYPE sr0 11:0 1 3.7G 0 rom /media/CentOS_6.10_Final iso9660 sda 8:0 0 200G 0 disk ├─sda1 8:1 0 1G 0 part /boot ext4 ├─sda2 8:2 0 48.8G 0 part / ext4 ├─sda3 8:3 0 29.3G 0 part /data ext4 ├─sda4 8:4 0 1K 0 part └─sda5 8:5 0 3.9G 0 part [SWAP] swap
在Linux系統中,以.開頭的文件表示隱藏文件,例如以前修改過的.bash_history和.bashrc等等都是隱藏文件。
Linux文件系統對文件名的後綴沒有特殊的要求,而Windows文件系統的可執行程序的後綴必須是.exe或者.msi。shell
在Linux下的路徑分隔使用正斜線(/)分隔,而Linux文件系統的路徑主要有相對路徑和絕對路徑兩種,其中相對路徑是相對於當前路徑開始的路徑,而絕對路徑是從根路徑下開始,和路徑相關的還有兩個特殊的符號,其中..表示上一級目錄,而./表示當前目錄,相對路徑相比絕對路徑而言會更加靈活,在後期編寫腳本程序時應該優先使用相對路徑。vim
使用相對路徑查看/etc目錄下的motd文件centos
[root@centOS6 ~]#pwd #首先查看當前用戶所在的路徑 /root [root@centOS6 ~]#cat ../etc/motd 歡迎跟光磊一塊兒學習Linux系統運維與架構設計
使用絕對路徑查看/etc目錄下的motd文件緩存
[root@centOS6 ~]#cat /etc/motd 歡迎跟光磊一塊兒學習Linux系統運維與架構設計
使用相對路徑查看/etc目錄下的motd文件安全
[root@centOS6 ~]#cat ../etc/motd 歡迎跟光磊一塊兒學習Linux系統運維與架構設計
若是隻要路徑的文件或者文件夾,可使用basename獲取,若是隻想要獲取文件或者文件夾所在的路徑,可使用dirname獲取bash
[root@centOS6 ~]#basename /etc/sysconfig/network network [root@centOS6 ~]#dirname /etc/sysconfig/network /etc/sysconfig
在Linux文件系統中,每一個文件都有相應的數據,包含文件屬性數據(例如大小,修改時間,權限信息等等),也被稱爲元數據(metadata)以及文件存放的內容自己的數據兩部分組成,使用ll命令就能夠查看文件的屬性數據,以下命令輸出顯示。
[root@centOS6 ~]#ll /etc/profile -rw-r--r--. 1 root root 1841 Mar 22 2017 /etc/profile
在Linux文件系統中,不一樣類型的文件使用不一樣的顏色來區分,在/etc目錄下的DIR_COLORS文件中定義了各個類型的文件所對應的顏色,可使用vim /etc/DIR_COLORS命令查看,若是想要修改文件的顏色,只須要修改該文件便可。
[root@centos7 bin]#vim /etc/DIR_COLORS
而常見的文件類型以下列表所示
顏色對應的文件類型以下表格所示
顏色 | 文件類型 |
---|---|
藍色 | 目錄 |
綠色 | 可執行文件 |
紅色 | 壓縮文件 |
淺藍色 | 連接文件 |
灰色 | 其餘文件 |
一般運維人員打交道最多的是普通文件和目錄文件
Linux文件系統中文件名最長只能是255個字節之內,若是在建立文件時,長度超過255個字節會提示 File name too long,並且包含路徑在內的文件名最長爲4095個字節。
Linux系統的文件名不能使用空格,斜杆和NUL,也極不推薦使用命令選項做爲文件名。若是在工做中遇到了垃圾的文件沒法正常使用rm -f命令加上相對路徑刪除,只須要加上垃圾文件的絕對路徑就能夠刪除。
Linux文件系統的目錄結構和Windows大相徑庭,由文件系統層次結構標準組(Firesystem Hierarchy Standard Group )發佈的文件系統層次結構標準明確規定了Linux文件系統的各個目錄的結構,目前最新的版本是2004年1月29日發佈的2.3版本,經過瀏覽文件系統層次結構標準的pdf文件,能夠了解到Linux系統中每一個文件的做用,目錄實際上也是看成一種文件。
當咱們登陸系統以後,默認會在家目錄,例如普通用戶的家目錄通常是/home/USERNAME,而管理員用戶的家目錄是在/root目錄下,而這個/就是Linux系統的根目錄,相比Windows而言是以某個盤符(例如C盤或者D盤)做爲根目錄。
咱們可使用pwd命令查看登陸系統後用戶所在的目錄。
root用戶
[root@centOS6 ~]#pwd /root
普通用戶
[guanglei@centOS6 ~]$pwd /home/guanglei
而在根目錄下有些重要的目錄,這裏咱們使用tree命令來查看。
系統安裝之後,默認是不帶tree命令,須要咱們加載光盤鏡像後本身安裝,安裝以前須要確保系統鏡像已經被加載,以CentOS7.6爲例,以下圖所示
CentOS7.6系統可使用以下命令安裝tree
[root@centos7 media]#rpm -ivh /run/media/root/CentOS\ 7\ x86_64/Packages/tree-1.6.0-10.el7.x86_64.rpm
而CentOS6.10系統可使用以下命令安裝tree
[root@centOS6 ~]#rpm -ivh /media/CentOS_6.10_Final/Packages/tree-1.5.3-3.el6.x86_64.rpm
而後使用tree -L 1 / 命令就能夠查看到根目錄下的一級目錄
CentOS6.7根目錄下的一級子目錄以下命令輸出所示
[root@centOS6 ~]#tree -L 1 / / ├── bin ├── boot ├── data ├── dev ├── etc ├── home ├── lib ├── lib64 ├── lost+found ├── media ├── misc ├── mnt ├── net ├── opt ├── proc ├── root ├── sbin ├── selinux ├── srv ├── sys ├── tmp ├── usr └── var
CentOS7.6系統的根目錄下一級目錄以下命令輸出顯示
[root@centos7 ~]#tree -L 1 / / ├── bin -> usr/bin ├── boot ├── data ├── dev ├── etc ├── home ├── lib -> usr/lib ├── lib64 -> usr/lib64 ├── media ├── mnt ├── opt ├── proc ├── root ├── run ├── sbin -> usr/sbin ├── srv ├── sys ├── tmp ├── usr └── var 20 directories, 0 files
二者根目錄的一級子目錄直接的對比,以下圖所示
接下來就介紹根目錄下的一級子目錄及其做用,以下表格所示,其中標記 *的是FHS規定Linux系統中必須具有的目錄。
目錄名稱 | 主要做用 |
---|---|
/bin * | 供全部用戶使用的基本命令;不能關聯至獨立分區,OS啓動即會用到的程序 |
/boot * | 引導文件存放目錄,內核文件(vmlinuz)、引導加載器(bootloader, grub)都存放於此目錄,須要獨立分區,若是損壞,系統沒法啓動 |
/dev * | 設備文件以及特殊文件存儲位置,例如塊設備以及字符設備 |
/sbin * | 管理類(針對root用戶)的基本命令;不能關聯至獨立分區, OS啓動即會用到的程序 |
/etc * | 配置文件目錄,存放應程序的相關配置 |
/lib * | 啓動時程序依賴的基本共享庫文件以及內核模塊文件(/lib/modules) |
/home/USERNAME | 普通用戶的家目錄 |
/root | 管理員的家目錄 |
/media * | 便攜式移動設備掛載點 |
/mnt * | 臨時文件系統掛載點 |
/var * | 存放系統和軟件運行的日誌信息 |
/opt * | 第三方應用程序的安裝位置 |
/usr * | 存放應用程序和文件 |
/proc | 用於輸出內核與進程信息相關的虛擬文件系統 |
/sys | 用於輸出當前系統上硬件設備相關信息虛擬文件系統 |
/srv * | 系統上運行的服務用到的數據 |
/tmp * | 臨時文件存儲位置 |
/selinux | security enhanced Linux, selinux相關的安全策略等信息的存儲位置 |
/boot目錄
這裏使用ls -al /boot查看/boot目錄下的文件列表,其中 vmlinuz-3.10.0-957.el7.x86_64就是Linux的內核,CentOS7.x的內核是3.10版本,以下命令執行輸出結果顯示。
[root@centos7 ~]#ls -al /boot total 128912 dr-xr-xr-x. 5 root root 4096 Jan 10 10:10 . dr-xr-xr-x. 18 root root 236 Jan 10 10:08 .. -rw-r--r--. 1 root root 151918 Nov 9 07:43 config-3.10.0-957.el7.x86_64 drwx------. 3 root root 17 Nov 9 08:46 efi drwxr-xr-x. 2 root root 27 Jan 10 10:03 grub drwx------. 5 root root 97 Jan 10 10:09 grub2 -rw-------. 1 root root 74000668 Jan 10 10:07 initramfs-0-rescue-40316785f67f494a9a258e83fd0d87a4.img -rw-------. 1 root root 29363228 Jan 10 10:09 initramfs-3.10.0-957.el7.x86_64.img -rw-------. 1 root root 11324650 Jan 10 10:10 initramfs-3.10.0-957.el7.x86_64kdump.img -rw-r--r--. 1 root root 314036 Nov 9 07:43 symvers-3.10.0-957.el7.x86_64.gz -rw-------. 1 root root 3543471 Nov 9 07:43 System.map-3.10.0-957.el7.x86_64 -rwxr-xr-x. 1 root root 6639904 Jan 10 10:07 vmlinuz-0-rescue-40316785f67f494a9a258e83fd0d87a4 -rwxr-xr-x. 1 root root 6639904 Nov 9 07:43 vmlinuz-3.10.0-957.el7.x86_64 -rw-r--r--. 1 root root 166 Nov 9 07:43 .vmlinuz-3.10.0-957.el7.x86_64.hmac
/dev目錄
這裏使用ll命令查看/dev下的設備列表
主要設備分爲塊設備和字符設備
其中塊設備特色是物理存在的,支持隨機訪問,支持緩存。
字符設備是不帶緩存,是邏輯的概念,不支持隨機訪問,也就是順序訪問。
並且塊設備和字符設備都沒有大小的屬性,只有設備的類別以及編號。
查看/dev下的磁盤分區
[root@centos7 ~]#ll /dev/sd* brw-rw----. 1 root disk 8, 0 Jan 29 23:05 /dev/sda brw-rw----. 1 root disk 8, 1 Jan 29 23:05 /dev/sda1 brw-rw----. 1 root disk 8, 2 Jan 29 23:05 /dev/sda2 brw-rw----. 1 root disk 8, 3 Jan 29 23:05 /dev/sda3 brw-rw----. 1 root disk 8, 4 Jan 29 23:05 /dev/sda4 brw-rw----. 1 root disk 8, 5 Jan 29 23:05 /dev/sda5
/etc目錄
存放應用,服務的相關配置文件,後期運維工做會常常訪問該目錄來修改相應的配置文件(例如以前修改過的/etc/profile /etc/gdm/custom.conf /etc/profile.d/prompt.sh
),這裏咱們要養成一個良好的運維習慣,就是生產環境上在修改配置文件以前先備份,防止某些誤操做形成沒法挽回的損失。
/lib目錄
這裏使用ldd命令查看應用程序所依賴的系統庫文件。
Linux系統的命令都會依賴/lib或者/lib64(CentOS7都是64位)路徑下的系統庫。
咱們可使用ldd命令跟上命令的路徑就能夠查看命令所依賴的庫。
[root@centos7 ~]#which ls #查看ls命令的路徑 alias ls='ls --color=auto' /usr/bin/ls [root@centos7 ~]#ldd /usr/bin/ls linux-vdso.so.1 => (0x00007fffe2927000) libselinux.so.1 => /lib64/libselinux.so.1 (0x00007f3daeb99000) libcap.so.2 => /lib64/libcap.so.2 (0x00007f3dae994000) libacl.so.1 => /lib64/libacl.so.1 (0x00007f3dae78b000) libc.so.6 => /lib64/libc.so.6 (0x00007f3dae3be000) libpcre.so.1 => /lib64/libpcre.so.1 (0x00007f3dae15c000) libdl.so.2 => /lib64/libdl.so.2 (0x00007f3dadf58000) /lib64/ld-linux-x86-64.so.2 (0x00007f3daedc0000) libattr.so.1 => /lib64/libattr.so.1 (0x00007f3dadd53000) libpthread.so.0 => /lib64/libpthread.so.0 (0x00007f3dadb37000) [root@centos7 ~]#which --skip-alias hostname /usr/bin/hostname [root@centos7 ~]#ldd /usr/bin/hostname #查看hostname依賴的系統庫 linux-vdso.so.1 => (0x00007ffd21f8d000) libnsl.so.1 => /lib64/libnsl.so.1 (0x00007f360227d000) libc.so.6 => /lib64/libc.so.6 (0x00007f3601eb0000) /lib64/ld-linux-x86-64.so.2 (0x00007f3602497000)
/media和/mnt
/media目錄是當用戶登陸系統以後會自動掛載配置的鏡像文件,而/mnt一般是管理員手動輸入命令掛載。
/bin和/sbin
這裏以CentOS6.10中的普通命令ls和管理員命令init爲例說明/bin和/sbin目錄的做用
/bin目錄下存放的是普通用戶存放的命令,例如ls
/sbin目錄下存放的是管理員存放的命令,例如init
[root@centOS6 ~]#which --skip-alias ls /bin/ls [root@centOS6 ~]#which init /sbin/init
/usr目錄
/usr用於存放系統中的應用程序和文件,若是在安裝系統時選擇了不少軟件包,那麼這些軟件包默認是安裝在/usr路徑下,日常安裝的一些軟件默認也是在/usr路徑下,所以這個目錄通常會比較大,可使用du命令查看目錄對應的磁盤空間,以下命令輸出結果顯示除了光盤鏡像之外,/usr佔據的磁盤空間是最大的。
[root@centOS6 ~]#du -sh /* 7.7M /bin 38M /boot 20K /data 312K /dev 41M /etc 214M /home 207M /lib 28M /lib64 16K /lost+found 3.8G /media 0 /misc 8.0K /mnt 0 /net 8.0K /opt du: cannot access `/proc/4181/task/4181/fd/4': No such file or directory du: cannot access `/proc/4181/task/4181/fdinfo/4': No such file or directory du: cannot access `/proc/4181/fd/4': No such file or directory du: cannot access `/proc/4181/fdinfo/4': No such file or directory 0 /proc 1.2M /root 17M /sbin 0 /selinux 4.0K /srv 0 /sys 780K /tmp 3.1G /usr 83M /var
/proc目錄
/proc目錄是一個虛擬目錄,目錄中全部的信息都是內存的映射,經過這個虛擬的內內存映射目錄,能夠獲取CPU,內存,硬盤信息,/proc目錄存放與內存中,而不是磁盤。
若是想要查看CPU信息,可使用cat cpuinfo命令
[guanglei@centOS6 /]$cat /proc/cpuinfo #查看CPU信息 [guanglei@centOS6 /]$ll /proc/cpuinfo #查看cpuinfo文件大小是0,由於存在內存中 -r--r--r--. 1 root root 0 Jan 15 23:48 /proc/cpuinfo
若是想要查看內存信息,可使用cat meminfo命令查看,以下命令輸出所示
[guanglei@centOS6 /]$cat /proc/meminfo MemTotal: 8045264 kB MemFree: 6520032 kB Buffers: 83236 kB Cached: 830312 kB SwapCached: 0 kB Active: 647804 kB Inactive: 429556 kB Active(anon): 163912 kB Inactive(anon): 4112 kB Active(file): 483892 kB Inactive(file): 425444 kB Unevictable: 0 kB Mlocked: 0 kB SwapTotal: 4095996 kB SwapFree: 4095996 kB Dirty: 140 kB Writeback: 0 kB AnonPages: 163808 kB Mapped: 66952 kB Shmem: 4220 kB Slab: 310788 kB SReclaimable: 222452 kB SUnreclaim: 88336 kB KernelStack: 7920 kB PageTables: 28240 kB NFS_Unstable: 0 kB Bounce: 0 kB WritebackTmp: 0 kB CommitLimit: 8118628 kB Committed_AS: 853068 kB VmallocTotal: 34359738367 kB VmallocUsed: 198680 kB VmallocChunk: 34359518624 kB HardwareCorrupted: 0 kB AnonHugePages: 34816 kB HugePages_Total: 0 HugePages_Free: 0 HugePages_Rsvd: 0 HugePages_Surp: 0 Hugepagesize: 2048 kB DirectMap4k: 8192 kB DirectMap2M: 2088960 kB DirectMap1G: 6291456 kB
若是想要查看磁盤分區信息,可使用cat /proc/partitions 命令,以下命令輸出所示
當前的磁盤分區狀況。
[guanglei@centOS6 /]$cat /proc/partitions major minor #blocks name 8 0 209715200 sda 8 1 1048576 sda1 8 2 51200000 sda2 8 3 4096000 sda3 8 4 1 sda4 8 5 153368576 sda5
/sys目錄
/sys是用於存放系統硬件信息
[root@centos7 ~]#ls -al /sys total 0 dr-xr-xr-x. 13 root root 0 Jan 29 23:05 . dr-xr-xr-x. 18 root root 236 Jan 25 12:52 .. drwxr-xr-x. 2 root root 0 Jan 29 23:05 block drwxr-xr-x. 34 root root 0 Jan 29 23:05 bus drwxr-xr-x. 55 root root 0 Jan 29 23:05 class drwxr-xr-x. 4 root root 0 Jan 29 23:05 dev drwxr-xr-x. 16 root root 0 Jan 29 23:05 devices drwxr-xr-x. 6 root root 0 Jan 29 23:05 firmware drwxr-xr-x. 8 root root 0 Jan 29 23:05 fs drwxr-xr-x. 2 root root 0 Jan 29 23:05 hypervisor drwxr-xr-x. 10 root root 0 Jan 29 23:05 kernel drwxr-xr-x. 164 root root 0 Jan 29 23:05 module drwxr-xr-x. 2 root root 0 Jan 29 23:05 power
這裏使用/sys實現動態掛載硬盤,首先使用lsblk命令查看當前的磁盤以及分區信息,目前只有sda一塊硬盤。
[guanglei@centOS6 /]$lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sr0 11:0 1 55M 0 rom sda 8:0 0 200G 0 disk ├─sda1 8:1 0 1G 0 part /boot ├─sda2 8:2 0 48.8G 0 part / ├─sda3 8:3 0 3.9G 0 part [SWAP] ├─sda4 8:4 0 1K 0 part └─sda5 8:5 0 146.3G 0 part /data
咱們能夠在VMware Work Station中給CentOS6.10添加一個磁盤,而後使用以下命令讓系統自動識別,該命令會觸發磁盤掃描,讓系統識別新增的磁盤。
[root@centOS6 ~]#echo '- - -'> /sys/class/scsi_host/host2/scan
再次使用lsblk命令查看新增長的磁盤信息,新增了一個100G的磁盤,默認未分區。
[root@centOS6 ~]#lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sr0 11:0 1 3.7G 0 rom /media/CentOS_6.10_Final sda 8:0 0 200G 0 disk ├─sda1 8:1 0 1G 0 part /boot ├─sda2 8:2 0 48.8G 0 part / ├─sda3 8:3 0 3.9G 0 part [SWAP] ├─sda4 8:4 0 1K 0 part └─sda5 8:5 0 146.3G 0 part /data sdb 8:16 0 100G 0 disk
/misc
首先使用lsblk查看光盤的掛在狀況
當用戶在圖形界面登陸系統後,光盤會自動掛在到/run/media/root目錄下。
若是開機時進入的是字符界面,則光盤不會自動掛載。
[root@centos7 ~]#lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sda 8:0 0 200G 0 disk ├─sda1 8:1 0 500M 0 part /boot ├─sda2 8:2 0 50G 0 part / ├─sda3 8:3 0 30G 0 part /data ├─sda4 8:4 0 1K 0 part └─sda5 8:5 0 4G 0 part [SWAP] sr0 11:0 1 10G 0 rom /run/media/root/CentOS 7 x86_64
針對上述狀況,咱們能夠藉助/misc目錄實現光盤掛載。
在CentOS7.6中,首先咱們使用yum install -y autofs 安裝autofs服務
而後使用systemctl start autofs 實現開機啓動autofs服務。
[root@centos7 ~]#yum install -y autofs #安裝autofs服務 [root@centos7 ~]#systemctl start autofs #開機啓動autofs服務
而後使用ll /misc/cd 查看該目錄下的內容
[root@centos7 ~]#ll /misc/cd total 1656 -rw-rw-r--. 1 root root 14 Nov 26 00:01 CentOS_BuildTag drwxr-xr-x. 3 root root 2048 Nov 26 00:20 EFI -rw-rw-r--. 1 root root 227 Aug 30 2017 EULA -rw-rw-r--. 1 root root 18009 Dec 10 2015 GPL drwxr-xr-x. 3 root root 2048 Nov 26 00:21 images drwxr-xr-x. 2 root root 2048 Nov 26 00:20 isolinux drwxr-xr-x. 2 root root 2048 Nov 26 00:20 LiveOS drwxrwxr-x. 2 root root 1656832 Nov 25 23:58 Packages drwxrwxr-x. 2 root root 4096 Nov 26 22:21 repodata -rw-rw-r--. 1 root root 1690 Dec 10 2015 RPM-GPG-KEY-CentOS-7 -rw-rw-r--. 1 root root 1690 Dec 10 2015 RPM-GPG-KEY-CentOS-Testing-7 -r--r--r--. 1 root root 2883 Nov 26 22:22 TRANS.TBL
而後就能夠經過訪問/misc/cd/Packages/安裝指定的軟件包了
[root@centos7 ~]#rpm -ivh /misc/cd/Packages/lrzsz-0.12.20-36.el7.x86_64.rpm #安裝lrzsz實現文件上傳下載
在CentOS7.x之後,以下列表的目錄都已經被合併了。
以下命令輸出顯示,/bin軟鏈的形式指向/usr/bin
[root@centos7 ~]#ll -d /bin /usr/bin lrwxrwxrwx. 1 root root 7 Jan 10 10:02 /bin -> usr/bin dr-xr-xr-x. 2 root root 49152 Jan 15 22:27 /usr/bin
Linux應用程序由二進制程序以及依賴的庫文件和配置文件以及幫助文件組成。
一般二進制程序位於/bin,/sbin,/usr/bin,/usr/local/bin以及/usr/local/sbin目錄下
而庫文件位於/lib,/lib64,/usr/lib,/usr/lib64,/usr/local/lib,/usr/local/lib64目錄下
配置文件位於/etc/及其子目錄以及 /usr/local/etc下
幫助文件位於/usr/share/man,/usr/share/doc,/user/local/share/man,/usr/local/share/doc目錄下
功能說明:pwd命令是顯示當前工做目錄,在平常運維工做中,咱們會在不一樣的目錄之間進行切換,當執行pwd命令後能夠馬上得知當前工做目錄的絕對路徑。
經常使用選項:
應用案例:
[root@centos7 bin]#pwd #顯示軟連接路徑 /bin [root@centos7 bin]#pwd -L # pwd默認就是顯示軟鏈接路徑 /bin [root@centos7 bin]#pwd -P # 顯示真實物理路徑 /usr/bin
功能說明:cd(change directory)命令用於切換目錄,在平常運維場景中,常用cd命令切換到不一樣的目錄下執行不一樣的維護任務。
cd是Shell自帶的內置命令,在切換目錄時可使用TAB實現路徑補全。
[root@centos7 bin]#type cd cd is a shell builtin
在切換目錄時,還有一些特殊符號進入相關的目錄
符號名稱 | 表示目錄 |
---|---|
~ | 家目錄 |
~guanglei | 進入到用戶給guanglei的家目錄 |
- | 上一次目錄 |
.. | 上一級目錄 |
應用案例:分別使用cd切換到管理員root用戶家目錄和普通用戶guanglei的家目錄
[root@centos7 ~]#cd ~ [root@centos7 ~]#pwd /root [root@centos7 ~]#cd ~guanglei [root@centos7 guanglei]#pwd /home/guanglei
應用案例:使用cd實現切換到上級目錄
[root@centos7 guanglei]#cd ~ [root@centos7 ~]#pwd /root [root@centos7 ~]#cd .. [root@centos7 /]#pwd /
其中當前目錄保存在$PWD環境變量中,而上一次目錄的路徑保存在$OLDPWD環境變量中,能夠分別使用echo 查看它們的值
[root@centos7 bin]#echo $PWD /bin [root@centos7 bin]#echo $OLDPWD /root
若是某個目錄是軟鏈接,例如在CentOS7.6中,/bin目錄就是軟連接目錄,cd默認切換的目錄是軟連接,而若是想要切換到真實的目錄,能夠經過-P選項實現。
[root@centos7 bin]#cd /bin [root@centos7 bin]#pwd /bin [root@centos7 bin]#cd -P /bin [root@centos7 bin]#pwd /usr/bin
若是想要獲取一個文件的目錄名稱,可使用dirname加上文件的絕對路徑實現
[root@centos7 bin]#dirname /etc/sysconfig/network-scripts/ifcfg-ens33 /etc/sysconfig/network-scripts
若是想要獲取一個目錄下的文件名,可使用basename加上文件的絕對路徑實現
[root@centos7 bin]#basename /etc/sysconfig/network-scripts/ifcfg-ens33 ifcfg-ens33
功能說明:ls命令用於查看文件或者目錄列表
經常使用選項:
選項名稱 | 功能描述 |
---|---|
-a | 顯示目錄下的全部文件,包含以.開頭的隱藏文件 |
-d | 當遇到目錄時,列出目錄自己而非目錄的文件,而且不跟隨符號連接 |
-F | 在條目後加上文件類型的指示符號 |
-h | 以人類可讀的信息顯示文件或者目錄的大小 |
-i | 顯示文件節點編號 |
-l | 顯示文件詳細信息(包括文件的類型,權限,大小,所屬用戶,所屬組別,文件內容最後一次修改時間,所屬用戶,文件類型),等價於ll |
-r | 按照字典順序反向排序 |
-R | 遞歸顯示文件夾(包含子文件夾的內容) |
-S | 按照文件從大到小排序 |
-t | 根據最後的修改時間(mtime)排序,默認是以文件名稱排序 |
-u | 根據最後的訪問時間(atime)排序 |
-U | 按照文件生成的前後順序存放 |
-c | 根據最後的元數據修改時間(ctime)排序 |
-X | 按照文件的後綴排序 |
-1 | 按照行顯示目錄,默認按照數字,字母排序 |
若是先要了解ls的更多選項,可使用ls --help查閱。
使用ls查看文件列表時,默認使用的是別名,可使用type查看ls命令的類型
[root@centos7 ~]#type ls ls is aliased to `ls --color=auto'
,而別名的ls默認會給不一樣的文件類型增長顏色以示區分
[root@centos7 ~]#ls anaconda-ks.cfg Desktop Documents Downloads helloworld.c initial-setup-ks.cfg Music Pictures Public Templates Videos
若是想要使用ls的原始命令,只須要在命令加上''便可
[root@centos7 ~]#'ls' anaconda-ks.cfg Desktop Documents Downloads helloworld.c initial-setup-ks.cfg Music Pictures Public Templates Videos
若是想要查看指定目錄下的全部文件,包含隱藏文件可使用-a選項實現
[root@centos7 ~]#ls -a . .bash_history .bashrc .cshrc Documents helloworld.c .local Public Videos .. .bash_logout .cache .dbus Downloads .ICEauthority Music .tcshrc .viminfo anaconda-ks.cfg .bash_profile .config Desktop .esd_auth initial-setup-ks.cfg Pictures Templates
ls中的-A選項,不會顯示.和..
[root@centos7 ~]#ls -a . .bash_history .bashrc .cshrc Documents helloworld.c .local Public Videos .. .bash_logout .cache .dbus Downloads .ICEauthority Music .tcshrc .viminfo anaconda-ks.cfg .bash_profile .config Desktop .esd_auth initial-setup-ks.cfg Pictures Templates
若是想要獲取文件的屬性信息,可使用-l選項實現,也可使用ll代替
以文件anaconda-ks.cfg爲例,其中-表示該文件爲普通文本文件。
Jan 25 12:53 表示該文件內容的最後修改日期(mtime)
root表示文件所屬的用戶
root表示文件所屬的組
[root@centos7 ~]#ls -l total 12 -rw-------. 1 root root 1741 Jan 25 12:53 anaconda-ks.cfg drwxr-xr-x. 2 root root 6 Jan 29 15:09 Desktop drwxr-xr-x. 2 root root 6 Jan 29 15:09 Documents drwxr-xr-x. 2 root root 6 Jan 29 15:09 Downloads -rw-r--r--. 1 root root 97 Jan 29 15:39 helloworld.c -rw-r--r--. 1 root root 1772 Jan 25 12:56 initial-setup-ks.cfg drwxr-xr-x. 2 root root 6 Jan 29 15:09 Music drwxr-xr-x. 2 root root 6 Jan 29 15:09 Pictures drwxr-xr-x. 2 root root 6 Jan 29 15:09 Public drwxr-xr-x. 2 root root 6 Jan 29 15:09 Templates drwxr-xr-x. 2 root root 6 Jan 29 15:09 Videos
若是修改文件的詳細信息(例如這裏修改文件的全部者),文件的修改時間是不會發生變化的
[root@centos7 ~]#ll total 12 -rw-------. 1 root root 1741 Jan 25 12:53 anaconda-ks.cfg drwxr-xr-x. 2 root root 6 Jan 29 15:09 Desktop drwxr-xr-x. 2 root root 6 Jan 29 15:09 Documents drwxr-xr-x. 2 root root 6 Jan 29 15:09 Downloads -rw-r--r--. 1 guanglei root 97 Jan 29 15:39 helloworld.c -rw-r--r--. 1 root root 1772 Jan 25 12:56 initial-setup-ks.cfg drwxr-xr-x. 2 root root 6 Jan 29 15:09 Music drwxr-xr-x. 2 root root 6 Jan 29 15:09 Pictures drwxr-xr-x. 2 root root 6 Jan 29 15:09 Public drwxr-xr-x. 2 root root 6 Jan 29 15:09 Templates drwxr-xr-x. 2 root root 6 Jan 29 15:09 Videos [root@centos7 ~]#chown guanglei helloworld.c #修改文件的所屬用戶爲guanglei [root@centos7 ~]#ll total 12 -rw-------. 1 root root 1741 Jan 25 12:53 anaconda-ks.cfg drwxr-xr-x. 2 root root 6 Jan 29 15:09 Desktop drwxr-xr-x. 2 root root 6 Jan 29 15:09 Documents drwxr-xr-x. 2 root root 6 Jan 29 15:09 Downloads -rw-r--r--. 1 guanglei root 97 Jan 29 15:39 helloworld.c -rw-r--r--. 1 root root 1772 Jan 25 12:56 initial-setup-ks.cfg drwxr-xr-x. 2 root root 6 Jan 29 15:09 Music drwxr-xr-x. 2 root root 6 Jan 29 15:09 Pictures drwxr-xr-x. 2 root root 6 Jan 29 15:09 Public drwxr-xr-x. 2 root root 6 Jan 29 15:09 Templates drwxr-xr-x. 2 root root 6 Jan 29 15:09 Videos
而文件的元數據修改時間使用ctime來記錄,若是想要查看文件的元數據修改時間,可使用ll --time=ctime實現
[root@centos7 ~]#ll --time=ctime helloworld.c -rw-r--r--. 1 guanglei root 97 Feb 12 10:45 helloworld.c
這裏經過重命名文件,實現修改文件的元數據,則ctime會發生改變
[root@centos7 ~]#ll --time=ctime helloworld.c -rw-r--r--. 1 guanglei root 97 Feb 12 10:45 helloworld.c [root@centos7 ~]#mv helloworld.c hello.c [root@centos7 ~]#ll --time=ctime hello.c -rw-r--r--. 1 guanglei root 97 Feb 12 10:50 hello.c
當讀取文件時,文件的訪問時間(atime)會發生改變
[root@centos7 ~]#ll --time=atime anaconda-ks.cfg -rw-------. 1 root root 1741 Jan 25 12:55 anaconda-ks.cfg [root@centos7 ~]#cat anaconda-ks.cfg [root@centos7 ~]#ll --time=atime anaconda-ks.cfg -rw-------. 1 root root 1741 Feb 12 10:57 anaconda-ks.cfg
atime更新的條件
若是想要查看一個文件的atime,ctime和mtime,可使用stat命令查看
[root@centos7 ~]#stat hello.c File: ‘hello.c’ Size: 97 Blocks: 8 IO Block: 4096 regular file Device: 802h/2050d Inode: 100922353 Links: 1 Access: (0644/-rw-r--r--) Uid: ( 1000/guanglei) Gid: ( 0/ root) Context: unconfined_u:object_r:admin_home_t:s0 Access: 2019-02-12 10:52:15.094891199 +0800 Modify: 2019-01-29 15:39:31.969925223 +0800 Change: 2019-02-12 10:50:00.662896712 +0800 Birth: -
實際上記錄atime沒有太大的意義,還增長了磁盤IO,後期在網站運維時應該要考慮屏蔽atime。
在使用ls查看文件列表時,不會直接看到目錄下的子目錄及其文件,若是想要查看,能夠經過-R遞歸查看,以下命令輸出顯示。
[root@centos7 ~]#ls -R /boot
默認的ls是按照字典順序(數字,字母)默認升序排序,若是想要倒序排序,可使用-r選項實現,以下命令輸出顯示。
[root@centos7 ~]#ls -r Videos Templates Public Pictures Music initial-setup-ks.cfg hello.c Downloads Documents Desktop anaconda-ks.cfg
ls -l能夠查看文件的元數據信息,可是若是想要查看指定目錄的元數據信息,能夠經過-d選項實現,以下命令輸出顯示。
[root@centos7 ~]#ls -ld Desktop/ drwxr-xr-x. 2 root root 6 Jan 29 15:09 Desktop/
ls查看文件內容的時候,默認是橫着顯示的,若是想要按照每行顯示,可使用-1實現,以下命令輸出顯示了二者的對比。
[root@centos7 ~]#ls anaconda-ks.cfg Desktop Documents Downloads hello.c initial-setup-ks.cfg Music Pictures Public Templates Videos [root@centos7 ~]#ls -1 anaconda-ks.cfg Desktop Documents Downloads hello.c initial-setup-ks.cfg Music Pictures Public Templates Videos
若是想要在查看文件列表時,按照從大到小排序,可使用-S選項實現
[root@centos7 ~]#ls -Sl total 12 -rw-r--r--. 1 root root 1772 Jan 25 12:56 initial-setup-ks.cfg -rw-------. 1 root root 1741 Jan 25 12:53 anaconda-ks.cfg -rw-r--r--. 1 guanglei root 97 Jan 29 15:39 hello.c drwxr-xr-x. 2 root root 6 Jan 29 15:09 Desktop drwxr-xr-x. 2 root root 6 Jan 29 15:09 Documents drwxr-xr-x. 2 root root 6 Jan 29 15:09 Downloads drwxr-xr-x. 2 root root 6 Jan 29 15:09 Music drwxr-xr-x. 2 root root 6 Jan 29 15:09 Pictures drwxr-xr-x. 2 root root 6 Jan 29 15:09 Public drwxr-xr-x. 2 root root 6 Jan 29 15:09 Templates drwxr-xr-x. 2 root root 6 Jan 29 15:09 Videos
若是隻想要查看目錄下的文件夾,不會遞歸顯示,可使用ls -d */ 實現
[root@centos7 ~]#ls -d */ Desktop/ Documents/ Downloads/ Music/ Pictures/ Public/ Templates/ Videos/
ls查看文件大小時默認是以字節的單位顯示,若是想要計算成KB,MG或者GB,可使用-h選項實現
[root@centos7 ~]#cd /etc/ [root@centos7 etc]#ls -lh
Linux提供了諸多的文件通配符來實現對文件的管理,經常使用的通配符以下表格所示
通配符名稱 | 功能 |
---|---|
* | 匹配零個或者多個字符 |
? | 匹配任意單個字符 |
~ | 當前用戶家目錄 |
~guanglei | 用戶guanglei家目錄 |
~+ | 當前工做目錄 |
~- | 前一個工做目錄 |
[0-9] | 匹配數字範圍 |
[a-z] | 匹配字母範圍 |
[A-Z] | 匹配字母範圍 |
[wang] | 匹配列表中的任意字符 |
[^wang] | 匹配列表中的全部字符的之外字符 |
應用案例:查看以.cfg結尾的文件
[root@centos7 ~]#ls *.cfg anaconda-ks.cfg initial-setup-ks.cfg
應用案例:查看三個字符的文本文件
字母和英文都是一個字符
[root@centos7 ~]#cd /tmp [root@centos7 tmp]#touch abc.txt 劉光磊.txt [root@centos7 tmp]#ll ???.txt -rw-r--r--. 1 root root 0 Feb 16 10:05 abc.txt -rw-r--r--. 1 root root 0 Feb 16 10:05 劉光磊.txt
應用案例: 查看當前用戶家目錄文件列表
[root@centos7 tmp]#ls ~ anaconda-ks.cfg Documents hello.c Music Public test.sh Desktop Downloads initial-setup-ks.cfg Pictures Templates Videos
應用案例:查看當前工做目錄文件列表
[root@centos7 tmp]#ls ~+ abc.txt ssh-54so54gWt0i3 ssh-5a9O0v5VuZjN systemd-private-6d0a7a093d3747a7a364bdb0fcde2871-colord.service-OMvvKs systemd-private-6d0a7a093d3747a7a364bdb0fcde2871-fwupd.service-7cIBY8 systemd-private-6d0a7a093d3747a7a364bdb0fcde2871-rtkit-daemon.service-9ntYps systemd-private-aa581b2066814a7abb99227727ade380-bolt.service-n8nqET systemd-private-aa581b2066814a7abb99227727ade380-colord.service-Gxx3OS systemd-private-aa581b2066814a7abb99227727ade380-cups.service-v2ZAhp systemd-private-aa581b2066814a7abb99227727ade380-fwupd.service-gRLg43 systemd-private-aa581b2066814a7abb99227727ade380-rtkit-daemon.service-OzO7BR tracker-extract-files.0 vmware-root_9047-4146505294 vmware-root_9064-3100720561 yum_save_tx.2019-02-11.10-12.oVUsni.yumtx yum_save_tx.2019-02-12.10-04.J3Asgo.yumtx yum_save_tx.2019-02-13.09-48.Ddee4t.yumtx yum_save_tx.2019-02-14.09-44.KRg3qP.yumtx yum_save_tx.2019-02-16.09-37.3vpThE.yumtx 劉光磊.txt
應用案例:查看上一個工做目錄文件列表
[root@centos7 tmp]#ls ~- anaconda-ks.cfg Documents hello.c Music Public test.sh Desktop Downloads initial-setup-ks.cfg Pictures Templates Videos
應用案例:按照數字通配符匹配文件
首先建立五個文件 file1,file2,file3,file4,file5
[root@centos7 tmp]#touch file{1..5} [root@centos7 tmp]#ll file* -rw-r--r--. 1 root root 0 Feb 16 10:08 file1 -rw-r--r--. 1 root root 0 Feb 16 10:08 file2 -rw-r--r--. 1 root root 0 Feb 16 10:08 file3 -rw-r--r--. 1 root root 0 Feb 16 10:08 file4 -rw-r--r--. 1 root root 0 Feb 16 10:08 file5
而後查找file1-file5文件列表
[root@centos7 tmp]#ll file[1-5] -rw-r--r--. 1 root root 0 Feb 16 10:08 file1 -rw-r--r--. 1 root root 0 Feb 16 10:08 file2 -rw-r--r--. 1 root root 0 Feb 16 10:08 file3 -rw-r--r--. 1 root root 0 Feb 16 10:08 file4 -rw-r--r--. 1 root root 0 Feb 16 10:08 file5
查找指定名字的文件列表
[root@centos7 tmp]#ll file[1,3,5] -rw-r--r--. 1 root root 0 Feb 16 10:08 file1 -rw-r--r--. 1 root root 0 Feb 16 10:08 file3 -rw-r--r--. 1 root root 0 Feb 16 10:08 file5
應用案例:按照字母通配符查看文件列表
首先建立測試數據 filea,fileb,filec,filed,filee,filef
[root@centos7 tmp]#touch file{a..f}
而後使用字母通配符查看文件列表
[root@centos7 tmp]#ll file[af] -rw-r--r--. 1 root root 0 Feb 16 10:12 filea -rw-r--r--. 1 root root 0 Feb 16 10:12 fileb -rw-r--r--. 1 root root 0 Feb 16 10:12 filec -rw-r--r--. 1 root root 0 Feb 16 10:12 filed -rw-r--r--. 1 root root 0 Feb 16 10:12 filee -rw-r--r--. 1 root root 0 Feb 16 10:12 filef
應用案例:數字和字母通配符的綜合使用
[root@centos7 tmp]#ll file[a-zA-Z0-9] -rw-r--r--. 1 root root 0 Feb 16 10:08 file1 -rw-r--r--. 1 root root 0 Feb 16 10:08 file2 -rw-r--r--. 1 root root 0 Feb 16 10:08 file3 -rw-r--r--. 1 root root 0 Feb 16 10:08 file4 -rw-r--r--. 1 root root 0 Feb 16 10:08 file5 -rw-r--r--. 1 root root 0 Feb 16 10:12 filea -rw-r--r--. 1 root root 0 Feb 16 10:12 fileb -rw-r--r--. 1 root root 0 Feb 16 10:12 filec -rw-r--r--. 1 root root 0 Feb 16 10:12 filed -rw-r--r--. 1 root root 0 Feb 16 10:12 filee -rw-r--r--. 1 root root 0 Feb 16 10:12 filef
應用案例:[a-f]和[af]的區別
首先建立測試文件fileA,fileB,fileC,fileD,fileE,fileF
[root@centos7 tmp]#touch file{A..F} [root@centos7 tmp]#ll file* -rw-r--r--. 1 root root 0 Feb 16 10:08 file1 -rw-r--r--. 1 root root 0 Feb 16 10:08 file2 -rw-r--r--. 1 root root 0 Feb 16 10:08 file3 -rw-r--r--. 1 root root 0 Feb 16 10:08 file4 -rw-r--r--. 1 root root 0 Feb 16 10:08 file5 -rw-r--r--. 1 root root 0 Feb 16 10:12 filea -rw-r--r--. 1 root root 0 Feb 16 10:19 fileA -rw-r--r--. 1 root root 0 Feb 16 10:12 fileb -rw-r--r--. 1 root root 0 Feb 16 10:19 fileB -rw-r--r--. 1 root root 0 Feb 16 10:12 filec -rw-r--r--. 1 root root 0 Feb 16 10:19 fileC -rw-r--r--. 1 root root 0 Feb 16 10:12 filed -rw-r--r--. 1 root root 0 Feb 16 10:19 fileD -rw-r--r--. 1 root root 0 Feb 16 10:12 filee -rw-r--r--. 1 root root 0 Feb 16 10:19 fileE -rw-r--r--. 1 root root 0 Feb 16 10:12 filef -rw-r--r--. 1 root root 0 Feb 16 10:19 fileF
而後比較二者的區別
[af]表示匹配兩個字符,[a-f]表示匹配區間,可是它也會匹配大寫字母,按照小寫字母,大寫字母排序。
[root@centos7 tmp]#ll file[af] -rw-r--r--. 1 root root 0 Feb 16 10:12 filea -rw-r--r--. 1 root root 0 Feb 16 10:12 filef [root@centos7 tmp]#ll file[a-f] -rw-r--r--. 1 root root 0 Feb 16 10:12 filea -rw-r--r--. 1 root root 0 Feb 16 10:19 fileA -rw-r--r--. 1 root root 0 Feb 16 10:12 fileb -rw-r--r--. 1 root root 0 Feb 16 10:19 fileB -rw-r--r--. 1 root root 0 Feb 16 10:12 filec -rw-r--r--. 1 root root 0 Feb 16 10:19 fileC -rw-r--r--. 1 root root 0 Feb 16 10:12 filed -rw-r--r--. 1 root root 0 Feb 16 10:19 fileD -rw-r--r--. 1 root root 0 Feb 16 10:12 filee -rw-r--r--. 1 root root 0 Feb 16 10:19 fileE -rw-r--r--. 1 root root 0 Feb 16 10:12 filef
若是想要實現
預約義的字符類:
字符類名稱 | 功能 |
---|---|
[:digit:] | 任意數字,至關於0-9 |
[:lower:] | 任意小寫字母 |
[:upper:] | 任意大寫字母 |
[:alpha:] | 任意大小寫字母 |
[:alnum:] | 任意數字和字母 |
應用案例:匹配文件名是file小寫字母的文件列表
[root@centos7 tmp]#ll file[[:lower:]] -rw-r--r--. 1 root root 0 Feb 16 10:12 filea -rw-r--r--. 1 root root 0 Feb 16 10:12 fileb -rw-r--r--. 1 root root 0 Feb 16 10:12 filec -rw-r--r--. 1 root root 0 Feb 16 10:12 filed -rw-r--r--. 1 root root 0 Feb 16 10:12 filee -rw-r--r--. 1 root root 0 Feb 16 10:12 filef
應用案例:匹配文件名是file數字的文件列表
[root@centos7 tmp]#ll file[[:digit:]] -rw-r--r--. 1 root root 0 Feb 16 10:08 file1 -rw-r--r--. 1 root root 0 Feb 16 10:08 file2 -rw-r--r--. 1 root root 0 Feb 16 10:08 file3 -rw-r--r--. 1 root root 0 Feb 16 10:08 file4 -rw-r--r--. 1 root root 0 Feb 16 10:08 file5
應用案例:匹配文件名是file大寫字母的文件列表
[root@centos7 tmp]#ll file[[:upper:]] -rw-r--r--. 1 root root 0 Feb 16 10:19 fileA -rw-r--r--. 1 root root 0 Feb 16 10:19 fileB -rw-r--r--. 1 root root 0 Feb 16 10:19 fileC -rw-r--r--. 1 root root 0 Feb 16 10:19 fileD -rw-r--r--. 1 root root 0 Feb 16 10:19 fileE -rw-r--r--. 1 root root 0 Feb 16 10:19 fileF
應用案例:排除文件名是file數字的文件列表
[root@centos7 tmp]#ll file[^[:digit:]] -rw-r--r--. 1 root root 0 Feb 16 10:12 filea -rw-r--r--. 1 root root 0 Feb 16 10:19 fileA -rw-r--r--. 1 root root 0 Feb 16 10:12 fileb -rw-r--r--. 1 root root 0 Feb 16 10:19 fileB -rw-r--r--. 1 root root 0 Feb 16 10:12 filec -rw-r--r--. 1 root root 0 Feb 16 10:19 fileC -rw-r--r--. 1 root root 0 Feb 16 10:12 filed -rw-r--r--. 1 root root 0 Feb 16 10:19 fileD -rw-r--r--. 1 root root 0 Feb 16 10:12 filee -rw-r--r--. 1 root root 0 Feb 16 10:19 fileE -rw-r--r--. 1 root root 0 Feb 16 10:12 filef -rw-r--r--. 1 root root 0 Feb 16 10:19 fileF
應用案例:顯示指定目錄下的隱藏文件
[root@centos7 ~]#ls -d .[^.]* .bash_history .bash_profile .cache .cshrc .esd_auth .local .viminfo .bash_logout .bashrc .config .dbus .ICEauthority .tcshrc
若是想要了解更多通配符,可使用man 7 glob命令查看
[root@centos7 tmp]#man 7 glob
ls和通配符的綜合案例
應用案例:顯示/var目錄下全部以l開頭,以一個小寫字母結尾,且中
間出現至少一位數字的文件或目錄
[root@centos7 ~]#cd /var/ [root@centos7 var]#touch l8e [root@centos7 var]#touch lf8ga [root@centos7 var]#ll /var/l*[[:digit:]]*[[:lower:]]
應用案例:顯示/etc目錄下以任意一位數字開頭,且以非數字結尾的
文件或目錄
[root@centos7 var]#ll -d /etc/[[:digit:]]*[^[:digit:]]
應用案例:顯示/etc/目錄下以非字母開頭,後面跟了一個字母及其
它任意長度任意字符的文件或目錄
[root@centos7 var]#ll /etc/[^[:alpha:]][[:alpha:]]*
應用案例:顯示/etc/目錄下全部以rc開頭,並後面是0-6之間的數
字,其它爲任意字符的文件或目錄
[root@centos7 var]#ls -d /etc/rc[0-6]* /etc/rc0.d /etc/rc1.d /etc/rc2.d /etc/rc3.d /etc/rc4.d /etc/rc5.d /etc/rc6.d
應用案例:顯示/etc目錄下,全部以.d結尾的文件或目錄
[root@centos7 var]#ls -d /etc/rc[0-6]* /etc/rc0.d /etc/rc1.d /etc/rc2.d /etc/rc3.d /etc/rc4.d /etc/rc5.d /etc/rc6.d
應用案例:顯示/etc目錄下,全部.conf結尾,且以m,n,r,p開頭的文
件或目錄
[root@centos7 ~]#ll -d /etc/[mnrp]*.conf -rw-r--r--. 1 root root 5171 Oct 31 04:26 /etc/man_db.conf -rw-r--r--. 1 root root 936 Oct 31 03:03 /etc/mke2fs.conf -rw-r--r--. 1 root root 2620 Jun 10 2014 /etc/mtools.conf -rw-r--r--. 1 root root 967 Nov 8 00:56 /etc/nfs.conf -rw-r--r--. 1 root root 3391 Nov 8 00:56 /etc/nfsmount.conf -rw-r--r--. 1 root root 1746 Jan 25 12:47 /etc/nsswitch.conf -rw-r--r--. 1 root root 91 Dec 3 2012 /etc/numad.conf -rw-r--r--. 1 root root 1362 Jun 10 2014 /etc/pbm2ppa.conf -rw-r--r--. 1 root root 6300 Jun 10 2014 /etc/pnm2ppa.conf -rw-r--r--. 1 root root 433 Oct 31 01:54 /etc/radvd.conf -rw-r--r--. 1 root root 1787 Jun 10 2014 /etc/request-key.conf -rw-r--r--. 1 root root 71 Feb 16 09:37 /etc/resolv.conf -rw-r--r--. 1 root root 458 Apr 11 2018 /etc/rsyncd.conf -rw-r--r--. 1 root root 3232 Oct 30 22:49 /etc/rsyslog.conf
應用案例:只顯示/root下的隱藏文件和目錄
[root@centos7 ~]#ll -d /root/.[^.]* -rw-------. 1 root root 5750 Feb 14 21:00 /root/.bash_history -rw-r--r--. 1 root root 18 Dec 29 2013 /root/.bash_logout -rw-r--r--. 1 root root 176 Dec 29 2013 /root/.bash_profile -rw-r--r--. 1 root root 176 Dec 29 2013 /root/.bashrc drwx------. 13 root root 277 Jan 29 15:10 /root/.cache drwx------. 14 root root 261 Jan 29 15:10 /root/.config -rw-r--r--. 1 root root 100 Dec 29 2013 /root/.cshrc drwx------. 3 root root 25 Jan 25 12:55 /root/.dbus -rw-------. 1 root root 16 Jan 29 15:09 /root/.esd_auth -rw-------. 1 root root 1554 Feb 14 09:43 /root/.ICEauthority drwx------. 3 root root 19 Jan 29 15:09 /root/.local -rw-r--r--. 1 root root 129 Dec 29 2013 /root/.tcshrc -rw-------. 1 root root 6745 Jan 29 23:01 /root/.viminfo
應用案例:只顯示/etc下的非隱藏目錄
[root@centos7 ~]#ll -d /etc/[^.]*/
功能說明:用於建立空文件或者建立已經存在的文件時會刷新文件的時間戳(atime,ctime,mtime)
經常使用選項:
選項名稱 | 功能描述 |
---|---|
-a | 改變atime和ctime |
-m | 改變mtime和ctime |
-c | 若是文件不存在,則不建立 ,即刷新時間戳 |
-t | 指定文件的atime和mtime時間戳 |
應用案例: 建立一個腳本文件
[root@centos7 ~]#touch test.sh [root@centos7 ~]#ll test.sh -rw-r--r--. 1 root root 0 Feb 14 15:30 test.sh
應用案例:修改文件的時間戳
[root@centos7 ~]#stat test.sh File: ‘test.sh’ Size: 0 Blocks: 0 IO Block: 4096 regular empty file Device: 802h/2050d Inode: 100663381 Links: 1 Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root) Context: unconfined_u:object_r:admin_home_t:s0 Access: 2019-02-14 15:30:14.598261308 +0800 Modify: 2019-02-14 15:30:14.598261308 +0800 Change: 2019-02-14 15:30:14.598261308 +0800 Birth: - [root@centos7 ~]#touch -a test.sh [root@centos7 ~]#stat test.sh File: ‘test.sh’ Size: 0 Blocks: 0 IO Block: 4096 regular empty file Device: 802h/2050d Inode: 100663381 Links: 1 Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root) Context: unconfined_u:object_r:admin_home_t:s0 Access: 2019-02-14 15:31:39.975258276 +0800 Modify: 2019-02-14 15:30:14.598261308 +0800 Change: 2019-02-14 15:31:39.975258276 +0800 Birth: -
功能描述:用於普通文件文件或者目錄的複製
經常使用選項:
選項名稱 | 功能描述 |
---|---|
-i | 覆蓋存在的文件提示 |
-n | 不覆蓋,注意-i和-n使用時,-i須要放在-n後面 |
-r | 遞歸複製目錄以及目錄下的全部內容 |
-d | 複製軟鏈接 ,默認是複製的軟鏈接指向的文件 |
--preserv | 複製文件保留指定的屬性,例如timestamp,owership |
-a | 至關於-dR -preserv=all ,複製文件時保留文件全部屬性,實現歸檔 |
-v | 查看複製文件是時的詳細些信息 |
-u | 只複製源比目標更新文件或者目標不存在的文件 |
--backup=numberd | 若是目標存在,覆蓋前先備份後加上數字後綴 |
應用案例:複製一個源文件到不存在的目錄
若是該目錄下不存在,則會將源文件(例如這裏的motd)更名以後複製到指定的目錄中,原有的文件依然存在
[root@centos7 ~]#cp /etc/motd /tmp/motd.bak [root@centos7 ~]#ll /tmp|grep motd.bak -rw-r--r--. 1 root root 60 Feb 16 11:11 motd.bak
使用cat查看複製後源文件和目標文件的內容
[root@centos7 ~]#cat /tmp/motd.bak 歡迎跟光磊一塊兒學習Linux系統運維與架構設計 [root@centos7 ~]#cat /etc/motd 歡迎跟光磊一塊兒學習Linux系統運維與架構設計
應用案例:複製一個源文件到已經存在的目錄
若是沒有使用任何選項,root用戶默認會提示覆蓋已經存在的文件(普通用戶不會提示),由於root用戶使用的是別名
[root@centos7 ~]#alias cp alias cp='cp -i'
若是想要覆蓋文件,輸入y則會覆蓋。,此類文件複製有丟失文件數據的風險
[root@centos7 ~]#cp /etc/issue /tmp/motd.bak cp: overwrite ‘/tmp/motd.bak’? y
使用cat查看複製後源文件和目標文件的內容
[root@centos7 ~]#cat /etc/issue \S Kernel \r on an \m [root@centos7 ~]#cat /tmp/motd.bak \S Kernel \r on an \m
應用案例:複製源文件到指定的目錄
當目標位置是目錄時,會將單個文件複製到目錄中,並保留源文件名稱。
[root@centos7 ~]#cp /etc/issue /tmp [root@centos7 ~]#ll /tmp/issue -rw-r--r--. 1 root root 23 Feb 16 11:24 /tmp/issue
應用案例:當複製多個文件時,目標必須是文件夾,不能是不存在的文件或者文件夾,不然會提示錯誤
[root@centos7 ~]#cp /etc/redhat-release /etc/issue /tmp/nofile cp: target ‘/tmp/nofile’ is not a directory
應用案例:當複製的源文件是文件夾時,須要添加-r選項,並且目標文件必需要是文件夾,若是是不存在的文件夾則會發生重命名(即將root重命名爲目標文件名)後複製。
[root@centos7 ~]#cp -r /root/ /tmp/xxx [root@centos7 ~]#tree /tmp/xxx /tmp/xxx ├── anaconda-ks.cfg ├── Desktop ├── Documents ├── Downloads ├── hello.c ├── initial-setup-ks.cfg ├── Music ├── Pictures ├── Public ├── Templates ├── test.sh └── Videos 8 directories, 4 files
應用案例:將/etc/目錄下全部文件備份到/testdir獨立的子目錄下, 並要求子目
錄格式爲 backupYYYY-mm-dd, 備份過程可見
[root@centos7 tmp]#cp -av /etc/ /tmp/backup`date +%F` [root@centos7 tmp]#ll /tmp/backup2019-02-16/
應用案例建立/testdir/rootdir目錄,並複製/root下全部
文件到該目錄內,要求保留原有權限
[root@centos7 ~]#mkdir -p /testdir/rootdir [root@centos7 ~]#cp -r /root/ /testdir/rootdir/ --preserve=mode
功能描述:移動或者重命名文件
經常使用選項:
選項名稱 | 選項功能 |
---|---|
-i | 交互式移動或者重命名 |
-f | 強制移動或者重命名 |
應用案例:實現文件移動
[root@centos7 tmp]#mkdir /tmp/source [root@centos7 tmp]#mkdir -p /tmp/target [root@centos7 tmp]#touch /tmp/source/test.txt [root@centos7 tmp]#mv /tmp/source/test.txt /tmp/target/ [root@centos7 tmp]#ll /tmp/source/ total 0 [root@centos7 tmp]#ll /tmp/target/ total 0 -rw-r--r--. 1 root root 0 Feb 16 12:33 test.txt
應用案例:實現文件重命名
[root@centos7 tmp]#mv /tmp/target/test.txt /tmp/target/test_new.txt [root@centos7 tmp]#ll /tmp/target/test_new.txt -rw-r--r--. 1 root root 0 Feb 16 12:33 /tmp/target/test_new.txt
應用案例:移動並重命名
[root@centos7 tmp]#mv /tmp/target/test_new.txt /tmp/source/test.txt [root@centos7 tmp]#ll /tmp/source/test.txt -rw-r--r--. 1 root root 0 Feb 16 12:33 /tmp/source/test.txt
應用案例:使用rename實現批量修改文件名
首先在/tmp目錄下建立10個測試文件
[root@centos7 tmp]#touch file{1..10}.log [root@centos7 tmp]#ll total 0 -rw-r--r--. 1 root root 0 Feb 16 12:44 file10.log -rw-r--r--. 1 root root 0 Feb 16 12:44 file1.log -rw-r--r--. 1 root root 0 Feb 16 12:44 file2.log -rw-r--r--. 1 root root 0 Feb 16 12:44 file3.log -rw-r--r--. 1 root root 0 Feb 16 12:44 file4.log -rw-r--r--. 1 root root 0 Feb 16 12:44 file5.log -rw-r--r--. 1 root root 0 Feb 16 12:44 file6.log -rw-r--r--. 1 root root 0 Feb 16 12:44 file7.log -rw-r--r--. 1 root root 0 Feb 16 12:44 file8.log -rw-r--r--. 1 root root 0 Feb 16 12:44 file9.log
而後使用rename實現批量變動文件
[root@centos7 tmp]#rename '.log' '.log.bak' file* [root@centos7 tmp]#ll total 0 -rw-r--r--. 1 root root 0 Feb 16 12:44 file10.log.bak -rw-r--r--. 1 root root 0 Feb 16 12:44 file1.log.bak -rw-r--r--. 1 root root 0 Feb 16 12:44 file2.log.bak -rw-r--r--. 1 root root 0 Feb 16 12:44 file3.log.bak -rw-r--r--. 1 root root 0 Feb 16 12:44 file4.log.bak -rw-r--r--. 1 root root 0 Feb 16 12:44 file5.log.bak -rw-r--r--. 1 root root 0 Feb 16 12:44 file6.log.bak -rw-r--r--. 1 root root 0 Feb 16 12:44 file7.log.bak -rw-r--r--. 1 root root 0 Feb 16 12:44 file8.log.bak -rw-r--r--. 1 root root 0 Feb 16 12:44 file9.log.bak
[root@centos7 tmp]#rename 'file' 'file0' file* [root@centos7 tmp]#ll total 0 -rw-r--r--. 1 root root 0 Feb 16 12:44 file010.log.bak -rw-r--r--. 1 root root 0 Feb 16 12:44 file01.log.bak -rw-r--r--. 1 root root 0 Feb 16 12:44 file02.log.bak -rw-r--r--. 1 root root 0 Feb 16 12:44 file03.log.bak -rw-r--r--. 1 root root 0 Feb 16 12:44 file04.log.bak -rw-r--r--. 1 root root 0 Feb 16 12:44 file05.log.bak -rw-r--r--. 1 root root 0 Feb 16 12:44 file06.log.bak -rw-r--r--. 1 root root 0 Feb 16 12:44 file07.log.bak -rw-r--r--. 1 root root 0 Feb 16 12:44 file08.log.bak -rw-r--r--. 1 root root 0 Feb 16 12:44 file09.log.bak
功能說明:刪除文件或者目錄
經常使用選項:
功能名稱 | 選項說明 |
---|---|
-f | 強制刪除 |
-r | 遞歸刪除 ,用於刪除目錄 |
-i | 交互式刪除 |
應用案例: 自殺式刪除,工做中禁止使用,CentOS7中已經被禁用了,除非加上--no-preserve-root長選項
[root@centos7 tmp]#rm -rf / rm: it is dangerous to operate recursively on ‘/’ rm: use --no-preserve-root to override this failsafe
應用案例:定義別名,防止誤刪除
[root@centos7 tmp]#mkdir -p /tmp/bak [root@centos7 tmp]#alias rm='mv -t /tmp/bak' [root@centos7 tmp]#rm /tmp/file0 file010.log.bak file02.log.bak file04.log.bak file06.log.bak file08.log.bak file01.log.bak file03.log.bak file05.log.bak file07.log.bak file09.log.bak [root@centos7 tmp]#rm /tmp/file0 file010.log.bak file02.log.bak file04.log.bak file06.log.bak file08.log.bak file01.log.bak file03.log.bak file05.log.bak file07.log.bak file09.log.bak [root@centos7 tmp]#rm /tmp/file01.log.bak [root@centos7 tmp]#ll /tmp/bak/ total 0 -rw-r--r--. 1 root root 0 Feb 16 12:44 file01.log.bak
在刪除大文件時,若是文件依然在使用則不會當即釋放空間。
這時可使用>實現清空文件內容,而不推薦使用rm。
功能描述:顯示指定的目錄樹
經常使用選項:
選項名稱 | 功能描述 |
---|---|
-d | 只顯示目錄 |
-L | 顯示指定的層級數目 |
-P | 只顯示由-P 匹配到的路徑 |
應用案例:顯示根目錄下的一級子目錄
8 directories, 4 files [root@centos7 ~]#tree -L 1 / / ├── bin -> usr/bin ├── boot ├── data ├── dev ├── etc ├── home ├── lib -> usr/lib ├── lib64 -> usr/lib64 ├── media ├── misc ├── mnt ├── net ├── opt ├── proc ├── root ├── run ├── sbin -> usr/sbin ├── srv ├── sys ├── testdir ├── tmp ├── usr └── var 23 directories, 0 files
應用案例:顯示指定文件夾的目錄樹
[root@centos7 ~]#tree -P /etc/sysconfig/ . ├── Desktop ├── Documents ├── Downloads ├── Music ├── Pictures ├── Public ├── Templates └── Videos 8 directories, 0 files
應用案例:顯示root用戶的文件夾目錄樹
[root@centos7 ~]#tree -d ~ /root ├── Desktop ├── Documents ├── Downloads ├── Music ├── Pictures ├── Public ├── Templates └── Videos 8 directories
功能描述:用於建立目錄
經常使用選項:
選項名稱 | 功能描述 |
---|---|
-p | 建立父目錄 |
-v | 查看建立過程 |
應用案例:建立多級目錄
[root@centos7 ~]#mkdir -p Desktop/parent/child
應用案例:顯示建立目錄的過程
[root@centos7 ~]#mkdir -pv Desktop/china/shanghai mkdir: created directory ‘Desktop/china’ mkdir: created directory ‘Desktop/china/shanghai’
應用案例:建立多個文件夾1
[root@centos7 Desktop]#cd /tmp/ [root@centos7 tmp]#mkdir -pv /testdir/dir1/{x,y}/{a,b} mkdir: created directory ‘/testdir/dir1’ mkdir: created directory ‘/testdir/dir1/x’ mkdir: created directory ‘/testdir/dir1/x/a’ mkdir: created directory ‘/testdir/dir1/x/b’ mkdir: created directory ‘/testdir/dir1/y’ mkdir: created directory ‘/testdir/dir1/y/a’ mkdir: created directory ‘/testdir/dir1/y/b’
應用案例:建立多個文件夾2
[root@centos7 tmp]#mkdir -pv /testdir/dir2/{x/{a,b},y} mkdir: created directory ‘/testdir/dir2’ mkdir: created directory ‘/testdir/dir2/x’ mkdir: created directory ‘/testdir/dir2/x/a’ mkdir: created directory ‘/testdir/dir2/x/b’ mkdir: created directory ‘/testdir/dir2/y’
應用案例:建立多個文件夾3
[root@centos7 testdir]#mkdir -pv /testdir/dir{3,4,5/dir{6,7}} mkdir: created directory ‘/testdir/dir3’ mkdir: created directory ‘/testdir/dir4’ mkdir: created directory ‘/testdir/dir5’ mkdir: created directory ‘/testdir/dir5/dir6’ mkdir: created directory ‘/testdir/dir5/dir7’
功能描述:刪除空目錄,若是目錄下有內容是沒法刪除的。
經常使用選項:
選項名稱 | 功能描述 |
---|---|
-p | 刪除父目錄 |
應用案例:刪除子目錄
[root@centos7 ~]#rmdir Desktop/china/shanghai/ [root@centos7 ~]#ll Desktop/china/ total 0
應用案例:刪除多級子目錄
[root@centos7 Desktop]#mkdir -p parent/child [root@centos7 Desktop]#rmdir -p parent/child/ [root@centos7 Desktop]#ll total 0 drwxr-xr-x. 2 root root 6 Feb 16 13:11 china