Linux下有多少種類型的檔案,又如何篩選自己想要的檔案?

Linux系統的檔案型別

在Linux系統中一共有如下7種檔案型別

- 普通檔案

d目錄檔案 directory

l 符號連線檔案 link

b 塊裝置 block

c 字元裝置 character

p 管道檔案 pipe

s 套接字檔案 socket

其中Linux運維架構人員使用最多的是普通檔案、目錄檔案和符號連結檔案,這三種檔案需要熟練掌握。

而塊裝置、字元裝置、管道檔案和套接字是由系統自動生成的,而不是手動建立的,因此瞭解即可。

anaconda-ks。cfg和initial-setup-ks。cfg都是普通檔案

[root@ceonts7-1 ~]# lltotal 8-rw————-。 1 root root 1880 Oct 20 16:05 anaconda-ks。cfg-rw-r——r——。 1 root root 1928 Oct 20 16:05 initial-setup-ks。cfg

/bin 目錄就是連結檔案,也就是Windows上的快捷方式,實際指向的是/usr/bin,透過使用快捷方式可以簡化目錄的訪問。

[root@ceonts7-1 ~]# ll /total 24lrwxrwxrwx。 1 root root 7 Oct 20 15:53 bin -> usr/bindr-xr-xr-x。 6 root root 4096 Oct 20 16:05 bootdrwxr-xr-x。 2 root root 6 Oct 20 15:52 datadrwxr-xr-x。 18 root root 3220 Oct 23 13:04 devdrwxr-xr-x。 139 root root 8192 Oct 23 13:04 etcdrwxr-xr-x。 3 root root 22 Oct 20 16:04 homelrwxrwxrwx。 1 root root 7 Oct 20 15:53 lib -> usr/liblrwxrwxrwx。 1 root root 9 Oct 20 15:53 lib64 -> usr/lib64drwxr-xr-x。 2 root root 6 Apr 11 2018 mediadrwxr-xr-x。 2 root root 6 Apr 11 2018 mntdrwxr-xr-x。 3 root root 16 Oct 20 15:59 optdr-xr-xr-x。 238 root root 0 Oct 23 13:04 procdr-xr-x——-。 5 root root 224 Oct 23 13:50 rootdrwxr-xr-x。 42 root root 1300 Oct 23 13:06 runlrwxrwxrwx。 1 root root 8 Oct 20 15:53 sbin -> usr/sbindrwxr-xr-x。 2 root root 6 Apr 11 2018 srvdr-xr-xr-x。 13 root root 0 Oct 23 13:04 sysdrwxrwxrwt。 40 root root 4096 Oct 23 13:43 tmpdrwxr-xr-x。 13 root root 155 Oct 20 15:53 usrdrwxr-xr-x。 20 root root 282 Oct 20 16:05 var

塊裝置和字元裝置是兩種裝置檔案,都放在/dev目錄下。其中塊裝置是以塊文單位,而字元裝置是以字元為單位。

[root@ceonts7-1 ~]# ll /dev/nvme0n1brw-rw——。 1 root disk 259, 0 Oct 23 13:04 /dev/nvme0n1

[root@ceonts7-1 ~]# ll /dev/zerocrw-rw-rw-。 1 root root 1, 5 Oct 23 13:04 /dev/zero

管道檔案是用於程序之間通訊,只不過只能支援單向通訊

[root@ceonts7-1 ~]# ll /run/dmeventd-clientprw————-。 1 root root 0 Oct 23 13:04 /run/dmeventd-client[root@ceonts7-1 ~]# ll /run/dmeventd-serverprw————-。 1 root root 0 Oct 23 13:04 /run/dmeventd-server

套接字檔案用於程序之間通訊,支援雙向通訊

[root@ceonts7-1 ~]# ll /run/rpcbind。sock srw-rw-rw-。 1 root root 0 Oct 23 13:04 /run/rpcbind。sock

檢視檔案型別

在Windows中可執行檔案的字尾必須是。exe,例如記事本就是notepad。exe,如果嘗試將該檔案的字尾名改成其他的,那麼將會無法雙擊執行。因為Windows對可執行檔案的副檔名必須是。exe。

Linux下有多少種類型的檔案,又如何篩選自己想要的檔案?

而在Linux系統中可執行檔案是沒有副檔名要求的,甚至都是沒有後綴名,比如之前使用過的ls,cd,hostname等命令

