1. 建立目錄
mkdir
NO1. 在當前路徑建立一級目錄
[root@rehat root]# mkdir test
NO2. 在當前路徑建立多級目錄
[root@rehat root]# mkdir -p mytest/test1/test1_1
NO3. 在建立目錄的同時給建立的目錄賦許可權
[root@rehat root]# mkdir -m 777 testmod
這樣任何人對此目錄都有任何許可權
2. 複製檔案與檔案夾
cp
NO1. 複製指定目錄的檔案到目前的目錄,並重新命名
[root@rehat root]# cp ~/.bashrc bashrc_bak
NO2. 強制複製指定目錄的檔案到目前的目錄,而不管目前的目錄是否含有該檔案
[root@rehat root]# cp -f ~/.bashrc bashrc
NO2. 複製指定目錄到目前的目錄
[root@rehat root]# cp -r /root/test .[root@rehat root]# cp -r /root/test/ .
兩者效果一樣,在複製目錄時,會將源路徑的最後一級目錄全部複製過去,包括它本身。
NO3. 複製指定目錄的檔案到指定目錄
[root@rehat root]# cp ~/.bashrc /bak/.bashrc
NO4. 在複製時將源檔案的全部屬性也複製過來。若不指定參數,則目標檔案與源檔案屬性可能不一致。
[root@rehat root]# cp -a ~/.bashrc /bak/.bashrc
NO5. 若兩個檔案夾要保證同步,一個檔案的改了,另一個檔案也跟著改,但是要保證兩個檔案的檔案都是最新的。
[root@rehat root]# cp -u /src/.bashrc /bak_src/bashrc
3. 建立連結檔案,包括永久連結與軟連結
ln
NO1. 建立類似於 Windows 的捷徑
[root@rehat root]# ln -s test.txt test.txt_slnk
NO2. 當想備份一個檔案,但空間又不夠,則可以為該檔案建立一個硬串連。這樣,就算原檔案刪除了,只要該
連結檔案沒被刪除,則在儲存空間裡還是沒有被刪除。
[root@rehat root]# ln -l test.txt test.txt_hlnk
4. 刪除檔案
rm
NO1. 刪除目前的目錄的檔案
[root@rehat root]# rm test.txt
NO2. 強制移除目前的目錄的檔案,不彈出提示
[root@rehat root]# rm -f test.txt
NO3. 強制移除整個目錄,包括目錄與檔案全部刪除,需要管理員權限
[root@rehat root]# rm -r -f test
5. 刪除檔案夾
rmdir
NO1. 刪除一個空目錄
[root@rehat root]# rmdir emptydir
NO2. 刪除多級空目錄
[root@rehat root]# rmdir -p emptydir/d1/d11
6. 掛載檔案系統與卸載檔案系統
mount / umount
NO1. 掛載光碟機
[root@rehat root]# mount -t iso9660 /dev/cdrom /mnt/cdrom
NO2. 掛載光碟機,支援中文
[root@rehat root]# mount -t iso9660 -o codepage=936,iocharset=cp936 /dev/cdrom /mnt/cdrom
NO3. 掛載 Windows 分區,FAT檔案系統
[root@rehat root]# mount -t vfat /dev/hda3 /mnt/cdrom
NO4. 掛載 Windows 分區,NTFS檔案系統
[root@rehat root]# mount -t ntfs -o iocharset=cp936 /dev/hda7 /mnt/had7
No5. 掛載 ISO 檔案
[root@rehat root]# mount -o loop /abc.iso /mnt/cdrom
NO6. 掛載 軟碟機
[root@rehat root]# mount /dev/fd0 /mnt/floppy
NO7. 掛載閃盤
[root@rehat root]# mount /dev/sda1 /mnt/cdrom
NO8. 掛載 Windows 作業系統共用的檔案夾
[root@rehat root]# mount -t smbfs -o username=guest,password=guest //machine/path /mnt/cdrom
NO9. 顯示掛載的檔案系統
[root@rehat root]# mount[root@rehat root]# cat /etc/fstab
顯示系統啟動自動載入的檔案系統
[root@rehat root]# cat /etc/mtab
顯示當前載入的檔案系統
7. 檢查磁碟空間
df
NO1. 顯示所有儲存系統空間使用方式,同時顯示儲存系統的檔案系統類型s
[root@rehat root]# df -aT
NO2. 顯示指定檔案系統的空間使用方式
[root@rehat root]# df -t ext3
NO3. 人性化顯示各儲存空間大小
[root@rehat root]# df -ah
NO4. 有時候掛載了網路檔案系統,若只想看原生檔案系統用如下命令
[root@rehat root]# df -ahlT
NO5. 查看某個檔案系統的磁碟使用方式
[root@rehat root]# df -h /dev/cdrom
8. 檢查目錄空間大小
du
NO1. 查看當前檔案夾大小
[root@rehat root]# du -sh
NO2. 查看當前檔案及檔案中包含的子檔案夾大小
[root@rehat root]# du -ch
NO3. 查看檔案的大小
[root@rehat root]# du -h test1.txt
NO4. 同時查看多個檔案的大小
[root@rehat root]# du -h test1.txt test2.txt
9. 磁碟磁碟重組
linux 下基本上不用磁碟重組,它每隔一段時間會自動整理
10. 建立/改變檔案系統
NO1. 建立檔案系統類型
1 2 3 |
[root@rehat root]# umount /dev/sdb1[root@rehat root]# mkfs -t ext3 /dev/db1[root@rehat root]# mount /dev/sdb1 /practice 11. 改變檔案或檔案夾許可權 chmod NO1. 將自己的筆記設為只有自己才能看 [root@rehat root]# chmod go-rwx test.txt 或者 [root@rehat root]# chmod 700 test.txt NO2. 同時修改多個檔案的許可權 [root@rehat root]# chmod 700 test1.txt test2.txt NO3. 修改一個目錄的許可權,包括其子目錄及檔案 [root@rehat root]# chmod 700 -R test 12. 改變檔案或檔案夾擁有者 chown 該命令只有 root 才能使用 NO1. 更改某個檔案的擁有者 [root@rehat root]# chown jim:usergroup test.txt NO2. 更改某個目錄的擁有者,並包含子目錄 [root@rehat root]# chown jim:usergroup -R test 13. 查看文字檔內容 cat NO1. 查看檔案內容,並在每行前面加上行號 [root@rehat root]# cat -n test.txt NO2. 查看檔案內容,在不是空行的前面加上行號 [root@rehat root]# cat -b test.txt NO3. 合并兩個檔案的內容 [root@rehat root]# cat test1.txt test2.txt > test_new.txt NO4. 全並兩具檔案的內容,並追回到一個檔案 [root@rehat root]# cat test1.txt test2.txt >> test_total.txt NO5. 清空某個檔案的內容 [root@rehat root]# cat /dev/null > test.txt NO6. 建立一個新的檔案 [root@rehat root]# cat >> new.txt 按 CTRL + C 結束錄入 14. 編輯檔案檔案 vi NO1. 建立檔案檔案 [root@rehat root]# vi newfile.txt NO2. 修改檔案檔案 [root@rehat root]# vi test.txt test.txt 已存在 NO3. vi 的兩種工作模式:命令模式,編輯模式 NO4. 進入 vi 後為命令模式,按 Insrt 鍵進入編輯模式 按 ESC 進入命令模式,在命令模式不能編輯,只能輸入命令 NO5. 命令模式常用命令 :w 儲存當前文檔 :q 直接退出 vi :wq 先儲存後退出 15. 路徑操作 cd pwd NO1. 顯示當前路徑 [root@rehat root]# pwd NO2. 返回使用者主目錄 [root@rehat root]# cd NO3. 改變到其它路徑 [root@rehat root]# cd /etc NO4. 返回到上一級目錄 [root@rehat root]# cd .. NO5. 返回到根目錄 [root@rehat root]# cd / 16. 查詢檔案或檔案夾 find NO1. 尋找目前使用者主目錄下的所有檔案 [root@rehat root]# find ~ NO2. 讓目前的目錄中檔案屬主具有讀、寫入權限,並且檔案所屬組的使用者和其他使用者具有讀許可權的檔案; [root@rehat root]# find . -perm 644 -exec ls -l {} \; NO3. 為了尋找系統中所有檔案長度為0的普通檔案,並列出它們的完整路徑; [root@rehat root]# find / size 0 -type f -exec ls -l {} \; NO4. 尋找/var/logs目錄中更改時間在7日以前的普通檔案,並在刪除之前詢問它們; [root@rehat root]# find /var/logs -mtime +7 -type f -ok rm -i {} \; NO5. 為/找系統中所有屬於root組的檔案; [root@rehat root]# find / -group root -exec ls -l {} \; NO6. find命令將刪除當目錄中訪問時間在7日以來、含有數字尾碼的admin.log檔案 [root@rehat root]# find . -name "admin.log[0-9][0-9][0-9]" -atime -7 -ok rm { } \; NO7. 為了尋找當前檔案系統中的所有目錄並排序 [root@rehat root]# find . -type d | sort NO8. 為了尋找系統中所有的rmt磁帶裝置 [root@rehat root]# find /dev/rmt 17. 顯示檔案/檔案夾清單 ls / dir NO1. 顯示所有檔案,包括以.開頭的隱含檔案 [root@rehat root]# ls -a NO2. 顯示檔案的詳細資料 [root@rehat root]# ls -l NO3. 顯示目前的目錄及所有子目錄資訊 [root@rehat root]# ls -Rl NO4. 以時間排序顯示目錄,這在找最新檔案有用 [root@rehat root]# ls -tl NO5. 以檔案大小排序 [root@rehat root]# ls -Sl NO6. 顯示檔案大小,並按大小排序 [root@rehat root]# ls -s -l -S 18. 移動或變更檔/檔案夾名稱 mv 與 cp命令用法相似 NO1. 若移動目標檔案已存在,要在移動之前,先備份原來的目錄檔案 [root@rehat root]# mv -b test.txt test2/ 這樣在 test2 下將有兩個檔案 test.txt 及 text.txt~ 其中 test.txt~ 是備份檔案,test.txt是新的檔案 NO2. 若移動目標檔案已存在,但不想彈出是否覆蓋的提示,直接覆蓋 [root@rehat root]# mv -f test.txt test2/ NO3. 當源與目標都擁有同一個檔案,若源檔案比目標新則移動,否則不移動 [root@rehat root]# mv -u test.txt test2/ NO4. 變更檔名稱 [root@rehat root]# mv test.txt test2.txt NO5. 更改目錄名稱 [root@rehat root]# mv /test2 /test2_2 19. iptables 開啟連接埠相關命令: [root@rehat root]# /sbin/iptables -I INPUT -p tcp --dport 80 -j ACCEPT[root@rehat root]# /sbin/iptables -I INPUT -p tcp --dport 22 -j ACCEPT[root@rehat root]# /etc/rc.d/init.d/iptables save[root@rehat root]# /etc/init.d/iptables restart |