Linux檔案型別(學習筆記六)

weixin_33831673發表於2015-02-19

一、Linux下的檔案型別

  • 普通檔案:在由 ls –al 所顯示出來的屬性方面,第一個屬性為 [ - ]
  • 目錄檔案:在由 ls –al 所顯示出來的屬性方面,第一個屬性為 [ d ]
  • 裝置檔案:一般都在dev目錄,有[b][c]

二、普通檔案

2.1、文字檔案

以ASCII碼形式儲存在計算機中

2.2、二進位制檔案

以文字的二進位制形式儲存在計算機中,如聲音,影像等,只有通過相應的軟體才能將其顯示出來

2.3、連線檔案

就是類似 Windows 底下的快捷方式啦!第一個屬性為 [ l ];

三、目錄檔案

設計目錄檔案的主要目的是用於管理和組織系統中的大量檔案,它儲存一組相關檔案的位置,大小與檔案 有關的資訊目錄檔案簡稱目錄

四、裝置檔案

裝置檔案是Linux系統很重要的一個角色,Linux把每個I/O裝置看成一個檔案,與普通檔案一樣處理,這樣可以使用檔案 與裝置的操作儘可能統一,從使用者角度來看,對I/O裝置的使用和一般檔案使用一樣,使用者不必瞭解I/O細節,

裝置檔案又為塊裝置、和字元裝置

4.1、塊裝置

成塊讀取資料的裝置,硬碟,記憶體等 就是一些儲存資料,以提供系統存取的介面裝置,簡單的說就是硬碟啦!

例如你的一號硬碟的程式碼是 /dev/hda1 等等的檔案啦!第一個屬性為 [ b ]

4.2、字元裝置

 亦即是一些串列埠的介面裝置,例如鍵盤、滑鼠等等!第一個屬性為 [ c ]。

五、檔案相關的一些命令

5.1、touch

建立新檔案

touch /data/text.txta

ls /data 檢視

或者進入/data目錄後,再建立

cd /data

touch text.txt

建立多個檔案

touch 1.txt 2.txt 3.txt

或者

touch {a,b,c,d}.txt

touch /data/{q,w,e,r,t,y}.txt

大於號建立

>c.txt

重定向

ehco > a.txt

5.2、cp

複製檔案或者目錄

 

