Linux-文件基本操做

3.文件管理

本章同步視頻:https://edu.51cto.com/sd/e4874

3.1 目錄與路徑

3.1.1 絕對路徑與相對路徑

n  絕對路徑:路徑的寫法『必定由根目錄 / 寫起』,例如: /usr/share/doc 。shell

n  相對路徑:路徑的寫法『不是由 / 寫起』,例如由 /usr/share/doc 要到 /usr/share/man 底下時,能夠寫成:『cd ../man』這就是相對路徑的寫法啦!相對路徑意指『相對於目前工做目錄的路徑!』less

3.1.2 目錄的相關操做

.           表明此層目錄ide

..          表明上一層目錄spa

-           表明前一個工做目錄unix

~           表明『目前用戶身份』所在的家目錄component

~account  表明   account 這個用戶的家目錄(account是個帳號名稱)orm

1.cd -Change  the  current  directory to dir視頻

(1)使用絕對路徑ip

[root@localhost 桌面]# cd /tmp/rem

(2)使用相對路徑

[root@localhost tmp]# cd bbb

(3)直接回到家目錄1

[root@localhost bbb]# cd

(4)直接回到家目錄2

[root@localhost tmp]# cd ~

(5)回到前一次工做目錄

[root@localhost ~]# cd -

/tmp

[root@localhost tmp]#

(6)回到父目錄

[root@localhost tmp]# cd ..

[root@localhost /]#

2. pwd - print name of current/working directory

[root@localhost ~]# pwd

/root

3.mkdir - make directories

mkdir [OPTION]... DIRECTORY...

(1)建立一個目錄

[root@localhost tmp]# mkdir aaa

[root@localhost tmp]# ll

總用量 4

drwxr-xr-x. 2 root root 4096 2月  27 19:34 aaa

(2)建立多級目錄

[root@localhost tmp]# mkdir -p bbb/ccc

[root@localhost tmp]# ll -R

.:

總用量 8

drwxr-xr-x. 2 root root 4096 2月  27 19:34 aaa

drwxr-xr-x. 3 root root 4096 2月  27 19:35 bbb

./aaa:

總用量 0

./bbb:

總用量 4

drwxr-xr-x. 2 root root 4096 2月  27 19:35 ccc

./bbb/ccc:

總用量 0

(3)建立目錄時指定權限

[root@localhost tmp]# mkdir -m 777 ddd

[root@localhost tmp]# ll

總用量 12

drwxr-xr-x. 2 root root 4096 2月  27 19:34 aaa

drwxr-xr-x. 3 root root 4096 2月  27 19:35 bbb

drwxrwxrwx. 2 root root 4096 2月  27 19:39 ddd

4.rmdir - remove empty directories

rmdir [OPTION]... DIRECTORY...

(1)刪除一個空目錄

[root@localhost tmp]# ls

aaa  bbb  ddd

[root@localhost tmp]# rmdir ddd

[root@localhost tmp]# ls

aaa  bbb

(2)同時刪除多級空目錄

[root@localhost tmp]# ls

aaa  bbb

[root@localhost tmp]# rmdir -p bbb/ccc/

[root@localhost tmp]# ls

aaa

3.1.3 文件與目錄管理

1.ls - list directory contents

ls [OPTION]... [FILE]...

(1)查看目錄內容

[root@localhost tmp]# ls

aaa

(2)查看目錄詳細信息

[root@localhost tmp]# ls -l

總用量 4

drwxr-xr-x. 2 root root 4096 2月  27 19:34 aaa

(3)查看目錄下所有內容(含.和..)

[root@localhost tmp]# ls -a

.   aaa     .esd-1000   .ICE-unix   .X0-lock   .XIM-unix

..  .esd-0  .font-unix  .Test-unix  .X11-unix

(4)查看目錄下所有內容

[root@localhost tmp]# ls -A

aaa  .esd-0  .esd-1000  .font-unix  .ICE-unix  .Test-unix  .X0-lock  .X11-unix  .XIM-unix

(5)只查看當前目錄信息

[root@localhost tmp]# ls -dl

drwxrwxrwt. 10 root root 4096 2月  27 19:43 .

(6)查看iNode信息

[root@localhost tmp]# ls -il

總用量 4

145499 drwxr-xr-x. 2 root root 4096 2月  27 19:34 aaa

2.cp - copy files and directories

cp [OPTION]... SOURCE... DIRECTORY

(1)複製

[root@localhost tmp]# ls

aaa  bbb

[root@localhost tmp]# cp bbb bbb.cp

[root@localhost tmp]# ls

aaa  bbb  bbb.cp

(2)目標文件已存在的複製

[root@localhost tmp]# ls

aaa  bbb  bbb.cp

[root@localhost tmp]# cp bbb bbb.cp

cp:是否覆蓋"bbb.cp"? y

[root@localhost tmp]# ls

aaa  bbb  bbb.cp

(3)保留原屬性的複製

[calf@localhost tmp]$ ll

總用量 4

-rw-rw-rw-. 1 root root    0 2月  27 19:51 bbb

[calf@localhost tmp]$ cp bbb bbb.calf      #正常複製

[calf@localhost tmp]$ ll

總用量 4

-rw-rw-rw-. 1 root root    0 2月  27 19:51 bbb

-rw-rw-r--. 1 calf calf    0 2月  27 20:06 bbb.calf