[root@centos8-1 ~]# which ls cd hostname #檢視ls cd和hostname這三個命令的路徑alias ls=‘ls ——color=auto’ /usr/bin/ls/usr/bin/cd/usr/bin/hostname

我們甚至可以自己將這些命令改名字,例如將hostname改成 hostname。txt

[root@centos8-1 ~]# mv /usr/bin/hostname /usr/bin/hostname。txt #將hostname改名為hostname。txt

但是hostname。txt它也不是一個文字檔案,而是一個二進位制檔案,依然可以檢視主機名

[root@centos8-1 ~]# hostname。txt # 檢視主機名centos8-1。ittimeline。net[root@centos8-1 ~]# type hostname。txt # 檢視hostname。txt的命令型別hostname。txt is hashed (/usr/bin/hostname。txt)

由於Linux系統對檔名的字尾沒有要求,但是我們可以透過file命令用來檢視7種檔案型別

[root@centos8-1 ~]# file /dev/nvme0 #字元裝置檔案/dev/nvme0: character special (244/0)[root@centos8-1 ~]# file /etc/issue # 文字檔案/etc/issue: ASCII text[root@centos8-1 ~]# file /etc # 目錄檔案/etc: directory[root@centos8-1 ~]# file /usr/bin/hostname。txt #可執行檔案/usr/bin/hostname。txt: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64。so。2, for GNU/Linux 3。2。0, BuildID[sha1]=a8a7ee9d5002492edfc62e3e2e44149e981f9866, stripped[root@centos8-1 ~]# file /bin # 連結檔案/bin: symbolic link to usr/bin

即使是文字,Windows和Linux也是不同的,不同之處主要體現在換行上。

首先我們在Windows上使用記事本建立一個文字檔案,檔案內容就是每行有個大寫字母。

Linux下有多少種類型的檔案,又如何篩選自己想要的檔案?

然後使用MobaXterm傳到Linux伺服器上去

Linux下有多少種類型的檔案,又如何篩選自己想要的檔案?

Linux下有多少種類型的檔案,又如何篩選自己想要的檔案?

上傳以後可以使用file win。txt檢視檔案型別

[root@centos8-1 ~]# file win。txtwin。txt: ASCII text, with CRLF line terminators

然後使用vim linux。txt建立linux。txt,此時不能修改內容,需要透過i鍵進入編輯模式

[root@centos8-1 ~]# vim linux。txt

然後每行輸入三個字母

Linux下有多少種類型的檔案,又如何篩選自己想要的檔案?

輸完以後按esc鍵退出編輯模式,然後按:wq儲存退出即可。

然後使用file linux。txt命令檢視

[root@centos8-1 ~]# file linux。txtlinux。txt: ASCII text

透過file win。txt和file linux。txt的執行結果對比發現,同樣是文字檔案,卻是不同的檔案型別。

而透過hexdump -C可以檢視文字的十六進位制儲存,從中可以看出Windows的換行是0d 0a,而Linux的換行是0a,其中0d表示回車。

[root@centos8-1 ~]# hexdump -C win。txt00000000 41 0d 0a 42 0d 0a 43 |A。。B。。C|00000007[root@centos8-1 ~]# hexdump -C linux。txt00000000 41 0a 42 0a 43 0a |A。B。C。|00000006

透過dos2unix和unix2dos可以實現windows和Linux的文字格式相互轉換

[root@centos8-1 ~]# dos2unix win。txtdos2unix: converting file win。txt to Unix format。。。[root@centos8-1 ~]# file win。txtwin。txt: ASCII text

[root@centos8-1 ~]# unix2dos linux。txtunix2dos: converting file linux。txt to DOS format。。。[root@centos8-1 ~]# file linux。txtlinux。txt: ASCII text, with CRLF line terminators

檔案萬用字元

生產環境的系統中的檔案眾多,可能成百上千,甚至是百萬級,那樣如果執行某些操作會導致系統宕機,因此我們應該篩選出想要操作的檔案。

檔案萬用字元(wildcard)指的是用一些特殊的符號描述符合條件的檔案,方便批次管理檔案。

通配符采用特定的符號,表示特定的含義,此符號稱為元(meta)字元

常見的萬用字元以及含義如下表格所示

萬用字元

含義

*

匹配零個或者多個字元,但是不匹配“。”開頭的隱藏檔案

