1. 程式人生 > >我來談談Linux文件目錄與操作-給新手看

我來談談Linux文件目錄與操作-給新手看

from 時間 二進制文件 循環 方式 text1 小寫 terminal 版本

第三章 Linux文件和目錄操作
第一節 Linux文件和目錄簡介
第二節 文件和目錄基本操作
第三節 Linux查看文件內容命令
第四節 Linux修改文件日期
第五節 Linux文件和目錄權限管理
第六節 Linux隱藏屬性和特殊權限
第七節 Linux文件查找

linux 文件類型
普通文件 目錄文件 設備文件(/dev) 鏈接文件 管道文件 查看文件類型(- 普通文件 . 隱藏文件 d 目錄文件 c 設備文件 l 鏈接文件)
[root@localhost ~]# nano hello.txt #快捷創建記事本
[root@localhost ~]# cat hello.txt #查看記事本

hello #顯示的內容
welcome
hi
[root@localhost ~]# ll
總用量 12
-rw-------. 1 root root 1862 5月 4 05:42 anaconda-ks.cfg
-rw-r--r--. 1 root root 17 5月 4 19:54 hello.txt
....

查看文件信息 ll + 文件名
[root@localhost dev]# ll /dev/null
crw-rw-rw-. 1 root root 1, 3 5月 4 18:12 /dev/null
[root@localhost dev]# ll /dev/cdrom

lrwxrwxrwx. 1 root root 3 5月 4 18:12 /dev/cdrom -> sr0

查看文件類型 file + 文件名
[root@localhost ~]# file hello.txt
hello.txt: ASCII text
[root@localhost ~]# which ls
alias ls=‘ls --color=auto‘
/usr/bin/ls
[root@localhost ~]# file /usr/bin/ls
/usr/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

