1、顯示當前工作目錄
[root@centos7 ~]#cd /bin
[root@centos7 bin]#pwd
/bin
[root@centos7 bin]#pwd -P
/usr/bin
[root@centos7 bin]#ll /bin
lrwxrwxrwx. 1 root root 7 Jul 14 11:16 /bin -> usr/bin
如果當前工作目錄是一個軟連接目錄,則pwd默認當前目錄是軟連接的目錄,加上P就顯示軟連接源文件的目錄。
2、ls 命令
- ls命令默認數字排在字母的前面
[root@centos7 ~]#touch 9
[root@centos7 ~]#ls
9 Desktop Downloads Music Public Videos
anaconda-ks.cfg Documents initial-setup-ks.cfg Pictures Templates
- ll顯示軟連接目錄和普通目錄的區別
[root@centos7 ~]#ll /etc|wc -l #不加-d就會遞歸進入子目錄
269
[root@centos7 ~]#ll /etc/|wc -l
269
[root@centos7 ~]#ll /etc -d #-d表示只顯示etc這個目錄
drwxr-xr-x. 133 root root 8192 Jul 20 07:40 /etc
[root@centos7 ~]#ll /etc/ -d
drwxr-xr-x. 133 root root 8192 Jul 20 07:40 /etc/
[root@centos7 ~]#ll /bin|wc -l #不加/表示軟連接目錄
1
[root@centos7 ~]#ll /bin/|wc -l #加/表示軟連接的源目錄
1589
[root@centos7 ~]#ll -d /bin
lrwxrwxrwx. 1 root root 7 Jul 14 11:16 /bin -> usr/bin
[root@centos7 ~]#ll -d /bin/
dr-xr-xr-x. 2 root root 45056 Jul 18 07:58 /bin/
總結:如果ll顯示一般目錄加/和不加沒有什么區別,但對于軟連接目錄,加/代表軟連接的源目錄,不加代表軟連目錄。
-d代表只顯示目錄,不遞歸進入子目錄。
加上-d顯示的是源文件的bin
3、文件通配符
?*匹配零個或多個字符
??匹配任何單個字符
?~ 當前用戶家目錄
?~mage 用戶mage家目錄
?~+ 當前工作目錄
?~-前一個工作目錄
?[0-9]匹配數字范圍
?[a-z]:字母
?[A-Z]:字母
?[wang]匹配列表中的任何的一個字符
?[^wang]匹配列表中的所有字符以外的字符
?[:digit:]:任意數字,相當于0-9
?[:lower:]:任意小寫字母
?[:upper:]: 任意大寫字母
?[:alpha:]: 任意大小寫字母
?[:alnum:]:任意數字或字母
?[:blank:]:水平空白字符
?[:space:]:水平或垂直空白字符
?[:punct:]:標點符號
?[:print:]:可打印字符
?[:cntrl:]:控制(非打印)字符
?[:graph:]:圖形字符
?[:xdigit:]:十六進制字符
- 大小寫字母的說明
[root@centos6 app]#touch f{a..c}
[root@centos6 app]#ls
fa fb fc
[root@centos6 app]#touch f{A..C}
[root@centos6 app]#ls
fa fA fb fB fc fC
[root@centos6 app]#ll f[a-c]
-rw-r--r--. 1 root root 0 Jul 20 08:09 fa
-rw-r--r--. 1 root root 0 Jul 20 08:09 fA
-rw-r--r--. 1 root root 0 Jul 20 08:09 fb
-rw-r--r--. 1 root root 0 Jul 20 08:09 fB
-rw-r--r--. 1 root root 0 Jul 20 08:09 fc
[root@centos6 app]#ll f[A-C]
-rw-r--r--. 1 root root 0 Jul 20 08:09 fA
-rw-r--r--. 1 root root 0 Jul 20 08:09 fb
-rw-r--r--. 1 root root 0 Jul 20 08:09 fB
-rw-r--r--. 1 root root 0 Jul 20 08:09 fc
-rw-r--r--. 1 root root 0 Jul 20 08:09 fC
總結:[a-c]和[A-C]代表大小寫字母,并且從并且小寫字母在前.
4、touch命令
- touch創建空文件和>及>>的區別
>創建空文件,如果文件存在,則會把文件情況,比較危險
touch 創建空文件,如果文件存在,則會更改文件的時間戳
>>創建空文件,如果文件存在,則不改變文件內容也不改變時間戳,是最安全的創建空文件
touch可以創建多個空文件 - 指定時間戳
[root@centos6 app]#touch -t 201810201230.20 f3
[root@centos6 app]#stat f3
File: `f3'
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: 803h/2051d Inode: 13 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2018-10-20 12:30:20.000000000 +0800
Modify: 2018-10-20 12:30:20.000000000 +0800
Change: 2017-07-19 20:27:22.629271264 +0800
5、cp命令
- cp在復制的時候可能會丟失一些源數據
[root@centos6 app]#cp /etc/issue /app/xxx
[root@centos6 app]#ll !*
ll /etc/issue /app/xxx
-rw-r--r--. 1 root root 55 Jul 19 20:30 /app/xxx #時間戳沒有改變
-rw-r--r--. 1 root root 55 Jul 15 20:56 /etc/issue
為了避免丟失源數據,可以使用-a進行備份,-p 保留權限,所有者和時間戳, --preserv=all 保留所有源數據
- cp復制軟連接文件
[root@centos7 ~]#ll /etc/grub2.cfg
lrwxrwxrwx. 1 root root 22 Jul 14 11:20 /etc/grub2.cfg -> ../boot/grub2/grub.cfg
[root@centos7 ~]#cp /etc/grub2.cfg /app
[root@centos7 ~]#ll /app
total 24
-rw-r--r--. 1 root root 5275 Jul 18 10:49 cmd.log
-rw-r--r--. 1 root root 4215 Jul 19 20:47 grub2.cfg# 復制的是軟連接指向的源文件
drwxr-xr-x. 2 root root 230 Jul 19 18:52 music
-rwxr-xr-x. 1 root root 271 Jul 19 19:13 music.sh
-rw-r--r--. 1 root root 384 Jul 18 10:49 time.log
cp復制軟連接文件時復制的是軟連接指向的源文件,可以用-P選項,不跟隨軟連接源。
- cp復制特殊文件
[root@centos6 app]#cp -a /dev/sda1 /app
[root@centos6 app]#ll
total 4
prw-r--r--. 1 root root 0 Jul 19 18:58 f1
srwxr-xr-x. 1 root root 0 Jul 19 19:02 f2
-rw-r--r--. 1 root root 0 Oct 20 2018 f3
brw-rw----. 1 root disk 8, 1 Jul 19 07:33 sda1 #屬性相同
-rw-r--r--. 1 root root 55 Jul 19 20:30 xxx
[root@centos6 app]#ll /dev/sda1
brw-rw----. 1 root disk 8, 1 Jul 19 07:33 /dev/sda1 #屬性相同
[root@centos6 app]#cp -a /dev/zero /app
[root@centos6 app]#ll
total 4
prw-r--r--. 1 root root 0 Jul 19 18:58 f1
srwxr-xr-x. 1 root root 0 Jul 19 19:02 f2
-rw-r--r--. 1 root root 0 Oct 20 2018 f3
brw-rw----. 1 root disk 8, 1 Jul 19 07:33 sda1
-rw-r--r--. 1 root root 55 Jul 19 20:30 xxx
crw-rw-rw-. 1 root root 1, 5 Jul 19 07:33 zero #屬性相同
[root@centos6 app]#ll /dev/zero
crw-rw-rw-. 1 root root 1, 5 Jul 19 07:33 /dev/zero #屬性相同
cp命令在復制/dev/sda1和/dev/zero 這些特殊文件時,要保留屬性,否則無法使用。
- rename批量修改文件名
[root@centos6 app]#touch f{1,2,3}.txt
[root@centos6 app]#ll
total 4
prw-r--r--. 1 root root 0 Jul 19 18:58 f1
-rw-r--r--. 1 root root 0 Jul 19 21:06 f1.txt
srwxr-xr-x. 1 root root 0 Jul 19 19:02 f2
-rw-r--r--. 1 root root 0 Jul 19 21:06 f2.txt
-rw-r--r--. 1 root root 0 Oct 20 2018 f3
-rw-r--r--. 1 root root 0 Jul 19 21:06 f3.txt
brw-rw----. 1 root disk 8, 1 Jul 19 07:33 sda1
-rw-r--r--. 1 root root 55 Jul 19 20:30 xxx
crw-rw-rw-. 1 root root 1, 5 Jul 19 07:33 zero
[root@centos6 app]#rename '.txt' '.txt.bak' *.txt #將所有的以.txt結尾的文件,將.txt格式改為.txt.bak格式
[root@centos6 app]#ll
total 4
prw-r--r--. 1 root root 0 Jul 19 18:58 f1
-rw-r--r--. 1 root root 0 Jul 19 21:06 f1.txt.bak
srwxr-xr-x. 1 root root 0 Jul 19 19:02 f2
-rw-r--r--. 1 root root 0 Jul 19 21:06 f2.txt.bak
-rw-r--r--. 1 root root 0 Oct 20 2018 f3
-rw-r--r--. 1 root root 0 Jul 19 21:06 f3.txt.bak
brw-rw----. 1 root disk 8, 1 Jul 19 07:33 sda1
-rw-r--r--. 1 root root 55 Jul 19 20:30 xxx
crw-rw-rw-. 1 root root 1, 5 Jul 19 07:33 zero
6、rm命令
選項 -r 遞歸,刪除目錄 ;-f 不提示,直接刪除;-i 提示
- rm命令刪除一個大文件技巧
rm刪除一個大文件時,如果文件正在使用,即使刪除了這個大文件,磁盤空間也無法釋放,為了讓磁盤空間盡快釋放,可以先用> 清空這個文件,再用rm刪除這個文件,就可以馬上釋放磁盤空間。
-如何安全的刪除一個文件,可以使用如下命令
shred: f3: pass 1/6 (random)...
shred: f3: pass 2/6 (ffffff)...
shred: f3: pass 3/6 (random)...
shred: f3: pass 4/6 (000000)...
shred: f3: pass 5/6 (random)...
shred: f3: pass 6/6 (000000)...
shred: f3: removing
shred: f3: renamed to 00
shred: 00: renamed to 0
shred: f3: removed
- rm刪除點打頭的文件
[root@centos6 app]#touch .a
[root@centos6 app]#ls -a
. .. .a f1 f1.txt.bak f2 f2.txt.bak f3.txt.bak sda1 xxx zero
[root@centos6 app]#rm -f * #不能刪除點打頭的文件
[root@centos6 app]#ls -a
. .. .a #點打頭的文件還在
[root@centos6 app]#rm -f .* #執行這條命令才能刪除點打頭的文件
[root@centos6 app]#ls -a
. ..
- rm刪除目錄
[root@centos6 app]#ls
f1
[root@centos6 app]#rm -rf /app #提示要把目錄也刪除,但/app是個分區掛載點,無法刪除,只把目錄里的內容刪除
rm: cannot remove `/app': Device or resource busy
[root@centos6 app]#ls
[root@centos6 app]#mkdir f1
[root@centos6 app]#ls
f1
[root@centos6 app]#rm -rf /app/* #不提示刪除/app
[root@centos6 app]#ls
總結:分區掛載點無法刪除,要想刪除一個目錄下的文件而不刪除目錄的寫法為rm -rf /D/* ,如果rm-rf /D 則會連目錄也刪除。
7、tree命令
[root@centos6 app]#tree
.
├── f1
├── f1.txt.bak
├── f2
├── f2.txt.bak
├── f3.txt.bak
├── sda1
├── xxx
└── zero
0 directories, 8 files
[root@centos6 app]#tree -d #只顯示目錄
.
0 directories
[root@centos6 app]#tree -L 1#只顯示/app下的一級目錄
.
├── f1
├── f1.txt.bak
├── f2
├── f2.txt.bak
├── f3.txt.bak
├── sda1
├── xxx
└── zero
0 directories, 8 files
8、軟連接和硬鏈接、inode
ln 創建硬鏈接 ln-s 創建軟連接
- 軟連接和硬鏈接的區別
本質:硬鏈接本質上是同一個文件,只是一個文件具有兩個名字,彼此之間是平等的關系,節點號相同;
軟連接本質上不是同一個文件,節點號不同,相當于windows里面的快捷方式。
區別:
- 硬鏈接不允許對目錄創建,軟連接可以,如果要想使目錄的鏈接數增加,可以在當前目錄內創建多個子目錄來實現;
- 硬鏈接不允許跨分區,軟鏈接可以;
- 創建硬鏈接后連接數加1,軟鏈接鏈接數不增長;
- 刪除硬鏈接后互相不影響,刪除軟連接源文件后,軟連接就失效了;
- 硬鏈接文件的大小和屬性完全相同,軟鏈接大小、屬性和源文件不同,軟鏈接的大小由源文件的名的字符多少決定;
- 軟鏈接的相對路徑不是相對于當前工作目錄,而是相對于軟連接的當前工作目錄,硬鏈接相對于當前工作目錄和相對于硬鏈接的工作目錄都可以。
[root@centos6 app]#ln -s f1 f2 #創建f1文件的軟連接
[root@centos6 app]#ll
total 0
-rw-r--r--. 1 root root 0 Jul 19 22:06 f1
lrwxrwxrwx. 1 root root 2 Jul 19 22:06 f2 -> f1 #f2大小為f1的字符數
9、file命令
file 命令用來判斷文件類型,主要是查看文件頭部的magic number來判斷。
[root@centos6 app]#file /bin/cat #查看文件類型
/bin/cat: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.18, stripped
[root@centos6 app]#vim f2
/bin/cat
/bin/ls
[root@centos6 app]#file -f f2 #批量查看文件類型
/bin/cat: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.18, stripped
/bin/ls: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.18, stripped
10、面試題
如果磁盤空間沒有用完,但提示空間不足是什么原因?
原因為節點號用完了,一個分區有固定的節點號,可以通過下面命令查看。
[root@centos6 app]#df -i
Filesystem Inodes IUsed IFree IUse% Mounted on
/dev/sda2 3203072 133896 3069176 5% /
tmpfs 125514 5 125509 1% /dev/shm
/dev/sda3 2564096 12 2564084 1% /app
/dev/sda1 64000 38 63962 1% /boot
/dev/sr0 0 0 0 - /media/CentOS_6.9_Final
/boot分區的節點號總共有64000個,如果全部用光了,也會提示空間不足。