1. 程式人生 > >命令more、less、tail、chmod、chown、umask、lsattr、chattr

命令more、less、tail、chmod、chown、umask、lsattr、chattr

linux 基礎

more 分屏順序顯示文檔,按空格翻頁

less ctrl+b向上翻頁;ctrl+f向下翻頁或者空格翻頁;q鍵退出

/搜索文本按n鍵搜索下一次出現,shift+n向上搜索

less more 類似,但使用 less 可以隨意瀏覽文件,而more 僅能向前移動,卻不能向後移動,而且 less 在查看之前不會加載整個文件。

-b <緩沖區大小> 設置緩沖區的大小

-e 當文件顯示結束後,自動離開

-f 強迫打開特殊文件,例如外圍設備代號、目錄和二進制文件

-g 只標誌最後搜索的關鍵詞

-i 忽略搜索時的大小寫

-m 顯示類似more命令的百分比

-N 顯示每行的行號

-o <文件名> less 輸出的內容在指定文件中保存起來

-Q 不使用警告音

-s 顯示連續空行為一行

-S 行過長時間將超出部分舍棄

-x <數字> 將“tab”鍵顯示為規定的數字空格

/字符串:向下搜索“字符串”的功能

?字符串:向上搜索“字符串”的功能

n:重復前一個搜索(與 / ? 有關)

N:反向重復前一個搜索(與 / ? 有關)

b 向後翻一頁

d 向後翻半頁

h 顯示幫助界面

Q 退出less 命令

u 向前滾動半頁

y 向前滾動一行

空格鍵滾動一行

回車鍵滾動一頁

[pagedown] 向下翻動一頁

[pageup] 向上翻動一頁

less附加備註

1.全屏導航

ctrl + F - 向前移動一屏

ctrl + B - 向後移動一屏

ctrl + D - 向前移動半屏

ctrl + U - 向後移動半屏

2.單行導航

j - 向前移動一行

k - 向後移動一行

3.其它導航

G - 移動到最後一行

g - 移動到第一行

q / ZZ - 退出 less 命令

4.其它有用的命令

v - 使用配置的編輯器編輯當前文件

h - 顯示 less 的幫助文檔

&pattern - 僅顯示匹配模式的行,而不是整個文件

5.標記導航

當使用 less 查看大文件時,可以在任何一個位置作標記,可以通過命令導航到標有特定標記的文本位置:

ma - 使用 a 標記文本的當前位置

‘a - 導航到標記

a

head 查看文件的頭10行,可指定行數

head -n 5 /etc/passwd

tail [-f ] [ -c Number | -n Number | -m Number | -b Number | -k Number ] [ File ]

參數解釋:

-f 該參數用於監視File文件增長。

-c Number Number 字節位置讀取指定文件

-n Number Number 行位置讀取指定文件。

-m Number Number 多字節字符位置讀取指定文件,比方你的文件假設包括中文字,假設指定-c參數,可能導致截斷,但使用-m則會避免該問題。

-b Number Number 表示的512字節塊位置讀取指定文件。

-k Number Number 表示的1KB塊位置讀取指定文件。

File 指定操作的目標文件名稱

上述命令中,都涉及到number,假設不指定,默認顯示10行。Number前面可使用正負號,表示該偏移從頂部還是從尾部開始計算。

tail可運行文件一般在/usr/bin/以下。

查看文件的尾10行,可指定行數;-f 可用來查看動態文件,特別是日誌文件

tail -f /var/log/messages

結束用Ctrl+Z

演示例子

1tail -f filename

說明:監視filename文件的尾部內容(默認10行,相當於增加參數 -n 10),刷新顯示在屏幕上。退出,按下CTRL+C

2tail -n 20 filename

說明:顯示filename最後20行。

3tail -n +20 filename

說明:顯示filename前面20行。

4tail -r -n 10filename

說明:逆序顯示filename最後10行。

補充:

tail功能相似的命令還有:

cat 從第一行開始顯示檔案內容。

tac 從最後一行開始顯示檔案內容。

more 分頁顯示檔案內容。

less more 相似,但支持向前翻頁

head 僅僅顯示前面幾行

tail 僅僅顯示後面幾行

