原創Blog,轉載請註明出處
附上之前訪問量比較高的幾篇linux部落格
本人使用shell的8個小技巧
grep的九個經典使用情境
sed命令詳解
awk命令詳解
linux中所有的東西都是檔案,命令就是一個一個二進位檔案
1、ls
/bin/ls
常用選項
-a 所有檔案(包括隱藏檔案)
-l 詳細資料
-d 目錄屬性
-i 查看inode
舉例
[root@localhost testForCsdn]# lsfileList first second[root@localhost testForCsdn]# ls -a. .. fileList first second[root@localhost testForCsdn]# ls -ltotal 16-rw-r--r-- 1 root root 100 Oct 18 23:12 fileList-rw-r--r-- 1 root root 0 Oct 24 18:04 first-rw-r--r-- 1 root root 0 Oct 24 18:04 second[root@localhost testForCsdn]# ls -altotal 32drwxr-xr-x 2 root root 4096 Oct 24 18:04 .drwxr-x--- 17 root root 4096 Oct 24 17:56 ..-rw-r--r-- 1 root root 100 Oct 18 23:12 fileList-rw-r--r-- 1 root root 0 Oct 24 18:04 first-rw-r--r-- 1 root root 0 Oct 24 18:04 second[root@localhost testForCsdn]# ls -d.[root@localhost testForCsdn]# ls -i2256719 fileList 2256718 first 2256730 second
分析下
-rw-r--r-- 1 root root 100 Oct 18 23:12 fileList
對於這一行來說,從左至右分析
-表示這是一個二進位檔案
rw-r--r-- 分別r表示讀許可權,w表示寫入權限,-表示沒有許可權,如果是x表示可執行許可權,一共9個,三個一組,分別表示建立者,所屬組,其他人
所以,對於建立者來說是rw-就是可讀寫不可執行,對於所屬組就是r--只有讀的許可權,其他人只有讀的許可權
1表示永久連結數目
第一個root表示使用者
第二個root表示使用者組
100表示檔案大小,如果是目錄就是目錄和子目錄大小
Oct 18 23:13:12 表示最後修改的時間
fileList 表示檔案名稱
2、cd
change direction
和cd配合的常用命令還有
pwd 查看目前的目錄
改變目錄
首先講解下目錄
/ 根分區,所有的檔案和目錄都由此開始
/bin 使用者可執行檔
/sbin 系統可執行檔,主要供管理員使用,s為super
/etc 設定檔
/dev 裝置檔案
/proc 進程資訊,如/proc/cpuinfo包含了處理器資訊
/var 變數檔案如log,mail
/usr 使用者程式,為使用者應用程式存放檔案
/home 使用者主目錄
/boot 啟動附加元件
/opt 可選應用
/mnt 掛載目錄
/media 抽取式媒體 如/media/cdrom
/srv 服務資料
舉例
[root@localhost etc]# cd /etc/[root@localhost etc]# pwd/etc[root@localhost etc]# cd ~[root@localhost ~]# pwd/root
這裡要說明的是
cd ~代表回到當前登陸使用者的主目錄
如果是root回到/root/
如果是普通使用者,回到/home/
3、touch
建立空白檔案檔案或者修改檔案的時間戳記
常用選項
-a 只更改存取時間
-c 不建立任何文檔
-d 使用指定的日期
-m 只更改變動時間
-r 把指定文檔的或者目錄的日期時間改為和參考文檔的相同
-t 設定指定的時間戳記
舉例
[root@localhost testForCsdn]# ls -ltotal 16-rw-r--r-- 1 root root 100 Oct 18 23:12 fileList-rw-r--r-- 1 root root 0 Oct 24 18:04 first-rw-r--r-- 1 root root 0 Oct 24 18:04 second[root@localhost testForCsdn]# touch thrid[root@localhost testForCsdn]# ls -ltotal 20-rw-r--r-- 1 root root 100 Oct 18 23:12 fileList-rw-r--r-- 1 root root 0 Oct 24 18:04 first-rw-r--r-- 1 root root 0 Oct 24 18:04 second-rw-r--r-- 1 root root 0 Oct 24 18:34 thrid[root@localhost testForCsdn]# touch -t 201410250935.40 thrid [root@localhost testForCsdn]# ls -ltotal 20-rw-r--r-- 1 root root 100 Oct 18 23:12 fileList-rw-r--r-- 1 root root 0 Oct 24 18:04 first-rw-r--r-- 1 root root 0 Oct 24 18:04 second-rw-r--r-- 1 root root 0 Oct 25 2014 thrid
可以看到,用-t更改了時間戳記
4 mkdir
建立一個目錄
常用選項
-m 建立時候指定許可權
-p 指定路徑,如果該路徑上有些目錄不存在,則會建立,就是可以一次建立多層目錄
-v 顯示詳細資料
舉例
[root@localhost testForCsdn]# mkdir -m 777 firstDir[root@localhost testForCsdn]# ls -altotal 44drwxr-xr-x 3 root root 4096 Oct 24 18:41 .drwxr-x--- 17 root root 4096 Oct 24 17:56 ..-rw-r--r-- 1 root root 100 Oct 18 23:12 fileList-rw-r--r-- 1 root root 0 Oct 24 18:04 firstdrwxrwxrwx 2 root root 4096 Oct 24 18:41 firstDir-rw-r--r-- 1 root root 0 Oct 24 18:04 second-rw-r--r-- 1 root root 0 Oct 25 2014 thrid[root@localhost testForCsdn]# mkdir -p firstDir/secondDir/thridDir[root@localhost testForCsdn]# cd firstDir/secondDir/thridDir/[root@localhost thridDir]# pwd/root/testForCsdn/firstDir/secondDir/thridDir
可以看到,通過-p一次建立了多層目錄,-m在建立目錄的同時給予777的許可權
5 cp
copy
常用選項
-b 刪除覆蓋目的檔案的時候先備份
-i 詢問是否覆蓋
-P 保留源檔案的或者目錄的屬性:所有者,所屬組,許可權時間
-r 拷貝目錄
舉例
[root@localhost testForCsdn]# lsfileList firstDir second thrid[root@localhost testForCsdn]# ls firstDir/fileList second[root@localhost testForCsdn]# cp thrid firstDir/[root@localhost testForCsdn]# cp -b fileList firstDir/cp: overwrite `firstDir/fileList'? y[root@localhost testForCsdn]# ls firstDir/fileList fileList~ second thrid[root@localhost testForCsdn]# mkdir secondDic[root@localhost testForCsdn]# cp -r firstDir/ secondDic/[root@localhost testForCsdn]# ls secondDic/firstDir
順便說下scp:舉個例子
scp是遠程拷貝,如果要遠程拷貝目錄用-r
scp -r root@192.168.0.12:~/testCSDN/ localCSDN/
把192.168.0.12的~/testCSDN拷貝到localCSND下
6、mv
剪下和重新命名
舉例
[root@localhost testForCsdn]# lsfileList firstDir second secondDic thridYou have new mail in /var/spool/mail/root[root@localhost testForCsdn]# mkdir tempDic[root@localhost testForCsdn]# lsfileList firstDir second secondDic tempDic thrid[root@localhost testForCsdn]# mv fileList newName[root@localhost testForCsdn]# mv newName tempDic/[root@localhost testForCsdn]# lsfirstDir second secondDic tempDic thrid[root@localhost testForCsdn]# ls tempDic/newName
7 cat more
都是用來查看檔案內容
cat適合較短的檔案
More會分頁查看:空格翻頁,斷行符號下一行,q退出
cat filename
more filename
8 head tail
查看檔案的前幾行,後幾行
tail -f可以用來動態查看檔案的後幾行
舉例
9 ln
建立軟串連和永久連結
軟連結:類似於windows中的捷徑
永久連結:兩個檔案同步更新
軟連結可以跨檔案系統,永久連結不可以
舉例
建立一個軟串連
[root@localhost ~]# ln -s file.txt file.hardlink
建立一個永久連結
[root@localhost ~]# ln file.txt file.hardlink
可以看到,file.txt的永久連結數目變成了2
-rw-r--r-- 2 root root 210 Oct 19 07:54 file.hardlinklrwxrwxrwx 1 root root 8 Oct 24 19:14 file.softlink -> file.txt-rw-r--r-- 2 root root 210 Oct 19 07:54 file.txt
然後,修改file.txt
在檔案開始鍵入testCSDN
再查看永久連結
[root@localhost ~]# head -5 file.hardlink testForCSDNbackupbinbootdev
可以看到,同步更新了內容