匹配任意單個字元,一個漢字也算一個字元

~

當前使用者家目錄

~guanglei

使用者guanglei家目錄

當前工作目錄

~-

前一個工作目錄

[0-9]

匹配數字範圍

[a-z]

匹配一個字母,包含大寫字母和小寫字母,大寫字母,但是不包含最後一個大寫字母

[A-Z]

匹配一個大寫字母

[abcdef]

匹配列表中的任意一個字元

[^abcdef]

匹配列表中所有字元以外的字元

[^a-z]

匹配列表中所有字元以外的字元

比如在Windows上我們可以使用everything透過*。java查詢所有。java結尾的檔案

Linux下有多少種類型的檔案,又如何篩選自己想要的檔案?

而在Linux上可以透過

ls *。txt

列出所有文字檔案

[root@centos8-1 ~]# ll *。txt-rw-r——r——。 1 root root 9 Oct 24 09:49 linux。txt-rw-r——r——。 1 root root 5 Oct 24 09:48 win。txt

但是

ll *。txt

無法列出隱藏檔案

[root@centos8-1 ~]# touch 。txt # 建立隱藏檔案[root@centos8-1 ~]# ll *。txt -rw-r——r——。 1 root root 9 Oct 24 09:49 linux。txt-rw-r——r——。 1 root root 5 Oct 24 09:48 win。txt[root@centos8-1 ~]# ls -a。 。。 anaconda-ks。cfg 。bash_history 。bash_logout 。bash_profile 。bashrc 。cache 。config 。cshrc 。dbus initial-setup-ks。cfg linux。txt 。local 。mozilla 。tcshrc 。txt 。viminfo win。txt 。Xauthority

使用

ls -d 。*

列出root使用者家目錄的隱藏檔案

[root@centos8-1 ~]# ls -d 。*。 。。 。bash_history 。bash_logout 。bash_profile 。bashrc 。cache 。config 。cshrc 。dbus 。local 。mozilla 。tcshrc 。txt 。viminfo 。Xauthority

萬用字元的案例:使用

ls ???。txt

列出三個字元的檔案

[root@centos8-1 ~]# ll ???。txt #列出三個字元的檔案-rw-r——r——。 1 root root 5 Oct 24 09:48 win。txt[root@centos8-1 ~]# touch 劉光磊。txt # 建立名為劉光磊。txt的文字檔案[root@centos8-1 ~]# ll ???。txt-rw-r——r——。 1 root root 5 Oct 24 09:48 win。txt-rw-r——r——。 1 root root 0 Oct 24 10:58 劉光磊。txt

萬用字元的案例:使用

ls file[liu]。txt

檢視filei。txt filel。txt fileu。txt

[root@centos8-1 ~]# cd /data/[root@centos8-1 data]# echo file{a。。z}。txtfilea。txt fileb。txt filec。txt filed。txt filee。txt filef。txt fileg。txt fileh。txt filei。txt filej。txt filek。txt filel。txt filem。txt filen。txt fileo。txt filep。txt fileq。txt filer。txt files。txt filet。txt fileu。txt filev。txt filew。txt filex。txt filey。txt filez。txt[root@centos8-1 data]# touch file{a。。z}。txt[root@centos8-1 data]# lsfilea。txt filec。txt filee。txt fileg。txt filei。txt filek。txt filem。txt fileo。txt fileq。txt files。txt fileu。txt filew。txt filey。txtfileb。txt filed。txt filef。txt fileh。txt filej。txt filel。txt filen。txt filep。txt filer。txt filet。txt filev。txt filex。txt filez。txt[root@centos8-1 data]# ls file[liu]。txtfilei。txt filel。txt fileu。txt

萬用字元案例:使用

ls file[a-d]。txt

檢視filea。txt fileA。txt fileb。txt fileB。txt filec。txt fileC。txt filed。txt,[a-d]包含a-d之間的小寫字母以及A,B,C,但是不包含D

[root@centos8-1 data]# touch file{a。。z}。txt[root@centos8-1 data]# touch file{A。。Z}。txt[root@centos8-1 data]# ls file[a-d]。txtfilea。txt fileA。txt fileb。txt fileB。txt filec。txt fileC。txt filed。txt

使用

ll file[a-z]。txt

列出滿足條件都檔案時 fileZ。txt沒有列出,證明不會包含最後一個大寫字母。