n 帶行號顯示檔案內容

od 以二進制方式顯示檔案內容

文件或目錄權限chmod

r read r=4

w write w=2

x excute執行 x=1

- rwx rwx rwx . 1 root root

類型 所有者 所屬組 其他用戶 所屬主 所屬組

.表示受制於selinux

chmod = change mode

使用方式 : chmod [-cfvR] [--help] [--version] mode file...

說明 : Linux/Unix 的檔案調用權限分為三級 : 檔案擁有者、群組、其他。利用 chmod 可以藉以控制檔案如何被他人所調用。

參數 :

mode : 權限設定字串,格式如下 : [ugoa...][[+-=][rwxX]...][,...],其中

u 表示該檔案的擁有者,g 表示與該檔案的擁有者屬於同一個群體(group)者,o 表示其他以外的人,a 表示這三者皆是。

+ 表示增加權限、- 表示取消權限、= 表示唯一設定權限。

r 表示可讀取,w 表示可寫入,x 表示可執行,X 表示只有當該檔案是個子目錄或者該檔案已經被設定過為可執行。

-c : 若該檔案權限確實已經更改,才顯示其更改動作

-f : 若該檔案權限無法被更改也不要顯示錯誤訊息

-v : 顯示權限變更的詳細資料

-R : 對目前目錄下的所有檔案與子目錄進行相同的權限變更(即以繼承的方式逐個變更)

--help : 顯示輔助說明

--version : 顯示版本

[root@24centos7-01 tmp]# ls -ld test

drwxr-xr-x 2 root root 59 10 20 21:56test

[root@24centos7-01 tmp]# chmod 700 test

[root@24centos7-01 tmp]# ls -ld test

drwx------ 2 root root 59 10 20 21:56test

[root@24centos7-01 tmp]# chmodu=rwx,g=rx,o=rx test

[root@24centos7-01 tmp]# ls -ld test

drwxr-xr-x 2 root vitus 59 10 20 21:56test

[root@24centos7-01 tmp]# chmod a+w test

[root@24centos7-01 tmp]# ls -ld test

drwxrwxrwx 2 root vitus 59 10 20 21:56test

[root@24centos7-01 tmp]# chmod a-w test

[root@24centos7-01 tmp]# ls -ld test

dr-xr-xr-x 2 root vitus 59 10 20 21:56test

-R

[root@24centos7-01 tmp]# chmod -R 700 test

[root@24centos7-01 tmp]# ls -ld test

drwx------ 2 root root 59 10 20 21:56test

[root@24centos7-01 tmp]# ls -l test

總用量 8

-rwx------ 1 root root 6 10 20 21:551.txt

-rwx------ 1 root root 6 10 20 21:551.txt~

-rwx------ 1 root root 0 10 20 21:412.txt

-rwx------ 1 root root 0 10 20 21:413.txt

chown 更改所有者和所屬組

chown owner:group filename/directory

chown -R owner:group directory

chown owner filename/directory

chown :group filename/directory

chown [選項]... [所有者][:[]] 文件...

    通過chown改變文件的擁有者和群組。在更改文件的所有者或所屬群組時,可以使用用戶名稱和用戶識別碼設置。普通用戶不能將自己的文件改變成其他的擁有者。其操作權限一般為管理員。

-c 顯示更改的部分的信息

-f 忽略錯誤信息

-h 修復符號鏈接

-R 處理指定目錄以及其子目錄下的所有文件

-v 顯示詳細的處理信息

-deference 作用於符號鏈接的指向,而不是鏈接文件本身

[root@24centos7-01 tmp]# chown vitus/tmp/test

[root@24centos7-01 tmp]# ls -l

dr-xr-xr-x 2 vitus vitus 59 10 20 21:56 test

[root@24centos7-01 tmp]# chown root:roottest

[root@24centos7-01 tmp]# ls -ld test

dr-xr-xr-x 2 root root 59 10 20 21:56test

[root@24centos7-01 tmp]# chown vitus:vitustest

[root@24centos7-01 tmp]# ls -ld test

dr-xr-xr-x 2 vitus vitus 59 10 20 21:56test

[root@24centos7-01 tmp]# chown :root test

