1. 描述Linux發(fā)行版的系統(tǒng)目錄名稱命名規(guī)則以及用途。
1.1. FHS目錄結(jié)構(gòu)
掛載點(diǎn) | 功能 |
---|---|
/boot | 引導(dǎo)文件存放目錄,內(nèi)核文件(vmlinuz)洽瞬、引導(dǎo)加載器(bootloader, grub) 都存放于此目錄 |
/bin | 所有用戶使用的基本命令秩仆;不能關(guān)聯(lián)至獨(dú)立分區(qū),OS啟動(dòng)即會(huì)用到的程序 |
/sbin | 管理類的基本命令烹笔;不能關(guān)聯(lián)至獨(dú)立分區(qū)裳扯,OS啟動(dòng)即會(huì)用到的程序 |
/lib | 啟動(dòng)時(shí)程序依賴的基本共享庫(kù)文件以及內(nèi)核模塊文件(/lib/modules) |
/lib64 | 專用于x86_64系統(tǒng)上的輔助共享庫(kù)文件存放位置 |
/etc | 配置文件目錄 |
/home/USERNAME | 普通用戶家目錄 |
/root | 管理員的家目錄 |
/media | 便攜式移動(dòng)設(shè)備掛載點(diǎn) |
/mnt | 臨時(shí)文件系統(tǒng)掛載點(diǎn) |
/dev | 設(shè)備文件及特殊文件存儲(chǔ)位置 |
- | b: block device,隨機(jī)訪問(wèn) |
- | c: character device谤职,線性訪問(wèn) |
/opt | 第三方應(yīng)用程序的安裝位置 |
/srv | 系統(tǒng)上運(yùn)行的服務(wù)用到的數(shù)據(jù) |
/tmp | 臨時(shí)文件存儲(chǔ)位置 |
/usr | universal shared, read-only data |
/var | variable data files |
/proc | 用于輸出內(nèi)核與進(jìn)程信息相關(guān)的虛擬文件系統(tǒng) |
/sys | 用于輸出當(dāng)前系統(tǒng)上硬件設(shè)備相關(guān)信息虛擬文件系統(tǒng) |
/selinux | security enhanced Linux饰豺,selinux相關(guān)的安全策略等信息的存儲(chǔ)位置 |
1.1.1. /usr/
bin: 保證系統(tǒng)擁有完整功能而提供的應(yīng)用程序
sbin:
lib:32 位 使 用 lib64:只存在64位系統(tǒng)
include: C 程 序 的 頭 文 件 (header files) share:結(jié)構(gòu)化獨(dú)立的數(shù)據(jù),例如doc, man等local:第三方應(yīng)用程序的安裝位置
bin, sbin, lib, lib64, etc, share
1.1.2. /var/
cache: 應(yīng)用程序緩存數(shù)據(jù)目錄
lib: 應(yīng)用程序狀態(tài)信息數(shù)據(jù)
local:專用于為/usr/local下的應(yīng)用程序存儲(chǔ)可變數(shù)據(jù)lock: 鎖文件
log: 日志目錄及文件
opt: 專用于為/opt下的應(yīng)用程序存儲(chǔ)可變數(shù)據(jù)
run: 運(yùn)行中的進(jìn)程相關(guān)數(shù)據(jù),通常用于存儲(chǔ)進(jìn)程pid文件
spool: 應(yīng)用程序數(shù)據(jù)池
tmp: 保存系統(tǒng)兩次重啟之間產(chǎn)生的臨時(shí)數(shù)據(jù)
1.1.3. CentOS7目錄變化[以下目錄為軟連接]
/bin 和 /usr/bin
/sbin 和 /usr/sbin
/lib 和/usr/lib
/lib64 和 /usr/lib64
2. 描述文件的元數(shù)據(jù)信息有哪些允蜈,分別表示什么含義冤吨,如何查看?如何修改文件的時(shí)間戳信息饶套?
2.1. 文件元數(shù)據(jù)
以下基于ext文件系統(tǒng)進(jìn)行描述
inode number : 節(jié)點(diǎn)編號(hào)
file type : 文件類型(普通文件,目錄,連接,設(shè)備,管道...)
permission : 權(quán)限(0644,0755,1777等12位權(quán)限位)
link count : 硬鏈接數(shù)
uid : 屬主信息
gid : 屬組信息
size : 文件大小
file date : 文件的日期時(shí)間(創(chuàng)建時(shí)間,修改時(shí)間,改變時(shí)間,訪問(wèn)時(shí)間)
block : 數(shù)據(jù)塊大小及占用數(shù)據(jù)塊數(shù)量
2.2. 查看元數(shù)據(jù)方法
使用stat FILENAME進(jìn)行查看
stat anaconda-ks.cfg
File: ‘a(chǎn)naconda-ks.cfg’
Size: 1664 Blocks: 8 IO Block: 4096 regular file
Device: 802h/2050d Inode: 50331715 Links: 1
Access: (0600/-rw-------) Uid: ( 0/ root) Gid: ( 0/ root)
Context: system_u:object_r:admin_home_t:s0
Access: 2020-05-17 10:48:06.964028005 +0800
Modify: 2020-05-04 21:04:18.103138486 +0800
Change: 2020-05-04 21:04:18.103138486 +0800
Birth: -
2.3. 修改文件時(shí)間戳
使用touch命令進(jìn)行修改
Usage:touch [OPTION]... FILE...
-a 僅改變 atime,同時(shí)會(huì)修改ctime
-m 僅改變 mtime,同時(shí)會(huì)修改ctime
-t 修改為指定日期時(shí)間,格式為 [[CC]YY]MMDDhhmm[.ss]
# 查看文件信息
stat statfile
File: ‘statfile’
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: 802h/2050d Inode: 50331715 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Context: unconfined_u:object_r:admin_home_t:s0
Access: 2020-06-21 07:00:46.645166801 +0800
Modify: 2020-06-21 07:00:46.645166801 +0800
Change: 2020-06-21 07:00:46.645166801 +0800
Birth: -
# 修改atime訪問(wèn)時(shí)間
touch -a -t 202006011359.59 statfile
stat statfile
File: ‘statfile’
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: 802h/2050d Inode: 50331715 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Context: unconfined_u:object_r:admin_home_t:s0
Access: 2020-06-01 13:59:59.000000000 +0800
Modify: 2020-06-21 07:00:46.645166801 +0800
Change: 2020-06-21 07:02:07.687172798 +0800
Birth: -
# 修改mtime修改時(shí)間
touch -m -t 202006180000.01 statfile
stat statfile
File: ‘statfile’
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: 802h/2050d Inode: 50331715 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Context: unconfined_u:object_r:admin_home_t:s0
Access: 2020-06-01 13:59:59.000000000 +0800
Modify: 2020-06-18 00:00:01.000000000 +0800
Change: 2020-06-21 07:03:05.121177048 +0800
Birth: -
3. 總結(jié)軟連接和硬連接區(qū)別漩蟆,并用實(shí)例操作說(shuō)明。
3.1. 軟連接與硬鏈接的區(qū)別
硬鏈接
創(chuàng)建硬鏈接會(huì)增加額外的記錄項(xiàng)以引用文件
對(duì)應(yīng)于同一文件系統(tǒng)上一個(gè)物理文件
每個(gè)目錄引用相同的inode號(hào)
創(chuàng)建時(shí)鏈接數(shù)遞增
刪除文件時(shí):
? rm命令遞減計(jì)數(shù)的鏈接
? 文件要存在妓蛮,至少有一個(gè)鏈接數(shù)
? 當(dāng)鏈接數(shù)為零時(shí)怠李,該文件被刪除
不能跨越驅(qū)動(dòng)器或分區(qū)
語(yǔ)法:
ln filename [linkname ]
軟連接
一個(gè)符號(hào)鏈接指向另一個(gè)文件
ls - l的 顯示鏈接的名稱和引用的文件
一個(gè)符號(hào)鏈接的內(nèi)容是它引用文件的名稱
可以對(duì)目錄進(jìn)行
可以跨分區(qū)
指向的是另一個(gè)文件的路徑;其大小為指向的路徑字符串的長(zhǎng)度仔引;不增加或減少目標(biāo)文件inode的引用計(jì)數(shù)
語(yǔ)法:
ln -s filename [linkname]
3.2. 操作實(shí)例
# 硬鏈接
ln statfile hardlinkfile
ls -il
50331715 -rw-r--r--. 2 root root 0 Jun 18 00:00 hardlinkfile
50331715 -rw-r--r--. 2 root root 0 Jun 18 00:00 statfile
# 軟連接
ls -il statfile softlinkfile
50331820 lrwxrwxrwx. 1 root root 8 Jun 21 07:13 softlinkfile -> statfile
50331715 -rw-r--r--. 2 root root 0 Jun 18 00:00 statfile
4. Linux上的文件管理類命令都有哪些扔仓,其常用的使用方法及其相關(guān)示例演示。
4.1. ls列出文件目錄
Usage: ls [OPTION]... [FILE]...
-a:列出所有文件
-A:列出除.和..外的所有文件
-d:僅列出目錄本身的屬性
-l:使用長(zhǎng)格式
-S:以文件大小排序,從大到小
-r:反向排序
-t:安裝mtime排序
-h,--human-readable:對(duì)文件大小進(jìn)行單位轉(zhuǎn)換咖耘,但轉(zhuǎn)換后可能會(huì)損失精度-b
# 使用長(zhǎng)格式,列出含有隱藏屬性的文件
ls -la .
total 32
dr-xr-x---. 2 root root 196 Jun 21 07:13 .
dr-xr-xr-x. 18 root root 236 May 4 21:03 ..
-rw-------. 1 root root 2133 Jun 21 06:23 .bash_history
-rw-r--r--. 1 root root 18 Dec 29 2013 .bash_logout
-rw-r--r--. 1 root root 176 Dec 29 2013 .bash_profile
-rw-r--r--. 1 root root 176 Dec 29 2013 .bashrc
-rw-r--r--. 1 root root 100 Dec 29 2013 .cshrc
-rw-r--r--. 2 root root 0 Jun 18 00:00 hardlinkfile
-rw-------. 1 root root 1024 May 17 15:41 .rnd
lrwxrwxrwx. 1 root root 8 Jun 21 07:13 softlinkfile -> statfile
-rw-r--r--. 2 root root 0 Jun 18 00:00 statfile
-rw-r--r--. 1 root root 129 Dec 29 2013 .tcshrc
-rw-------. 1 root root 577 May 17 15:14 .viminfo
# 使用長(zhǎng)格式,列出不含.和..的文件,從小到大排序
ls -AlSr .
total 32
-rw-r--r--. 2 root root 0 Jun 18 00:00 statfile
-rw-r--r--. 2 root root 0 Jun 18 00:00 hardlinkfile
lrwxrwxrwx. 1 root root 8 Jun 21 07:13 softlinkfile -> statfile
-rw-r--r--. 1 root root 18 Dec 29 2013 .bash_logout
-rw-r--r--. 1 root root 100 Dec 29 2013 .cshrc
-rw-r--r--. 1 root root 129 Dec 29 2013 .tcshrc
-rw-r--r--. 1 root root 176 Dec 29 2013 .bashrc
-rw-r--r--. 1 root root 176 Dec 29 2013 .bash_profile
-rw-------. 1 root root 577 May 17 15:14 .viminfo
-rw-------. 1 root root 1024 May 17 15:41 .rnd
-rw-------. 1 root root 2133 Jun 21 06:23 .bash_history
# 僅列出目錄本身屬性
ls -ld /etc/
drwxr-xr-x. 73 root root 8192 Jun 21 06:23 /etc/
# 長(zhǎng)格式顯示/etc/下修改時(shí)間從舊到新排序
ls -ltr /etc/
4.2. tree列出文件目錄樹(shù)
Usage: [OPTION]... [directory...]
-L:目錄層級(jí)深度
-d:僅包含目錄,不含文件
# 顯示當(dāng)前路徑目錄樹(shù)
tree .
.
├── hardlinkfile
├── softlinkfile -> statfile
└── statfile
# 顯示/etc/下僅包含的目錄,深度為2
tree -L 2 -d /etc/
4.3. stat顯示文件元數(shù)據(jù)
display file or file system status 獲取指定文件的元數(shù)據(jù)
詳見(jiàn)2.3節(jié)提供的案例
4.4. file查看文件類型
查看文件真實(shí)類型用于確認(rèn)文件內(nèi)容組織格式的類型
~]# 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.32, BuildID[sha1]=3d705971a4c4544545cb78fd890d27bf792af6d4, stripped
?
~]# file /dev
/dev: directory
?
~]# file /dev/sda
/dev/sda: block special
4.5. mkdir創(chuàng)建目錄
Usage: mkdir [OPTION]... DIRECTORY...
-p:遞歸創(chuàng)建目錄
-v:顯示創(chuàng)建信息
~]# mkdir -v a
mkdir: created directory ‘a(chǎn)’
~]# mkdir a/b/c
mkdir: cannot create directory ‘a(chǎn)/b/c’: No such file or directory
~]# mkdir -p a/b/c
4.6. rmdir刪除空目錄
rmdir - remove empty directories
-p: 遞歸刪除空目錄的父目錄
-v:顯示刪除信息
~]# tree
.
├── a
│ └── b
│ └── c
├── hardlinkfile
├── softlinkfile -> statfile
└── statfile
3 directories, 3 files
~]# rmdir -p a/b/c
~]# tree
.
├── hardlinkfile
├── softlinkfile -> statfile
└── statfile
4.7. cp文件復(fù)制
4.7.1. 基本命令
單源復(fù)制
cp [OPTION]... [-T] SOURCE DEST
|-- 如果DEST不存在:則事先創(chuàng)建此文件翘簇,并將源文件的數(shù)據(jù)流填充至DEST中;
|-- 如果DEST存在:
|-- 如果DEST是非目錄文件:則覆蓋目標(biāo)文件儿倒;
|-- 如果DEST是目錄文件:則先在DEST目錄下創(chuàng)建一個(gè)與源文件同名的文件版保,并復(fù)制其數(shù)據(jù)流;
多源復(fù)制
cp [OPTION]... SOURCE... DIRECTORY
cp [OPTION]... -t DIRECTORY SOURCE...
|-- 如果SOURCE是目錄:默認(rèn)不復(fù)制目錄【cp: omitting directory】
|-- 如果DEST不存在:錯(cuò)誤夫否;
|-- 如果DEST存在:
|-- 如果DEST是非目錄文件:錯(cuò)誤彻犁;
|-- 如果DEST是目錄文件:分別復(fù)制每個(gè)文件至目標(biāo)目錄中,并保持原名凰慈;
OPTIONS:
-i:交互式復(fù)制汞幢,即覆蓋之前提醒用戶確認(rèn);
-f:強(qiáng)制覆蓋目標(biāo)文件微谓;
-r, -R:遞歸復(fù)制目錄森篷;
-d:復(fù)制符號(hào)鏈接文件本身输钩,而非其指向的源文件;
-a:-dR --preserve=all, archive仲智,用于實(shí)現(xiàn)歸檔买乃;
--preserv=
mode:權(quán)限
ownership:屬主和屬組
timestamps: 時(shí)間戳
context:安全標(biāo)簽
xattr:擴(kuò)展屬性
links:符號(hào)鏈接
all:上述所有屬性
4.7.2. 實(shí)例
1、單源復(fù)制:DEST不存在
~]# ls /tmp/
~]# cp /etc/fstab /tmp/noDEST
~]# ls /tmp/
noDEST
2钓辆、單源復(fù)制:DEST存在為目錄
~]# ls /tmp/new/
~]# cp /etc/fstab /tmp/new/
~]# ls /tmp/new/
fstab
3剪验、單源復(fù)制:DEST存在為文件
~]# cp /etc/fstab /tmp/newfile # 復(fù)制第一次
~]# ls /tmp/
newfile
~]# cp /etc/fstab /tmp/newfile # 復(fù)制第二次
cp: overwrite ‘/tmp/newfile’? y
~]# cp -f /etc/fstab /tmp/newfile # -f強(qiáng)制覆蓋失敗[為何?]
cp: overwrite ‘/tmp/newfile’? y
~]# \cp -f /etc/fstab /tmp/newfile # 強(qiáng)行取消alias復(fù)制成功
4、多源復(fù)制:DIRECTORY存在
[root@bogon ~]# mkdir /tmp/new
[root@bogon ~]# ls /tmp/new/
[root@bogon ~]# cp /etc/fstab /etc/passwd /tmp/new/
[root@bogon ~]# ls /tmp/new/
fstab passwd
5前联、多源復(fù)制:DIRECTORY不存在
[root@bogon ~]# ls /tmp/
[root@bogon ~]# cp /etc/passwd /etc/fstab /tmp/new
cp: target ‘/tmp/new’ is not a directory
4.8. mv文件移到
mv [OPTION]... [-T] SOURCE DEST
mv [OPTION]... SOURCE... DIRECTORY
mv [OPTION]... -t DIRECTORY SOURCE...
OPTIONS:
-i:交互式功戚;
-f:force
# 文件/目錄重命名
~]# touch /tmp/abc
~]# mv /tmp/abc /tmp/def
# 移動(dòng)目錄
~]# mkdir -p /usr/local/src/abc/file
~]# mv /usr/local/src/abc/ /tmp/
~]# ls /tmp/
abc def
4.9. rm文件刪除
rm [OPTION]... FILE...
OPTIONS:
-i:interactive
-f:force
-r: recursive
刪除目錄:rm -rf /PATH/TO/DIR
|-- 危險(xiǎn)操作:rm -rf /*
|-- 注意:所有不用的文件建議不要直接刪除,而是移動(dòng)至某個(gè)專用目錄似嗤;(模擬回收站)
5. 復(fù)制/etc/profile至/tmp/目錄疫铜,用查找替換命令刪除/tmp/profile文件中的 行首的空白字符
cp /etc/profile /tmp/
sed -i "s@^ \+@@g" /tmp/profile
6. 在vim中設(shè)置tab縮進(jìn)為4個(gè)字符
6.1. 在終端臨時(shí)生效
vim file.txt
:set tabstop=4
6.2. 配置永久生效
echo "set tabstop=4" >> ~/.vimrc