[calf@localhost tmp]$ cp -p bbb bbb.p      #保留屬性的複製

[calf@localhost tmp]$ ll

總用量 4

-rw-rw-rw-. 1 root root    0 2月  27 19:51 bbb

-rw-rw-r--. 1 calf calf    0 2月  27 20:06 bbb.calf

-rw-rw-rw-. 1 calf calf    0 2月  27 19:51 bbb.p

(4)複製多個文件(目標必須是dir)

[root@localhost tmp]# cp /etc/passwd /etc/shadow /etc/group .

[root@localhost tmp]# ls      #複製3個文件到當前目錄

group  passwd  shadow

3.rm - remove files or directories

rm [OPTION]... FILE...

(1)正常刪除文件

[root@localhost tmp]# ls

aaa  bbb  bbb.a  bbb.calf  bbb.cp  bbb.p

[root@localhost tmp]# rm bbb.p

rm:是否刪除普通空文件 "bbb.p"?y

[root@localhost tmp]# ls

aaa  bbb  bbb.a  bbb.calf  bbb.cp

(2)強行刪除文件(系統不給提示)

[root@localhost tmp]# ls

aaa  bbb  bbb.a  bbb.calf  bbb.cp

[root@localhost tmp]# rm -f bbb.cp

[root@localhost tmp]# ls

aaa  bbb  bbb.a  bbb.calf

(3)刪除一個目錄下的所有文件

[root@localhost tmp]# rm -rf *    #刪除當前目錄下所有文件

[root@localhost ~]# rm -rf /tmp/*   #刪除/tmp目錄下所有文件

(4)刪除特殊文件 -aaa

[root@localhost tmp]# ls

-aaa

[root@localhost tmp]# rm -aaa      #刪除失敗

rm:無效選項 -- a

Try 'rm ./-aaa' to remove the file "-aaa".

Try 'rm --help' for more information.

[root@localhost tmp]# rm ./-aaa     #刪除成功

rm:是否刪除普通空文件 "./-aaa"?y

4.mv - move (rename) files

mv [OPTION]... SOURCE... DIRECTORY

(1)正常移動文件

[root@localhost tmp]# mv /root/aaa .  

#移動/root/aaa到當前目錄

[root@localhost tmp]# ls

aaa

(2)重命名

[root@localhost tmp]# ls

aaa

[root@localhost tmp]# mv aaa bbb

[root@localhost tmp]# ls

bbb

(3)移動多個文件(目標必須是dir)

[root@localhost tmp]# ls

bbb

[root@localhost tmp]# mv /root/aaa /root/ccc .   

#移動多個文件到當前目錄

[root@localhost tmp]# ls

aaa  bbb  ccc

5.basename - strip directory and suffix from filenames

[root@localhost tmp]# basename /etc/sysconfig/network

network

6.dirname - strip last component from file name

[root@localhost tmp]# dirname /etc/sysconfig/network

/etc/sysconfig

7.file — determine file type

[root@localhost tmp]# file test

test: ASCII text

[root@localhost tmp]# file /bin/cd

/bin/cd: POSIX shell script, ASCII text executable

[root@localhost tmp]# file /var/lib/mlocate/mlocate.db

/var/lib/mlocate/mlocate.db: data

3.2 查看文件內容

3.2.1 直接查看文件內容

1.cat - concatenate files and print on the standard output

cat [OPTION]... [FILE]..

[root@localhost tmp]# cat /etc/passwd

[root@localhost tmp]# cat -n /etc/passwd    #顯示行號

[root@localhost tmp]# cat -r /etc/passwd    

#倒序的倒序輸出文本內容

2.tac - concatenate and print files in reverse

[root@localhost tmp]# tac  /etc/passwd     #倒序輸出文本內容

3.nl - number lines of files

nl [OPTION]... [FILE]...

[root@localhost tmp]# nl /etc/passwd

3.2.2 可翻頁的查看文件內容

1.more - file perusal filter for crt viewing

more [options] file [...]

[root@localhost tmp]# more /etc/passwd

2.less - opposite of more

[root@localhost tmp]# less /etc/passwd

3.2.3 查看部分文件內容

1.head - output the first part of files

head [OPTION]... [FILE]...

[root@localhost tmp]# head /etc/passwd          

 #顯示前10行內容,默認

[root@localhost tmp]# head -n 5 /etc/passwd     #顯示前5行內容

[root@localhost tmp]# head -n +5 /etc/passwd   #顯示前5行內容

[root@localhost tmp]# head -n -5 /etc/passwd    

#顯示倒數第5行以前的內容

2.tail - output the last part of files

tail [OPTION]... [FILE]...

[root@localhost tmp]# tail /etc/passwd     #顯示最後10行,默認

[root@localhost tmp]# tail -n 5 /etc/passwd      #顯示最後5行

[root@localhost tmp]# tail -n -5 /etc/passwd     #顯示最後5行

[root@localhost tmp]# tail -n +5 /etc/passwd     #從第5行開始顯示

注意:head的最後一個例子與tail的最後一個例子的區別。

[root@localhost tmp]# tail -f /var/log/messages  

#若是message的內容繼續增長,tail將繼續輸出增長的內容。

3.2.4 查看非文本文件內容

1.od - dump files in octal and other formats

od [OPTION]... [FILE]...

[root@localhost tmp]# od /etc/passwd

相關文章
相關標籤/搜索