LPI Linux 認證/執行基本檔案管理
外觀
(LPIC-1 版本 5.0)
權重: 4
描述: 候選人應該能夠使用基本的 Linux 命令來管理檔案和目錄。
關鍵知識領域
- 分別複製、移動和刪除檔案和目錄。
- 遞迴地複製多個檔案和目錄。
- 遞迴地刪除檔案和目錄。
- 在命令中使用簡單和高階的萬用字元規範。
- 使用
find根據型別、大小或時間來定位和操作檔案。 tar、cpio和dd的使用。
以下是使用的檔案、術語和實用程式的部分列表
cpfindmkdirmvlsrmrmdirtouchtarcpioddfilegzipgunzipbzip2xzunxz- 檔案通配
要建立目錄,請使用 mkdir。
mkdir [options] dir
常見選項
-m mode: set permission mode. Default use umask. -p parent: create parent directory as needed.
示例
mkdir -m 0700 bin mkdir -p bin/system/x86
要刪除空目錄,請使用 rmdir。
rmdir [options] dir
常見選項
-p parent: remove empty superdirectories.
示例
rmdir tmp rmdir -p bin/system/x86
要將一個檔案複製到另一個檔案或目錄,請使用 cp。
cp [options] source target
源和目標可以是檔案或目錄。
常見選項
-i interactive: prompt to overwrite -r recursive: copy the subdirectories and contents. Use -R for special files. -f force: force the overwriting
預設情況下會靜默覆蓋目標檔案。(這不會改變原始檔)
示例
cp *.[a-z] /tmp cp readme readme.orig cp ls /bin cp -ri bin/* /bin
要重新命名檔案或目錄,或將檔案或目錄移動到另一個位置,請使用 mv。
mv [options] source target
源和目標可以是檔案或目錄。
常見選項
-i interactive: prompt to overwrite -f force: force the overwriting -v verbose
預設情況下會靜默覆蓋目標檔案。
示例
mv *.[a-z] /tmp mv readme readme.orig mv ls /bin mv -fi bin/* /bin
列出當前目錄中的檔案的命令是 ls。
ls [options] [filenames]
常見的選項是
-l For a long format -F Append a file type character -a All files, including hidden files -R Recursive listing of subtree -d Do not descend into directory
ls 等同於 DOS 上的 dir 命令。
ls 輸出的示例
$ ls -l /bin/ls -rwxr-xr-x 1 root root 46784 mar 23 2002 /bin/ls $ ls -ld /bin drwxr-xr-x 2 root root 2144 nov 5 11:55 /bin $ ls -a . .bash_history .bash_profile .bashrc ... $ ls -dF /etc .bashrc /bin/ls .bashrc /bin/ls* /etc/
長格式意味著
$ ls -l /etc/hosts #List a long format of the file hosts -rw-r—r-- 1 root root 677 Jul 5 22:18 /etc/hosts
檔案內容和位置 Linux/Unix 不像 Windows 那樣透過檔名副檔名來區分檔案。要確定檔案內容,請使用 file。
$ file /etc .bashrc /bin/ls /dev/cdrom /etc: directory .bashrc: ASCII English text /bin/ls: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked (uses shared libs), stripped /dev/cdrom: symbolic link to /dev/hdc
要確定命令是內建 shell 命令還是程式,請使用 type,並使用 which 來查詢其位置。
$ type cp cd ls which type cp is /bin/cp cd is a shell builtin ls is aliased to `ls $LS_OPTIONS' which is aliased to `type -p' type is a shell builtin $ which cut /usr/bin/cut
檔名可以用以下方式建立
- I/O 重定向
cat chapter1 chapter2 > book
- 編輯器,如 vi。
vi mynewfile
- 許多 Unix 實用程式
cp file newfile
- 應用程式
netscape
- touch 命令,它建立空檔案(或更新現有檔案的“修改日期”)
touch memo
有效的檔名可能包含(或為)
- 每個檔名最多 255 個字元
- 除正斜槓 '/' 之外的任何字元
- 建議使用字母數字字元以及加號、減號和下劃線字元。
- 區分大小寫('A' 和 'a' 被視為不同)
要避免的字元
- 連字元。
touch my-file -lt
- 空格。
touch more drink touch "more drink"
- 大多數其他特殊字元 !@#$%^&*():;"'}{|\<,>.?~`
touch memo*
要刪除檔案或子樹目錄,請使用 rm。
rm [options] files
檔案可以是檔案或目錄。
常見選項
-i interactive: prompt for each removal -f force: force the overwriting -r recursive: remove subtree directories and contents
沒有“撤銷刪除”或“恢復”命令。
示例
rm *.[a-z] rm readme readme.orig rm ls /bin rm -rfi /bin cd; rm -rf * .* # This removes all files in the home directory of the current user, as well as those in the subdirectories therein!
要搜尋子樹目錄中的檔案,請使用 find。
find [subtrees] [conditions] [actions]
該命令可以接受多個條件,並將遞迴地搜尋子樹。
一些可能的條件是
-name [FNG] # Search for the FNG name -type c # Type of file [bcdfl] -size [+-]# # Has a +- size in blocks (c: bytes, k: kilobytes) -user [name] # Own by user -atime [+-]# # Accessed days ago. +n means the file has not been accessed for the last n days. -n means the file has been accessed in the last n days. -mtime [+-]# # Modified days ago -perm nnn # Has permision flags nnn
一些可能的動作是
-print # Print the pathname
-exec cmd {} \; # Execute cmd on the file
-ok cmd {} \; # Same as -exec but ask first
示例
find . -name '*.[ch]' -print
find /var /tmp . -size +20 -print
find ~ -type c -name '*sys*' -print
find / -type f -size +2c -exec rm -i {} \;
find / -atime -3 -print
find ~jo ~toto -user chloe -exec mv {} /tmp \;
要定位二進位制檔案、原始檔或手冊頁,請使用 whereis。
whereis [options]
常見選項
-b: Search only for binaries. -m: Search only for manual sections. -s: Search only for sources.
示例
$ whereis host host: /usr/bin/host /etc/host.conf /usr/share/man/man1/host.1.gz $ whereis -m host host: /usr/share/man/man1/host.1.gz
要定位由 PATH 變數定義的位置的檔案,請使用 which。
$ which -a ls /bin/ls
-a 將在 PATH 中查詢所有可能的匹配項,而不僅僅是第一個匹配項。
- 編寫一個互動式命令來刪除主目錄中的所有 .tmp 檔案。對每個提示都回答 y。
- 列出使用者主目錄中所有以 .pdf 結尾且大小大於 50 個塊且一個月內未被訪問的檔案。
- 建立一個名為 file.h 的檔案,其中包含在 /usr 目錄中找到的所有以 .h 結尾的檔名。
- 對 /usr/src/packages 目錄中找到的所有 c 檔案進行 touch 操作。
- 建立新檔案和新目錄時的預設許可權是什麼?
- 如何建立一個檔名中包含空格的新檔案或目錄?(例如:'new dir')
- 刪除主目錄中所有型別為字元和塊的檔案的命令是什麼?
- 如何查詢 find 程式的位置?
- 刪除 /tmp 中所有不屬於 root 且一週內未被訪問的檔案。