[root@localhost ~]# file /dev/cdrom
/dev/cdrom: symbolic link to `sr0‘

linux文件結構
無論是程序 文檔 數據庫 或者 目錄 他們的結構都是相同的 存儲在分區的兩部分中
索引節點 :文件的基本信息
數據節點 :文件的內容

文件名 最大 255個字符 區分大小寫

目錄結構 Filesyste FHS標準

根目錄下子目錄的職能
bin 符號鏈接 相當於快捷方式 主要用來存放用戶調用的一些命令
boot 主要存放linux內核的啟動命令
dev 主要存放設備文件
etc 主要存放所有的配置文件
home 一般用戶的家目錄
lib linux系統所調用的函數庫
media 媒體信息
mnt 目錄掛載的文件夾 比如訪問光驅 /mnt/cdrom
opt 第三方的軟件
proc和system 內存相關的東西
root root賬號的家目錄
[root@localhost /]# cd ~
[root@localhost ~]# pwd
/root
run 運行程序的合法目錄
srv 服務啟動訪問的數據
tmp 臨時目錄
usr 第三方的安裝程序
var 可變內容的數據 比如網站的日誌

目錄與路徑

[root@localhost ~]# cd /etc/sysconfig/network-scripts/ #絕對路徑
[root@localhost network-scripts]# cd ../../../root #相對路徑
[root@localhost ~]# pwd
/root
[root@localhost ~]#

. 當前目錄 .. 上級目錄 -前一個工作目錄 ~當前用戶的家目錄 /根目錄
cd 切換目錄 pwd 顯示當前目錄 mkdir 新建目錄 rmdir 刪除一個空目錄

mkdir rmdir 的使用

[root@localhost ~]# mkdir abc #創建目錄
[root@localhost ~]# ls
abc hello.txt 公共 視頻 文檔 音樂
anaconda-ks.cfg initial-setup-ks.cfg 模板 圖片 下載 桌面
[root@localhost ~]# mkdir -p test1/test2/test3 #加上 -p 參數可以連續創建多個目錄
[root@localhost ~]# ll
總用量 12
drwxr-xr-x. 2 root root 6 5月 4 20:35 abc
-rw-------. 1 root root 1862 5月 4 05:42 anaconda-ks.cfg
-rw-r--r--. 1 root root 17 5月 4 19:54 hello.txt
-rw-r--r--. 1 root root 1910 5月 4 05:44 initial-setup-ks.cfg
drwxr-xr-x. 3 root root 19 5月 4 20:36 test1
drwxr-xr-x. 2 root root 6 5月 4 17:24 公共
drwxr-xr-x. 2 root root 6 5月 4 17:24 模板
drwxr-xr-x. 2 root root 6 5月 4 17:24 視頻
drwxr-xr-x. 2 root root 6 5月 4 17:24 圖片
drwxr-xr-x. 2 root root 6 5月 4 17:24 文檔
drwxr-xr-x. 2 root root 6 5月 4 17:24 下載
drwxr-xr-x. 2 root root 6 5月 4 17:24 音樂
drwxr-xr-x. 2 root root 6 5月 4 17:24 桌面
[root@localhost ~]# cd test1
[root@localhost test1]# cd test2/
[root@localhost test2]# cd test3/
[root@localhost test3]# pwd
/root/test1/test2/test3
[root@localhost test3]# rmdir abc
rmdir: 刪除 "abc" 失敗: 沒有那個文件或目錄
[root@localhost test3]# d
[root@localhost ~]# rmdir abc # 刪除目錄
[root@localhost ~]# ll
總用量 12
-rw-------. 1 root root 1862 5月 4 05:42 anaconda-ks.cfg
-rw-r--r--. 1 root root 17 5月 4 19:54 hello.txt
-rw-r--r--. 1 root root 1910 5月 4 05:44 initial-setup-ks.cfg
drwxr-xr-x. 3 root root 19 5月 4 20:36 test1
drwxr-xr-x. 2 root root 6 5月 4 17:24 公共
drwxr-xr-x. 2 root root 6 5月 4 17:24 模板
drwxr-xr-x. 2 root root 6 5月 4 17:24 視頻
drwxr-xr-x. 2 root root 6 5月 4 17:24 圖片
drwxr-xr-x. 2 root root 6 5月 4 17:24 文檔
drwxr-xr-x. 2 root root 6 5月 4 17:24 下載
drwxr-xr-x. 2 root root 6 5月 4 17:24 音樂
drwxr-xr-x. 2 root root 6 5月 4 17:24 桌面
[root@localhost ~]# rmdir -p test1/test2/test3 #加上 - p 參數連續刪除目錄
[root@localhost ~]# ll
總用量 12
-rw-------. 1 root root 1862 5月 4 05:42 anaconda-ks.cfg
-rw-r--r--. 1 root root 17 5月 4 19:54 hello.txt
-rw-r--r--. 1 root root 1910 5月 4 05:44 initial-setup-ks.cfg
drwxr-xr-x. 2 root root 6 5月 4 17:24 公共
drwxr-xr-x. 2 root root 6 5月 4 17:24 模板
drwxr-xr-x. 2 root root 6 5月 4 17:24 視頻
drwxr-xr-x. 2 root root 6 5月 4 17:24 圖片
drwxr-xr-x. 2 root root 6 5月 4 17:24 文檔
drwxr-xr-x. 2 root root 6 5月 4 17:24 下載
drwxr-xr-x. 2 root root 6 5月 4 17:24 音樂
drwxr-xr-x. 2 root root 6 5月 4 17:24 桌面

文件與目錄管理 (也可以使用 命令 + --help 查看更詳細的用途)
ls 查看文件與目錄 ls -l 單行顯示 相當於 ll ls -a 查看所有文件 包括隱藏文件 ls -l -i相當於 ls -li 查看 inode 節點號

d(文件類型)rwx(擁有者權限)r-x(所屬用戶組權限)r-x(其他用戶權限). 2 (鏈接數) root(所有者) root(所屬用戶組) 6(文件大小) 5月 4 17:24(最後修改時間) 公共(文件名)
r 表示允許讀權限
w 表示允許寫權限
x 表示允許執行權限

cp 拷貝文件與目錄

[root@localhost ~]# cp hello.txt ./abc
[root@localhost ~]# cp hello.txt /root/abc #功能同上 復制hello.test文件到當前目錄下的目錄中
cp:是否覆蓋"/root/abc/hello.txt"? y
[root@localhost ~]# mkdir -p test1/test2/test3 # -p 連續創建目錄
[root@localhost ~]# cp ./hello.txt ./test1/test2/test3/ #復制文件 到 次目錄下
[root@localhost ~]# cp -r ./test1 ./abc #由於test1中包含多個子目錄和子文件 所以使用 -r 參數循環復制到 此目錄下
[root@localhost ~]# cd abc #查看
[root@localhost abc]# ls
hello.txt test1
[root@localhost abc]# cd test1
[root@localhost test1]# cd test2
[root@localhost test2]# cd test3
[root@localhost test3]# pwd
/root/abc/test1/test2/test3
[root@localhost test3]#

rm 刪除文件與目錄
[root@localhost ~]# rm -f hello #強制刪掉 不詢問
[root@localhost ~]# rm -i hello.txt #刪除並且詢問
rm:是否刪除普通文件 "hello.txt"?n

[root@localhost ~]# ll
總用量 12
drwxr-xr-x. 3 root root 36 5月 4 21:07 abc
-rw-------. 1 root root 1862 5月 4 05:42 anaconda-ks.cfg
-rw-r--r--. 1 root root 17 5月 4 19:54 hello.txt
-rw-r--r--. 1 root root 1910 5月 4 05:44 initial-setup-ks.cfg
drwxr-xr-x. 3 root root 19 5月 4 21:06 test1

[root@localhost ~]# rm -rf test1 # -rf 循環刪除目錄 並且不詢問 比較常用
[root@localhost ~]# ll
總用量 12
drwxr-xr-x. 3 root root 36 5月 4 21:07 abc
-rw-------. 1 root root 1862 5月 4 05:42 anaconda-ks.cfg
-rw-r--r--. 1 root root 17 5月 4 19:54 hello.txt
-rw-r--r--. 1 root root 1910 5月 4 05:44 initial-setup-ks.cfg
drwxr-xr-x. 2 root root 6 5月 4 17:24 公共

mv 移動文件與目錄(可以用來給文件 目錄進行重命名 可以移動文件)

[root@localhost ~]# ll
總用量 12
drwxr-xr-x. 3 root root 36 5月 4 21:07 abc
-rw-------. 1 root root 1862 5月 4 05:42 anaconda-ks.cfg
-rw-r--r--. 1 root root 17 5月 4 19:54 hello.txt
-rw-r--r--. 1 root root 1910 5月 4 05:44 initial-setup-ks.cfg
drwxr-xr-x. 3 root root 19 5月 4 21:19 test1
drwxr-xr-x. 2 root root 6 5月 4 17:24 公共
drwxr-xr-x. 2 root root 6 5月 4 17:24 模板
drwxr-xr-x. 2 root root 6 5月 4 17:24 視頻
drwxr-xr-x. 2 root root 6 5月 4 17:24 圖片
drwxr-xr-x. 2 root root 6 5月 4 17:24 文檔
drwxr-xr-x. 2 root root 6 5月 4 17:24 下載
drwxr-xr-x. 2 root root 6 5月 4 17:24 音樂
drwxr-xr-x. 2 root root 6 5月 4 17:24 桌面
[root@localhost ~]# mv test1 test2
[root@localhost ~]# ll
總用量 12
drwxr-xr-x. 3 root root 36 5月 4 21:07 abc
-rw-------. 1 root root 1862 5月 4 05:42 anaconda-ks.cfg
-rw-r--r--. 1 root root 17 5月 4 19:54 hello.txt
-rw-r--r--. 1 root root 1910 5月 4 05:44 initial-setup-ks.cfg
drwxr-xr-x. 3 root root 19 5月 4 21:19 test2
drwxr-xr-x. 2 root root 6 5月 4 17:24 公共
drwxr-xr-x. 2 root root 6 5月 4 17:24 模板
drwxr-xr-x. 2 root root 6 5月 4 17:24 視頻
drwxr-xr-x. 2 root root 6 5月 4 17:24 圖片
drwxr-xr-x. 2 root root 6 5月 4 17:24 文檔
drwxr-xr-x. 2 root root 6 5月 4 17:24 下載
drwxr-xr-x. 2 root root 6 5月 4 17:24 音樂
drwxr-xr-x. 2 root root 6 5月 4 17:24 桌面
[root@localhost ~]# mv test2 ./abc/test1
mv:是否覆蓋"./abc/test1/test2"? ^C
[root@localhost ~]# mv test2 ./abc/test5
[root@localhost ~]# cd abc
[root@localhost abc]# ll
總用量 4
-rw-r--r--. 1 root root 17 5月 4 21:04 hello.txt
drwxr-xr-x. 3 root root 19 5月 4 21:07 test1
drwxr-xr-x. 3 root root 19 5月 4 21:19 test5

basename 取得路徑文件名
dirname 取得路徑目錄名
[root@localhost ~]# basename ./abc/hello.txt #取得路徑文件名
hello.txt
[root@localhost ~]# dirname ./abc/text1 #取得路徑目錄名
./abc

如何查看文件內容

cat 命令
[root@localhost ~]# cat hello.txt # 查看 文件內容
hello
welcome
hi

[root@localhost ~]# cat -n ./hello1.txt #加上 參數 -n 表示對所有行進行編號
1 hello world
2
3 welcome
4
5 hi
[root@localhost ~]# cat -b ./hello.txt #加上 參數 -b 表示對空行進行編號
1 hello
2 welcome
3 hi
[root@localhost ~]# cat -n ./hello1.txt
1 hello world
2
3 welcome
4
5 hi
tac查看
[root@localhost ~]# tac hello.txt #倒序輸出文件內容 用的較少 參數有 -r -b -s 如需了解 --help
hi
welcome
hello

more #主要用文件內容多的文件和目錄

[root@localhost ~]# more /etc/man_db.conf

You must provide all system manpaths, including manpaths for alternate

operating systems, locale specific manpaths, and combinations of both, if

they exist, otherwise the permissions of the user running man/mandb will

be used to manipulate the manual pages. Also, mandb will not initialise

the database cache for any manpaths not mentioned below unless explicitly

requested to do so.

...
...
...

space :向下翻一頁
Ente:向下翻一行/字符串 :在這個顯示行的內容中,向下查詢“字符串”
:f :立刻顯示出文件名以及目前顯示的行數
q:退出
less
與more相對應 (區別不大)
空格/PageDown :向下翻一
PageUp :向上翻一頁/字符串 :向下查找
?字符串 :向上查找q :退出
head (默認顯示前10行)

[root@localhost ~]# head /etc/man_db.conf #查看文件的前十行
#
#

This file is used by the man-db package to configure the man and cat paths.

It is also used to provide a manpath for those without one by examining

their PATH environment variable. For details see the manpath(5) man page.

#

Lines beginning with `#‘ are comments and are ignored. Any combination of