[root@24centos7-01 tmp]# ls -ld test

dr-xr-xr-x 2 vitus root 59 10 20 21:56test

[root@24centos7-01 tmp]# chown root: test

[root@24centos7-01 tmp]# ls -ld test

dr-xr-xr-x 2 root root 59 10 20 21:56test

umask 它的值共有4位,分別代表:gid/uid,屬主,組權,其它用戶權限;一般用的是後3 位。

作用:設置新建文件和目錄的默認權限

[root@24centos7-01 tmp]# umask 0002

[root@24centos7-01 tmp]# touch 2.txt

[root@24centos7-01 tmp]# ls -l 2.txt

-rw-rw-r-- 1 root root 0 10 22 18:462.txt

[root@24centos7-01 tmp]# mkdir 2

[root@24centos7-01 tmp]# ls -ld 2

drwxrwxr-x 2 root root 6 10 22 18:48 2

隱藏權限lsattr_chattr

lsattr 查看文件或目錄的特殊屬性

chattr 更改文件或目錄的特殊屬性

i權限 加上後文件只讀

a權限 只能追加文件內容和更改文件時間戳 ,不能重命名,不能更改和刪除

chattr +i /tmp/test.txt#添加“i”隱藏屬性後,就無法更改這個文件了

chattr -i /tmp/test.txt #解除"i"這個隱藏屬性

lsattr命令的用法:lsattr [-aR]

參數說明:

-a :將隱藏文件的屬性也列出來;

-R :連同子目錄的數據也一並列出來!

I屬性

[root@24centos7-01 tmp]# chattr +i test1

[root@24centos7-01 tmp]#

[root@24centos7-01 tmp]# lsattr -a test1

----i----------- test1/.

---------------- test1/..

[root@24centos7-01 tmp]# cd test1

[root@24centos7-01 test1]# ls

[root@24centos7-01 test1]# touch 1.txt

touch: 無法創建"1.txt":權限不夠

[root@24centos7-01 test1]# mkdir 1

mkdir: 無法創建目錄"1": 權限不夠

A屬性

[root@24centos7-01 tmp]# chattr -i test1

[root@24centos7-01 tmp]# lsattr -a test1

---------------- test1/.

---------------- test1/..

[root@24centos7-01 tmp]# touch./test1/test.txt

[root@24centos7-01 tmp]# ls -l ./test1/

總用量 0

-rw-r--r-- 1 root root 0 10 22 20:43test.txt

[root@24centos7-01 test]# touch test.txt

[root@24centos7-01 test]# ls

1.txt 1.txt~ 2.txt 3.txt 4.txt 5.txt test.txt

[root@24centos7-01 test]# ll

總用量 8

-rwx------ 1 root root 6 10 20 21:551.txt

-rwx------ 1 root root 6 10 20 21:551.txt~

-rwx------ 1 root root 0 10 20 21:412.txt

-rwx------ 1 root root 0 10 20 21:413.txt

drwxr-xr-x 2 root root 6 10 22 21:014.txt

-rw-r--r-- 1 root root 0 10 22 21:045.txt

-rw-r--r-- 1 root root 0 10 22 21:04test.txt

[root@24centos7-01 test]# chattr +atest.txt

[root@24centos7-01 test]# lsattr -atest.txt

-----a---------- test.txt

[root@24centos7-01 test]# echo"linux" >> test.txt

[root@24centos7-01 test]# cat test.txt

linux

不可修改

[root@24centos7-01 test]# echo ""> test.txt

-bash: test.txt: 不允許的操作

可追加

[root@24centos7-01 test]# echo "">> test.txt

不可重命名文件

[root@24centos7-01 test]# mv test.txttest1.txt

mv: 無法將"test.txt"移動至"test1.txt": 不允許的操作

不可刪除

[root@24centos7-01 test]# rm -f test.txt

rm: 無法刪除"test.txt":不允許的操作

可更改時間戳

[root@24centos7-01 test]# touch test.txt

[root@24centos7-01 test]# ls -l test.txt

-rw-r--r-- 1 root root 7 10 22 21:05test.txt


命令more、less、tail、chmod、chown、umask、lsattr、chattr