文件的尋址
絕對路徑:
文件在系統的真實位置,文件名字以“/”開頭
相對路徑:
文件相對與當前所在位置的一個名字的簡寫,這個名字不會以/開頭,而且名字會自動添加pwd顯示的值
注:pwd ##顯示當前工作目錄##
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
文件的管理
查看文件與目錄:ls
Usage: ls [OPTION]... [FILE]... ##用法:ls [參數] [對象]##
ls命令的參數:
-a:列出全部文件,包括隱藏文件(以.開頭的文件);
-A:列出全部文件,包括隱藏文件(以.開頭的文件),但不包括.與..這兩個目錄;
-d:只列出目錄本身,目錄內的文件數據不列出;
-f:不進行排序,直接列出結果(ls命令默認是以文件名排序的);
-F:根據文件、目錄等信息給予附加數據結構,例如:
*:代表可執行文件; /:代表目錄; =:代表socket文件; |:代表FIFO文件;
-h:將文件容量以人類較易讀懂的方式(例如:GB、KB等)列出來;
-i:列出inode號碼;
-l:列出長數據串,包含文件的屬性與權限等數據;
-n:列出UID與GID,而非用戶與用戶組名稱;
-r:將排序結果反向輸出,即與原本的排序結果相反;
-R:連同子目錄內容一起列出來,即第歸顯示該目錄下的文件;
-S:以文件容量大小排序,列出結果;
-t:以時間排序,列出結果;
--color=never:不依據文件特性給予顏色顯示;
--color=always:依據文件特性給予顏色顯示;
--color=auto:使系統自行依據設置判斷是否給予顏色顯示;
--full-time:以完整時間模式(年 月 日 時 分)輸出;
--time={atime,ctime}:輸出訪問時間(atime)或改變屬性時間(ctime),而非內容更改時間(modification time)。
常用ls命令示例:
[root@localhost ~]# cd /mnt/ [root@localhost mnt]# touch file{1..3} [root@localhost mnt]# touch .file [root@localhost mnt]# mkdir westos [root@localhost mnt]# touch westos/file{6..9} [root@localhost mnt]# ls ##列出當前工作目錄下的內容## file1 file2 file3 westos [root@localhost mnt]# cd westos/ [root@localhost westos]# ls file6 file7 file8 file9 [root@localhost westos]# cd .. [root@localhost mnt]# echo hello>file1 [root@localhost mnt]# cat file1 hello [root@localhost mnt]# ls westos/ ##列出目錄/mnt/westos/下的內容## file6 file7 file8 file9 [root@localhost mnt]# ls file1 file1 [root@localhost mnt]# ls -l /mnt/ ##列出目錄/mnt/下內容的屬性## total 4 -rw-r--r--. 1 root root 6 Feb 26 04:43 file1 -rw-r--r--. 1 root root 0 Feb 26 04:42 file2 -rw-r--r--. 1 root root 0 Feb 26 04:42 file3 drwxr-xr-x. 2 root root 54 Feb 26 01:25 westos [root@localhost mnt]# ls -d /mnt/ ##列出目錄/mnt/本身## /mnt [root@localhost mnt]# ls -ld /mnt/ ##列出目錄/mnt/本身的屬性## drwxr-xr-x. 3 root root 55 Feb 26 04:42 /mnt [root@localhost mnt]# ls -R /mnt/ ##第歸顯示目錄/mnt/下的內容## /mnt/: file1 file2 file3 westos /mnt/westos: file6 file7 file8 file9 [root@localhost mnt]# ls -a ##列出當前工作目錄下的全部內容## . .. .file file1 file2 file3 westos [root@localhost mnt]# ls -A ##同上,但不包括.和..兩個目錄## .file file1 file2 file3 westos
複製:cp
Usage: cp [OPTION]... [-T] SOURCE DEST
or: cp [OPTION]... SOURCE... DIRECTORY ##用法:cp [參數] 源文件 目標文件##
or: cp [OPTION]... -t DIRECTORY SOURCE...
cp命令的參數:
-a:即-pdr;
-d:若源文件爲連接文件的屬性(link file),則複製連接文件屬性而非文件本身;
-f:即force,若目標文件已經存在且無法開啓,則刪除後在嘗試一次;
-i:若目標文件(destination)已經存在時,在覆蓋時會先詢問操作的進行;
-l:進行硬連接(hard link)的連接文件創建,而非複製文件本身;
-p:連同文件的屬性一起復制,而非使用默認屬性,一般用於文件備份;
-r:第歸持續複製,用於目錄的複製行爲;
-s:複製成爲符號連接文件(aymbolic link),即“快捷方式”文件;
-u:若destination比source舊纔會更新destination。
常用cp命令的示例:
[root@localhost mnt]# ls -lR ##第歸顯示當前工作目錄下的內容## .: total 4 -rw-r--r--. 1 root root 6 Feb 27 06:07 file1 -rwxrwxrwx. 1 root root 0 Feb 27 06:06 file2 -rwxrwxrwx. 1 root root 0 Feb 27 06:13 file3 drwxr-xr-x. 2 root root 18 Feb 27 06:11 westos ./westos: total 0 -rw-r--r--. 1 root root 0 Feb 27 06:11 class [root@localhost mnt]# cp file1 westos/ [root@localhost mnt]# cp -p file2 westos/ ##連同權限一起復制## [root@localhost mnt]# cp file3 westos/ [root@localhost mnt]# ls -lR .: total 4 -rw-r--r--. 1 root root 6 Feb 27 06:07 file1 -rwxrwxrwx. 1 root root 0 Feb 27 06:06 file2 -rwxrwxrwx. 1 root root 0 Feb 27 06:13 file3 drwxr-xr-x. 2 root root 54 Feb 27 06:18 westos ./westos: total 4 -rw-r--r--. 1 root root 0 Feb 27 06:11 class -rw-r--r--. 1 root root 6 Feb 27 06:12 file1 -rwxrwxrwx. 1 root root 0 Feb 27 06:06 file2 -rwxr-xr-x. 1 root root 0 Feb 27 06:18 file3
刪除:rm
Usage: rm [OPTION]... FILE... ##用法:rm [參數] 對象##
rm命令的參數:
-f:即--force,忽略不存在的文件,且不出現警告信息;
-i:互動模式,刪除前會詢問用戶是否執行該命令;
-r:第歸刪除,常用於目錄刪除,該參數較爲危險。
常用rm命令示例:
[root@localhost mnt]# ls -R .: file1 file2 file3 westos ./westos: class1 class2 class3 [root@localhost mnt]# rm file{2..5} rm: remove regular empty file ‘file2’? y rm: remove regular empty file ‘file3’? y rm: cannot remove ‘file4’: No such file or directory ##file4不存在## rm: cannot remove ‘file5’: No such file or directory [root@localhost mnt]# ls -R .: file1 westos ./westos: class1 class2 class3 [root@localhost mnt]# rm -f file{1..3} ##執行rm命令無提示## [root@localhost mnt]# ls westos [root@localhost mnt]# rm westos/ rm: cannot remove ‘westos/’: Is a directory ##westos/是一個目錄## [root@localhost mnt]# rm -r westos/ rm: descend into directory ‘westos/’? y rm: remove regular empty file ‘westos/class1’? y rm: remove regular empty file ‘westos/class2’? y rm: remove regular empty file ‘westos/class3’? y rm: remove directory ‘westos/’? y [root@localhost mnt]# ls -R .:
移動:mv
mv file1 file2 dir ##移動文件file1 file2到目錄dir下##
mv 存在的文件 不存在的文件 ##把存在的文件重命名爲不存在的文件##
注:相同磁盤的mv是重命名,不同磁盤的mv是複製刪除過程
mv命令的參數:
-f:即--force,若目標文件已經存在,則不詢問直接覆蓋;
-i:若目標文件已經存在,則詢問是否覆蓋;
-u:若目標文件已經存在,且source(源文件)比較新,纔會更新。
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
文件內容的查閱
cat:由第一行開始顯示文件內容;
tac:由最後一行開始顯示文件內容;
nl:顯示的時候,順便輸出行號;
more:一頁一頁地顯示文件內容;
less:用途與more類似,但是可以往前翻頁;
head:只看文件頭幾行的內容;
tail:只看文件結尾幾行的內容;
od:以二進制的方式讀取文件的內容。
常用查看文件內容命令的示例
[root@localhost etc]# cat passwd ##顯示文件passwd的全部內容## root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin adm:x:3:4:adm:/var/adm:/sbin/nologin lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin sync:x:5:0:sync:/sbin:/bin/sync shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown halt:x:7:0:halt:/sbin:/sbin/halt mail:x:8:12:mail:/var/spool/mail:/sbin/nologin operator:x:11:0:operator:/root:/sbin/nologin games:x:12:100:games:/usr/games:/sbin/nologin ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin nobody:x:99:99:Nobody:/:/sbin/nologin dbus:x:81:81:System message bus:/:/sbin/nologin polkitd:x:999:998:User for polkitd:/:/sbin/nologin avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin avahi-autoipd:x:170:170:Avahi IPv4LL Stack:/var/lib/avahi-autoipd:/sbin/nologin rpc:x:32:32:Rpcbind Daemon:/var/lib/rpcbind:/sbin/nologin rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin ovirtagent:x:175:175:RHEV-M Guest Agent:/usr/share/ovirt-guest-agent:/sbin/nologin postfix:x:89:89::/var/spool/postfix:/sbin/nologin sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin chrony:x:998:996::/var/lib/chrony:/sbin/nologin student:x:1000:1000:Student User:/home/student:/bin/bash usbmuxd:x:113:113:usbmuxd user:/:/sbin/nologin colord:x:997:995:User for colord:/var/lib/colord:/sbin/nologin abrt:x:173:173::/etc/abrt:/sbin/nologin libstoragemgmt:x:996:994:daemon account for libstoragemgmt:/var/run/lsm:/sbin/nologin unbound:x:995:993:Unbound DNS resolver:/etc/unbound:/sbin/nologin qemu:x:107:107:qemu user:/:/sbin/nologin saslauth:x:994:76:"Saslauthd user":/run/saslauthd:/sbin/nologin ntp:x:38:38::/etc/ntp:/sbin/nologin rtkit:x:172:172:RealtimeKit:/proc:/sbin/nologin radvd:x:75:75:radvd user:/:/sbin/nologin pulse:x:171:171:PulseAudio System Daemon:/var/run/pulse:/sbin/nologin gdm:x:42:42::/var/lib/gdm:/sbin/nologin gnome-initial-setup:x:993:991::/run/gnome-initial-setup/:/sbin/nologin tcpdump:x:72:72::/:/sbin/nologin [root@localhost etc]# head -n 5 passwd ##顯示文件passwd的前五行## root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin adm:x:3:4:adm:/var/adm:/sbin/nologin lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin [root@localhost etc]# tail -n 5 passwd ##顯示文件passwd的後五行## radvd:x:75:75:radvd user:/:/sbin/nologin pulse:x:171:171:PulseAudio System Daemon:/var/run/pulse:/sbin/nologin gdm:x:42:42::/var/lib/gdm:/sbin/nologin gnome-initial-setup:x:993:991::/run/gnome-initial-setup/:/sbin/nologin tcpdump:x:72:72::/:/sbin/nologin
注:head命令和tail命令默認情況下顯示十行
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
創建文件和目錄
touch
touch file ####創建文件####
[root@localhost Desktop]# touch file ##在當前目錄下創建file文件## [root@localhost Desktop]# touch /mnt/file ##在/mnt目錄下創建file文件##
注:touch還可以修改文件的時間戳
mkdir
mkdir directory ####在當前工作目錄下創建目錄#####
mkdir -p directory ####上級目錄不存在自動建立####
[root@localhost Desktop]# ls [root@localhost Desktop]# mkdir westos [root@localhost Desktop]# ls westos [root@localhost Desktop]# mkdir -p linux/student [root@localhost Desktop]# ls -R .: linux westos ./linux: student ./linux/student: ./westos:
補:touch命令修改文件時間戳
用法:touch [參數] 文件
參數:
-a:僅修改訪問時間;
-c:僅修改文件的時間,若該文件不存在則不會創建新的文件;
-d:後面可以接欲修改的日期而不是當前日期;
-m:僅修改mtime;
-t:後面可以接欲修改的時間而不是當前時間,格式爲[YYMMDDHHMM]。
三個主要的變動時間:
modification time (mtime)-----更改文件內容數據的時間
status time (ctime)-----更改文件狀態(權限或屬性)的時間
access time (atime)-----文件內容被讀取的時間(例如:使用cat命令查看該文件)