cp a.txt test4   //複製檔案到test4檔案下
[root@model Documents]# cp a.txt hh.txt test test4/ -i
cp: overwrite `test4/a.txt'? y    //有相同檔案是提示覆蓋
[root@model Documents]# ll test4     //檢視tests4目錄
total 8
-rw-r--r--. 1 root root 23 Feb 20 06:09 a.txt
-rw-r--r--. 1 root root  0 Feb 20 06:09 hh.txt
-rw-r--r--. 1 root root 71 Feb 20 06:09 test
[root@model Documents]# 
[root@model Documents]# cp test4/  tests/  -rip     //將test4整個目錄cp到tests目錄下
[root@model Documents]# ls tests/
a.txt  c.txt  test1  test4  x.txt  z.txt
[root@model Documents]# ll tests/
total 12
-rw-r--r--. 1 root root   23 Feb 20 05:10 a.txt
-rw-r--r--. 1 root root    0 Feb 20 05:11 c.txt
drwxr-xr-x. 3 root root 4096 Feb 20 01:55 test1
drwxr-xr-x. 2 root root 4096 Feb 20 06:09 test4
-rw-r--r--. 1 root root    0 Feb 20 05:11 x.txt
-rw-r--r--. 1 root root    0 Feb 20 05:11 z.txt
[root@model Documents]# 

5.3、rm

刪除檔案或者目錄

[root@model Documents]# ll
total 32
-rw-r--r--. 1 root root   23 Feb 19  2015 a.txt
drwxr-xr-x. 4 root root 4096 Feb 20  2015 com
drwxr-xr-x. 3 root root 4096 Feb 20  2015 dir2
-rw-r--r--. 1 root root    0 Feb 20  2015 file1.txt
-rw-r--r--. 1 root root    0 Feb 20  2015 file2.doc
-rw-r--r--. 1 root root    0 Feb 20  2015 hh.txt
-rw-r--r--. 1 root root   21 Feb 20  2015 n.txt~
-rw-r--r--. 1 root root   71 Feb 20  2015 test
drwxr-xr-x. 3 root root 4096 Feb 20  2015 test1
drwxr-xr-x. 2 root root 4096 Feb 20  2015 test4
drwxr-xr-x. 4 root root 4096 Feb 20  2015 tests
[root@model Documents]# rm -rf n.txt~          //刪除檔案不提示
[root@model Documents]# ll
total 28
-rw-r--r--. 1 root root   23 Feb 19  2015 a.txt
drwxr-xr-x. 4 root root 4096 Feb 20  2015 com
drwxr-xr-x. 3 root root 4096 Feb 20  2015 dir2
-rw-r--r--. 1 root root    0 Feb 20  2015 file1.txt
-rw-r--r--. 1 root root    0 Feb 20  2015 file2.doc
-rw-r--r--. 1 root root    0 Feb 20  2015 hh.txt
-rw-r--r--. 1 root root   71 Feb 20  2015 test
drwxr-xr-x. 3 root root 4096 Feb 20  2015 test1
drwxr-xr-x. 2 root root 4096 Feb 20  2015 test4
drwxr-xr-x. 4 root root 4096 Feb 20  2015 tests
[root@model Documents]# 
ls
a.txt  dir2       file2.doc  test   test4
com    file1.txt  hh.txt     test1  tests
[root@model Documents]# rm hh.txt            //刪除檔案會預設提示
rm: remove regular empty file `hh.txt'? y
[root@model Documents]# 
[root@model Documents]# ll test5
total 0
-rw-r--r--. 1 root root 0 Feb 19 22:26 1.txt
-rw-r--r--. 1 root root 0 Feb 19 22:26 2.txt
-rw-r--r--. 1 root root 0 Feb 19 22:26 3.txt
-rw-r--r--. 1 root root 0 Feb 19 22:26 4.txt
-rw-r--r--. 1 root root 0 Feb 19 22:26 5.txt
[root@model Documents]# rm -rf test5      //刪除整個目錄不提示檔案一起刪除
[root@model Documents]# ll
total 28
-rw-r--r--. 1 root root   23 Feb 19  2015 a.txt
drwxr-xr-x. 4 root root 4096 Feb 20  2015 com
drwxr-xr-x. 3 root root 4096 Feb 20  2015 dir2
-rw-r--r--. 1 root root    0 Feb 20  2015 file1.txt
-rw-r--r--. 1 root root    0 Feb 20  2015 file2.doc
-rw-r--r--. 1 root root   71 Feb 20  2015 test
drwxr-xr-x. 3 root root 4096 Feb 20  2015 test1
drwxr-xr-x. 2 root root 4096 Feb 20  2015 test4
drwxr-xr-x. 4 root root 4096 Feb 20  2015 tests
[root@model Documents]# 

5.4、mv

移動檔案或者目錄

常用引數:

-b :若需覆蓋檔案,則覆蓋前先行備份。 

-f :force 強制的意思,如果目標檔案已經存在,不會詢問而直接覆蓋;

-i :若目標檔案 (destination) 已經存在時,就會詢問是否覆蓋!

-u :若目標檔案已經存在,且 source 比較新,才會更新(update)

[root@model Documents]# ll
total 28
-rw-r--r--. 1 root root   23 Feb 19  2015 a.txt
drwxr-xr-x. 4 root root 4096 Feb 20  2015 com
drwxr-xr-x. 3 root root 4096 Feb 20  2015 dir2
-rw-r--r--. 1 root root    0 Feb 20  2015 file1.txt
-rw-r--r--. 1 root root    0 Feb 20  2015 file2.doc
-rw-r--r--. 1 root root   71 Feb 20  2015 test
drwxr-xr-x. 3 root root 4096 Feb 20  2015 test1
drwxr-xr-x. 2 root root 4096 Feb 20  2015 test4
drwxr-xr-x. 4 root root 4096 Feb 20  2015 tests
[root@model Documents]# mv a.txt b.doc     //檔案重新命名
[root@model Documents]# ll
total 28
-rw-r--r--. 1 root root   23 Feb 19  2015 b.doc
drwxr-xr-x. 4 root root 4096 Feb 20  2015 com
drwxr-xr-x. 3 root root 4096 Feb 20  2015 dir2
-rw-r--r--. 1 root root    0 Feb 20  2015 file1.txt
-rw-r--r--. 1 root root    0 Feb 20  2015 file2.doc
-rw-r--r--. 1 root root   71 Feb 20  2015 test
drwxr-xr-x. 3 root root 4096 Feb 20  2015 test1
drwxr-xr-x. 2 root root 4096 Feb 20  2015 test4
drwxr-xr-x. 4 root root 4096 Feb 20  2015 tests
[root@model Documents]# 
[root@model Documents]# ls
b.doc  dir2       file2.doc  test1  tests
com    file1.txt  test       test4
[root@model Documents]# mv b.doc  test4/yy.txt   //移動檔案並重新命名
[root@model Documents]# ll test4/
total 12
-rw-r--r--. 1 root root 23 Feb 20  2015 a.txt
-rw-r--r--. 1 root root  0 Feb 20  2015 hh.txt
-rw-r--r--. 1 root root 71 Feb 20  2015 test
-rw-r--r--. 1 root root 23 Feb 19  2015 yy.txt
[root@model Documents]# 
[root@model Documents]# ll
total 24
drwxr-xr-x. 4 root root 4096 Feb 20  2015 com
drwxr-xr-x. 3 root root 4096 Feb 20  2015 dir2
-rw-r--r--. 1 root root    0 Feb 20  2015 file1.txt
-rw-r--r--. 1 root root    0 Feb 20  2015 file2.doc
-rw-r--r--. 1 root root   71 Feb 20  2015 test
drwxr-xr-x. 3 root root 4096 Feb 20  2015 test1
drwxr-xr-x. 2 root root 4096 Feb 19 22:32 test4
drwxr-xr-x. 4 root root 4096 Feb 20  2015 tests
[root@model Documents]# mv dir2/  test1/      //移動整個資料夾
[root@model Documents]# ll test1/
total 20
-rw-r--r--. 1 root root   23 Feb 20  2015 a.txt
-rw-r--r--. 1 root root   71 Feb 20  2015 b.txt
drwxr-xr-x. 3 root root 4096 Feb 20  2015 dir2
-rw-r--r--. 1 root root   87 Feb 20  2015 n.txt
drwxr-xr-x. 2 root root 4096 Feb 19 22:32 test2
[root@model Documents]# 