tabs or spaces may be used as `whitespace‘ separators.

#

There are three mappings allowed in this file:

[root@localhost ~]# head -n 5 /etc/man_db.conf # -n 參數 用來設置想要查看的行數 查看文件的前5行
#
#

This file is used by the man-db package to configure the man and cat paths.

It is also used to provide a manpath for those without one by examining

their PATH environment variable. For details see the manpath(5) man page.

tail (默認顯示後10行)

[root@localhost etc]# tail ./man_db.conf

formatted for a terminal of the given width, regardless of the width of

the terminal actually being used. This should generally be within the

range set by MINCATWIDTH and MAXCATWIDTH.

#
#CATWIDTH 0
#
#---------------------------------------------------------

Flags.

NOCACHE keeps man from creating cat pages.

#NOCACHE

[root@localhost etc]# tail -n 5 ./man_db.conf #-n 後接要顯行的行數 就可以顯示幾行 #
#---------------------------------------------------------

Flags.

NOCACHE keeps man from creating cat pages.

#NOCACHE

touch修改文件時間或者創建新文件

、、、[root@localhost ~]# echo "hello">hi.txt;ll hi.txt # echo 可以用來創建文件 格式是 “內容”>文件名稱
[root@localhost ~]# ll hi.txt;cat hi.txt
-rw-r--r--. 1 root root 6 5月 7 00:31 hi.txt
hello

[root@localhost ~]# touch ./test1.txt #創建新的文件 只是普通文件格式 創建目錄 需要用mkdir命令
[root@localhost ~]# ll
總用量 16
drwxr-xr-x. 3 root root 36 5月 4 21:27 abc
-rw-------. 1 root root 1862 5月 4 05:42 anaconda-ks.cfg
-rw-r--r--. 1 root root 25 5月 4 21:47 hello1.txt
-rw-r--r--. 1 root root 17 5月 4 19:54 hello.txt
-rw-r--r--. 1 root root 1910 5月 4 05:44 initial-setup-ks.cfg
drwxr-xr-x. 3 root root 19 5月 4 21:19 test1
-rw-r--r--. 1 root root 0 5月 6 01:19 test1.txt
drwxr-xr-x. 2 root root 6 5月 4 17:24 公共
drwxr-xr-x. 2 root root 6 5月 4 17:24 模板
drwxr-xr-x. 2 root root 6 5月 4 17:24 視頻
drwxr-xr-x. 2 root root 6 5月 4 17:24 圖片
drwxr-xr-x. 2 root root 6 5月 4 17:24 文檔
drwxr-xr-x. 2 root root 6 5月 4 17:24 下載
drwxr-xr-x. 2 root root 6 5月 4 17:24 音樂
drwxr-xr-x. 2 root root 6 5月 4 17:24 桌面

-mtime (內容數據修改時會改此時間)
-ctime (狀態改變會改此時間)
-atime (文件內容, 此時間就會改)

[root@localhost ~]# date
2018年 05月 06日 星期日 01:25:39 CST
[root@localhost ~]# ll test1.txt; ll --time=atime test1.txt; ll --time=ctime test1.txt
-rw-r--r--. 1 root root 15 5月 6 01:21 test1.txt
-rw-r--r--. 1 root root 15 5月 6 01:21 test1.txt
-rw-r--r--. 1 root root 15 5月 6 01:21 test1.txt
[root@localhost ~]# nano test.txt
[root@localhost ~]# nano test1.txt
[root@localhost ~]# ll test1.txt; ll --time=atime test1.txt; ll --time=ctime test1.txt
-rw-r--r--. 1 root root 29 5月 6 01:31 test1.txt
-rw-r--r--. 1 root root 29 5月 6 01:30 test1.txt
-rw-r--r--. 1 root root 29 5月 6 01:31 test1.txt
[root@localhost ~]# chmod g+w tesr1.txt
chmod: 無法訪問"tesr1.txt": 沒有那個文件或目錄
[root@localhost ~]# chmod g+w test1.txt
[root@localhost ~]# ll test1.txt; ll --time=atime test1.txt; ll --time=ctime test1.txt
-rw-rw-r--. 1 root root 29 5月 6 01:31 test1.txt
-rw-rw-r--. 1 root root 29 5月 6 01:30 test1.txt
-rw-rw-r--. 1 root root 29 5月 6 01:31 test1.txt

文件目錄權限管理

[root@localhost ~]# ll test1.txt
-rw-rw-r--. 1 root root 29 5月 6 01:31 test1.txt

所屬用戶 所屬組 其他用戶


rwx rwx rwx
111 111 111
7 7 7
rw- rw- r--
110 110 100
6 6 4

chmod 用法 更改文件目錄權限

方式一 二進制表示模式
[root@localhost ~]# chmod 755 test1.txt # 修改文件權限 chmod命令 加上權限二進制表示 加文件名
[root@localhost ~]# ll test1.txt
-rwxr-xr-x. 1 root root 29 5月 6 01:31 test1.txt
方式二 加減賦值操作
[root@localhost ~]# ll test1.txt;chmod u-w test1.txt;ll test1.txt #其中u代表user 就是所屬用戶
-rw-r--r--. 1 root root 29 5月 6 01:31 test1.txt
-r--r--r--. 1 root root 29 5月 6 01:31 test1.txt
[root@localhost ~]# ll test1.txt;chmod go+rw test1.txt;ll test1.txt #其中g代表group(所屬用戶組)o代表(其他用戶)
-r--r--r--. 1 root root 29 5月 6 01:31 test1.txt
-r--rw-rw-. 1 root root 29 5月 6 01:31 test1.txt
[root@localhost ~]# ll test1.txt;chmod ugo-r test1.txt;ll test1.txt #chmod ugo 相當於 chmod a 表示全部
-r--rw-rw-. 1 root root 29 5月 6 01:31 test1.txt
-----w--w-. 1 root root 29 5月 6 01:31 test1.txt
方式三 也可以用 “=”來進行權限修改 ,可以用逗號隔開
[root@localhost ~]# ll test1.txt;chmod u=rw,g=rw,o=r test1.txt;ll test1.txt
-rwxrwxrwx. 1 root root 29 5月 6 01:31 test1.txt
-rw-rw-r--. 1 root root 29 5月 6 01:31 test1.txt

chgrp 和chown 更改所屬用戶和用戶組

[root@localhost ~]# ll test1.txt;chgrp zhaoxin test1.txt;ll test1.txt #更改用戶的所屬用戶組
-rw-rw-r--. 1 root root 29 5月 6 01:31 test1.txt
-rw-rw-r--. 1 root zhaoxin 29 5月 6 01:31 test1.txt

[root@localhost ~]# ll test1.txt; chown zhaoxin:root test1.txt;ll test1.txt #更改文件的所屬用戶 和 所屬用戶組
-rw-rw-r--. 1 root zhaoxin 29 5月 6 01:31 test1.txt
-rw-rw-r--. 1 zhaoxin root 29 5月 6 01:31 test1.txt

針對目錄進行的操作 chown -R 和chmod -R

[root@localhost ~]# ll test1;chown -R zhaoxin:root test1;ll test1 #將test1目錄包括其所有子目錄都改為 以zhaoxin為用戶 root為所屬群組的命令 —R 參數實現
總用量 0
drwxr-xr-x. 3 root root 18 5月 4 21:19 test2
總用量 0
drwxr-xr-x. 3 zhaoxin root 18 5月 4 21:19 test2

[root@localhost ~]# ll test1;chmod -R 774 test1;ll test1 #將test1目錄包括其所有子目錄權限都改為 774(二進制方式) 的命令 —R 參數實現
總用量 0
drwxr-xr-x. 3 zhaoxin root 18 5月 4 21:19 test2
總用量 0
drwxrwxr--. 3 zhaoxin root 18 5月 4 21:19 test2

文件默認權限 umask
[root@localhost ~]# mkdir bbb;echo "hello">hi.txt;ll;ll hi.txt #創建一個名字為bbb的目錄 和 名字為 hi.txt的文件 並查看其權限

總用量 24
drwxr-xr-x. 3 root root 36 5月 4 21:27 abc
-rw-------. 1 root root 1862 5月 4 05:42 anaconda-ks.cfg
drwxr-xr-x. 2 root root 6 5月 7 02:23 bbb
...
-rw-r--r--. 1 root root 6 5月 7 02:23 hi.txt
[root@localhost ~]# umask #查看默認權限umask的值
0022

我創建的文件默認權限: 644 -rw-r--r--
目錄 755 drwxr-xr-x

目錄和文件的默認權限:
目錄 : 777 rwx rwx rwx 文件: 666 rw- rw- rw-
755 rwx r-x r-x 644 rw- r-- r--

[root@localhost ~]# umask 0003 #更改umask的默認權限值為0003
[root@localhost ~]# umask
0003
[root@localhost ~]# mkdir ccc
[root@localhost ~]# ll
總用量 24
drwxr-xr-x. 3 root root 36 5月 4 21:27 abc
-rw-------. 1 root root 1862 5月 4 05:42 anaconda-ks.cfg
drwxr-xr-x. 2 root root 6 5月 7 02:23 bbb
drwxrwxr--. 2 root root 6 5月 7 02:35 ccc

[root@localhost ~]# touch ccc.txt;ll ccc.txt
-rw-rw-r--. 1 root root 0 5月 7 02:37 ccc.txt

目錄和文件的默認權限:
目錄 : 777 rwx rwx rwx 文件: 666 rw- rw- rw-
umask : 003
ccc rwx rwx r-- ccc.txt rw- rw- r--
提示 不能簡單的用666-003=663 這是錯的 應該去對應其文件權限 比如003 就是用戶與用戶組減去0 所以權限不變 然而其他用戶則少3 3代表 -wx 權限 所以去掉這兩個權限也就是上面 rw- 權限變成 r-- 權限的原因

文件隱藏的屬性 chattr lsattr (attr 屬性的意思)

[root@localhost ~]# touch test1.txt
[root@localhost ~]# ll
總用量 24
...
-rw-rw-r--. 1 zhaoxin zhaoxin 29 5月 7 02:45 test1.txt
...
[root@localhost ~]# lsattr test1.txt #查看 test1.txt的隱藏屬性
---------------- test1.txt
[root@localhost ~]# chattr +i test1.txt #使用chattr命令 加上參數i 為文件添加不得任意更動文件或目錄的隱藏屬性
[root@localhost ~]# rm test1.txt #嘗試刪除文件 結果因為其隱藏屬性 不能刪除
rm:是否刪除普通文件 "test1.txt"?y
rm: 無法刪除"test1.txt": 不允許的操作
[root@localhost ~]# lsattr test1.txt #再次查看文件的隱藏屬性 發現i權限
----i----------- test1.txt
[root@localhost ~]# chattr -i test1.txt #去除i權限 可以使用chattr 減去i權限即可
[root@localhost ~]# rm test1.txt #嘗試刪除文件 結果成功
rm:是否刪除普通文件 "test1.txt"?y
[root@localhost ~]# ll test1.txt
ls: 無法訪問test1.txt: 沒有那個文件或目錄

chattr的一些參數

chattr [+/-/=<屬性>][文件或目錄...]b:不更新文件或目錄的最後存取時間。
c:將文件或目錄壓縮後存放。
d:將文件或目錄排除在dump操作之外。 i:不得任意更動文件或目錄。 ( 是一個標註 表示經常使用)
s:保密性刪除文件或目錄。
S:即時更新文件或目錄。
u:預防以外刪除。 a:只能加數據,不能修改或刪除
lsattr的一些參數

-a  顯示所有文件和目錄,包括以"."為名稱開頭字符的額外內建,現行目錄"."與上層目錄".."。
-d  顯示,目錄名稱,而非其內容。
-l  此參數目前沒有任何作用。
-R  遞歸處理,將指定目錄下的所有文件及子目錄一並處理。
-v  顯示文件或目錄版本。
-V  顯示版本信息。
文件特殊權限 : SUID ,SGID,SBIT (4--- 2--- 1---)

SUID 4 s

ls -l /usr/bin/passwd僅對二進制程序有效執行者對該程序要有x權限
在該程序執行過程中有效 執行者擁有該程序所有者的權限
[root@localhost ~]# ls -l /usr/bin/passwd
-rwsr-xr-x. 1 root root 27832 6月 10 2014 /usr/bin/passwd
#查看該文件 其所有者擁有s權限 也就是說 當第三方用戶也就是其他用戶訪問該文件的時候可以使用所有者權限 如zhaoxin用戶訪問該文件時本來只擁有x權限也就執行權限 但是由於其擁有 s 權限 所以zhaoxin用戶就擁有了其 所有者 也就是root的權限 rws權限

SGID 2 s
ls -l /usr/bin/locate可針對文件和目錄
SGID對二進制程序有用對執行者要求具備x權限 執行者擁有該程序用戶組的支持
[root@localhost ~]# ls -l /usr/bin/locate
-rwx--s--x. 1 root slocate 40512 11月 5 2016 /usr/bin/locate
[root@localhost ~]# ls -l /var/lib/mlocate/mlocate.db
-rw-r-----. 1 root slocate 3658465 5月 6 10:08 /var/lib/mlocate/mlocate.db
#查看該文件 其用戶組擁有s權限 也就是說 當第三方用戶也就是其他用戶訪問該文件的時候可以使用用戶組權限 如zhaoxin用戶訪問該文件時本來只擁有x權限也就執行權限 但是由於其擁有 s 權限 所以zhaoxin用戶就擁有了其用戶組的支持 由於mlocate.db屬於 slocate用戶組 所以zhaoxin用戶 就擁有使用mlocate.db的權限

SBIT(Sticky bit) 1 t
ls -ld /tmp
*只對目錄有效*當目錄有wx權限時,用戶在該目下建的文件只有自己和root有權操作
[root@localhost ~]# useradd guest #創建guest用戶
[root@localhost ~]# passwd guest #設置密碼
更改用戶 guest 的密碼 。
新的 密碼:
重新輸入新的 密碼:
passwd:所有的身份驗證令×××已經成功更新。
[root@localhost ~]# ll /home #查看home目錄下的guest用戶的信息
總用量 4
drwx------. 5 guest guest 107 5月 7 03:40 guest
drwx------. 15 zhaoxin zhaoxin 4096 5月 7 02:13 zhaoxin

[root@localhost ~]# su zhaoxin #切換到 zhaoxin用戶
密碼:
[zhaoxin@localhost root]$ cd /tmp
[zhaoxin@localhost tmp]$ echo "hello">hi.txt #創建一個名字為hi.txt的文件
[zhaoxin@localhost tmp]$ ll hi.txt
-rw-rw-r--. 1 zhaoxin zhaoxin 6 5月 7 03:42 hi.txt
[zhaoxin@localhost tmp]$ su guest #切換到guest用戶
密碼:
[guest@localhost tmp]$ ll hi.txt
-rw-rw-r--. 1 zhaoxin zhaoxin 6 5月 7 03:42 hi.txt
[guest@localhost tmp]$ whoami
guest
[guest@localhost tmp]$ rm hi.txt #試圖刪除文件 不成功
rm:是否刪除有寫保護的普通文件 "hi.txt"?y
rm: 無法刪除"hi.txt": 不允許的操作
[guest@localhost tmp]$ ll -d /tmp
drwxrwxrwt. 22 root root 4096 5月 7 03:42 /tmp

原因就是 tmp文件擁有 t 特殊權限 t權限只對目錄有效 也就是用戶擁有t權限的目錄下創建的文件只有用戶本身和root用戶才有權操作 其余用戶則不可以

如何增加 SUID SGID SBIT 權限

[root@localhost tmp]# cd
[root@localhost ~]# touch vel #創建文件
[root@localhost ~]# ll vel
-rw-r--r--. 1 root root 0 5月 7 03:55 vel
[root@localhost ~]# umask #umask擁有四個值 第一個值用來設置特殊權限
0022
[root@localhost ~]# chmod 4755 vel;ll vel #通過二進制增加權限的方法來 增加 SUID s 權限 SUID s 權限代表的是4
-rwsr-xr-x. 1 root root 0 5月 7 03:55 vel
[root@localhost ~]# chmod 2755 vel;ll vel #通過二進制增加權限的方法來 增加 SGID s 權限 SGID s 權限代表的是2
-rwxr-sr-x. 1 root root 0 5月 7 03:55 vel
[root@localhost ~]# chmod 6755 vel;ll vel #通過二進制增加權限的方法來 增加 SUID s 權限 和 SGID s 權限 權限代表的是4+2 =6
-rwsr-sr-x. 1 root root 0 5月 7 03:55 vel
[root@localhost ~]# chmod 1755 vel;ll vel #通過二進制增加權限的方法來 增加 SBIT T 權限 SBIT T 權限代表的是1
-rwxr-xr-t. 1 root root 0 5月 7 03:55 vel

去除特殊權限的操作

[root@localhost ~]# chmod o=r vel;ll vel #通過也可以用 “=”來進行權限修改
-rwxr-xr--. 1 root root 0 5月 7 03:55 vel

文件查找
which whereis locate find

[root@localhost ~]# which ls
alias ls=‘ls --color=auto‘
/usr/bin/ls
[root@localhost ~]# whereis ls
ls: /usr/bin/ls /usr/share/man/man1/ls.1.gz /usr/share/man/man1p/ls.1p.gz

which 根據PATH找執行文件whereis 從PATH,lib,man手中找二進制、源文件 、man頁
locate the binary, source, and manual page files for a command
-b : 只找二進制文件
-m: 只找在說明文件manual路徑下的文件
-s : 只找source源文件
-u : 沒有說明文檔的文件

locate (/var/lib/mlocate) #主要是在數據庫中尋找 系統每天都有固定時間更新數據庫 也可以使用 updatedb (時間比較漫長)手動更新數據庫

 find   全盤尋找

find (查找檔案,功能強大,就是耗時間和硬盤):
find [PATH] [option] [action] 檔案或目錄名稱
例如 find /root -name bill.txt [root@localhost ~]# find /root bill.txt

我來談談Linux文件目錄與操作-給新手看