[root@centos8-1 data]# ls file[a-z]。txtfilea。txt fileB。txt filed。txt fileE。txt fileg。txt fileH。txt filej。txt fileK。txt filem。txt fileN。txt filep。txt fileQ。txt files。txt fileT。txt filev。txt fileW。txt filey。txtfileA。txt filec。txt fileD。txt filef。txt fileG。txt filei。txt fileJ。txt filel。txt fileM。txt fileo。txt fileP。txt filer。txt fileS。txt fileu。txt fileV。txt filex。txt fileY。txtfileb。txt fileC。txt filee。txt fileF。txt fileh。txt fileI。txt filek。txt fileL。txt filen。txt fileO。txt fileq。txt fileR。txt filet。txt fileU。txt filew。txt fileX。txt filez。txt

萬用字元案例:檢視/etc目錄下的所有隱藏檔案

[root@centos8-1 data]# ls -d /etc/。* # 檢視/etc目錄下的所有隱藏檔案 -d表示有資料夾只顯示資料夾,而不進入資料夾/etc/。 /etc/。。 /etc/。pwd。lock /etc/。updated

另外還有在Linux系統中預定義的字元,它們也有特定的含義

預定義字元

含義

[:digit:]

任意數字,相當於0-9

[:lower:]

任意小寫字母,表示 a-z

[:upper:]

任意大寫字母,表示 A-Z

[:alpha:]

任意大小寫字母

[:alnum:]

任意數字或字母

[:blank:]

水平空白字元

[:space:]

水平或垂直空白字元

[:punct:]

標點符號

[:print:]

可列印字元

[:cntrl:]

控制(非列印)字元

[:graph:]

圖形字元

[:xdigit:]

六進位制字元

預定義字元的案例:列出所有file後面跟著小寫字母的檔案

[root@centos8-1 ~]# touch file{a。。z}。txt[root@centos8-1 data]# ls file[[:lower:]]。txt #列出所有file後面跟著小寫字母的檔案filea。txt filec。txt filee。txt fileg。txt filei。txt filek。txt filem。txt fileo。txt fileq。txt files。txt fileu。txt filew。txt filey。txtfileb。txt filed。txt filef。txt fileh。txt filej。txt filel。txt filen。txt filep。txt filer。txt filet。txt filev。txt filex。txt filez。txt

Linux下有多少種類型的檔案,又如何篩選自己想要的檔案?

命令執行結果

預定義字元的案例:列出所有file後面跟著大寫字母的檔案

[root@centos8-1 ~]# touch file{A。。Z}。txt[root@centos8-1 data]# ls file[[:upper:]]。txt #列出所有file後面跟著大寫字母的檔案fileA。txt fileC。txt fileE。txt fileG。txt fileI。txt fileK。txt fileM。txt fileO。txt fileQ。txt fileS。txt fileU。txt fileW。txt fileY。txtfileB。txt fileD。txt fileF。txt fileH。txt fileJ。txt fileL。txt fileN。txt fileP。txt fileR。txt fileT。txt fileV。txt fileX。txt fileZ。txt

Linux下有多少種類型的檔案,又如何篩選自己想要的檔案?

命令執行結果

預定義字元的案例:列出所有file後面跟著字母的檔案

[root@centos8-1 ~]# touch file{a。。z}。txt[root@centos8-1 ~]# touch file{A。。Z}。txt[root@centos8-1 data]# ls file[[:alpha:]]。txt #列出所有file後面跟著字母的檔案filea。txt fileB。txt filed。txt fileE。txt fileg。txt fileH。txt filej。txt fileK。txt filem。txt fileN。txt filep。txt fileQ。txt files。txt fileT。txt filev。txt fileW。txt filey。txt fileZ。txtfileA。txt filec。txt fileD。txt filef。txt fileG。txt filei。txt fileJ。txt filel。txt fileM。txt fileo。txt fileP。txt filer。txt fileS。txt fileu。txt fileV。txt filex。txt fileY。txtfileb。txt fileC。txt filee。txt fileF。txt fileh。txt fileI。txt filek。txt fileL。txt filen。txt fileO。txt fileq。txt fileR。txt filet。txt fileU。txt filew。txt fileX。txt filez。txt

Linux下有多少種類型的檔案,又如何篩選自己想要的檔案?

命令執行結果