5.5、find

查詢檔案或者目錄

 

  • -name:可以用萬用字元來查詢如:* ?
  • -size:可以用+,-來設定大小+表示大於多少,-表示少於多少
  • -user:屬於哪個使用者
  • -type:檔案型別,普通檔案有f,目錄用d,塊裝置用b,字元裝置用c
[root@model Documents]# find /etc/ -name resol*.conf   //-name使用
/etc/resolv.conf
/etc/sysconfig/networking/profiles/default/resolv.conf
[root@model Documents]# 
[root@model Documents]# find /boot/ -type d       //-type使用查詢boot目錄下的所有目錄
/boot/
/boot/efi
/boot/efi/EFI
/boot/efi/EFI/redhat
/boot/lost+found
/boot/grub
[root@model Documents]# 
//查詢當前目錄中屬於root使用者的.txt的所有檔案
[root@model test1]# ll
total 20
-rw-r--r--. 1 root root   23 Feb 20  2015 a.txt
-rw-r--r--. 1 root root   71 Feb 20  2015 b.txt
drwxr-xr-x. 3 root root 4096 Feb 20  2015 dir2
-rw-r--r--. 1 root root   87 Feb 20  2015 n.txt
drwxr-xr-x. 2 root root 4096 Feb 19 22:32 test2
[root@model test1]# find ./*.txt -user root
./a.txt
./b.txt
./n.txt
[root@model test1]# 

5.6、which

查詢命令檔案的位置

//查詢命令所在位置
[root@model test1]# which ls
alias ls='ls --color=auto'
        /bin/ls
[root@model test1]# which cat
/bin/cat
[root@model test1]# which rm
alias rm='rm -i'
        /bin/rm
[root@model test1]# which touch
/bin/touch
[root@model test1]# which mv
alias mv='mv -i'
        /bin/mv
[root@model test1]# 

5.7、file

檢視檔案型別

[root@model test1]# file a.txt 
a.txt: ASCII text
[root@model test1]# file dir2/
dir2/: directory
[root@model test1]# 
[root@model test1]# file ls
ls: cannot open `ls' (No such file or directory)
[root@model test1]# which ls
alias ls='ls --color=auto'
        /bin/ls
[root@model test1]# file /bin/ls
/bin/ls: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.18, stripped
[root@model test1]# file /etc/init.d/network 
/etc/init.d/network: Bourne-Again shell script text executable
[root@model test1]# file /dev/sda1
/dev/sda1: block special
[root@model test1]# file /dev/lp3
/dev/lp3: character special
[root@model test1]# 

5.8、ln

建立快捷方式:硬連線和符號連線(軟體連線)

不能對目錄,建立硬連線,也不能跨越分割槽建立硬鏈拉檔案

 

建立軟體連線時需要加引數[-s]

如果你用ls察看一個目錄時,發現有的檔案後面有一個@的符號,那就是一個用ln命令生成的檔案

[root@model Downloads]# ln ../Documents/test1/a.txt 
[root@model Downloads]# ll
total 4
-rw-r--r--. 2 root root 23 Feb 20  2015 a.txt

相關文章