码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2017-10-24 01:40:00      阅读:195      评论:0      收藏:0      [点我收藏+]

标签: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

标签:linux   基础   

原文地址:http://vitus.blog.51cto.com/6450023/1975331

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!