ICode9

精准搜索请尝试: 精确搜索
首页 > 系统相关> 文章详细

LINUX核心命令实战总结三——文件和目录操作命令二

2020-09-11 15:33:40  阅读:252  来源: 互联网

标签:实战 文件 操作命令 data web01 LINUX 20 txt root


1.1 ln:硬链接与软链接

【功能说明】

ln 命令可以理解为英文单词link的缩写,其功能是创建文件间的链接,链接包括硬链接和软链接。

【语法格式】

 

  ln         [OPTION]...     [source]       [target]
  ln         [选项]...         [源文件]       [目标文件或目录]

 

【选项说明】

命令ln的参数选项及说明

参数选项

解释说明(带*的为重点)

无参数

创建硬链接(*)

-s

创建软连接(符号链接)(*)

【硬链接使用范例】

硬链接是通过索引节点来进行链接。在Linux文件系统中,多个文件名指向同。一个索引节点是正常且允许的,这种情况下的文件就称为硬链接。

 

[root@web01 ~]# ln /etc/hosts hard_link
[root@web01 ~]# ls -i /etc/hosts hard_link
1310748 /etc/hosts  1310748 hard_link
[root@web01 ~]# rm -f /etc/hosts
[root@web01 ~]# cat /etc/hosts
cat: /etc/hosts: 没有那个文件或目录
[root@web01 ~]# ln hard_link /etc/hosts
[root@web01 ~]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
10.28.11.232     server
10.28.11.234     client
10.28.11.235     web01
10.28.11.236     web02
[root@web01 ~]# ls -i /etc/hosts hard_link
1310748 /etc/hosts  1310748 hard_link
View Code

 

【硬链接知识小总结】

1、具有相同的inode 节点号的多个文件互为硬链接

2、删除硬链接文件或者删除源文件任意之一,文件实体并未被删除。

3、只有删除了原文件以及源文件所有对应的硬链接文件,文件实体才会被删除。

4、当所有的硬链接文件及源文件被删除之后,再存放新的数据时会占用这个文件的空间,或者磁盘fsck检查的时候,删除的数据也会被系统回收。

5、硬链接文件就是文件的另一个入口

6、可以通过给文件设置硬链接文件,来防止重要文件被误删

7、执行命令“ln 源文件 硬链接文件”,即可完成硬链接的创建

8、硬链接文件可以用rm命令删除

9、对于静态文件(没有进程正在调用的文件)来讲,当对应硬链接数为0时,文件就会被删除,

【软连接语法格式】

 

 ln      -s    [OPTION]...     [source]       [target]
 ln      -s    [选项]...         [源文件]       [目标文件或目录]

 

执行ln命令创建软链接

 

[root@web01 ~]# ln -s /etc/hosts soft_link
[root@web01 ~]# ll -hi|grep /etc/hosts
1703982 lrwxrwxrwx  1 root root   10 1月  19 12:12 soft_link -> /etc/hosts

 

【软链接知识小结】

1、软链接类似于Windows的快捷方式

2、软链接类似于一个文本文件,里面存放的是源文件的路径,指向源文本实体

3、即使删除了源文件,软链接文件也还是依然存在,但是无法访问指向的源文件路径内容了。

4、失效的时候一般是白字红底闪烁提示

5、执行命令“ln  -s  源文件  软链接文件”, 即可完成创建软链接

6、软链接和源文件是不同类型的文件,也是不同的文件,inode号也不相同

7、删除软连接文件可以使用rm命令

 

1.2 readlink:查看符号链接文件的内容

 

【功能说明】

 

Readlink命令,它能够帮助我们查看符号链接文件的真是情况。

 

【语法格式】

 

readlink     [OPTION]...     [FILE]
readlink     [选项]...         [文件]

 

【选项说明】

Readlink命令的参数选项及说明

参数选项

解释说明(带*的为重点)

-f

一直跟随符号链接,直到非符号链接的文件位置,但要保证最后必须存在一个非符号链接文件

【使用范例】

 

[root@web01 ~]# ll -h /usr/bin/awk
lrwxrwxrwx. 1 root root 14 7月   5 2016 /usr/bin/awk -> ../../bin/gawk
[root@web01 ~]# readlink /usr/bin/awk
../../bin/gawk
[root@web01 ~]# readlink  -f /usr/bin/awk
/bin/gawk

 

1.3 find:查找目录下的文件

【功能说明】

Find 命令用于查找目录的文件,同时也可以调用其他命令执行相应的操作

【语法格式】

 

find     [-H]     [-L]     [-P]     [-D debugopts]     [-Olevel]     [path...]     [expression]
find         [选项]     [-D debugopts]     [-Olevel]     [路径]     [操作语]

 

【选项说明】

参数选项

解释说明(带*的为重点)

pathname

命令所查找的目录路径,例如“.”来表示当前目录,用“/”来表示系统根目录(*)

Options模块

-depth

从指定目录下最深的子目录开始查找

-maxdepth levels

查找的最大目录级数,levels为自然数(*)

-regextype type

改变正则表达式的模式,默认为emacs,还有posix-awk、posix-basic、posix-egrep、posix-extended。

Tests模块

-mtime[-n|n|+n]

按照文件的修改时间来查找文件(这个参数最常用),具体如下(*)

-n表示文件更改时间距现在n天以内

+n表示文件更改时间距现在n天以前

n是距现在第n天

-atime[-n|n|+n]

按照文件的访问时间来查找文件,单位为天

-ctime[-n|n|+n]

按照文件的状态改变时间来查找文件,单位为天

-amin

按照文件的访问时间来查找文件,单位为分钟

-cmin

按照文件的状态改变时间来查找文件,单位为分钟

-mmin

按照文件的修改时间来查找文件,单位为分钟

-group

按照文件所属的组来查找文件

-name

按照文件名查找文件,只支持*、?、[]等特殊通配符(*)

-newer

查找更改时间比指定文件新的文件

-nogroup

查找没有有效用户组的文件,即该文件所属的组在/etc/groups中不存在

-nouser

查找没有有效属主的文件,即该文件的属主在/etc/passwd中不存在

-path paternt

指定路径样式,配合-prune参数排除指定目录

-perm

按照文件权限来查找文件

-regex

接正则表达式

-iregex

接正则表达式,不区分大小写

-size n[]

查找文件长度为n块的文件

-user

按照文件属主查找文件

-type

查找某一类型的文件:(*)

b (块设备)

c (字符设备文件)

d (目录)

P  (管道文件)

l  (符号链接文件)

f  (普通文件)

s  (socket 文件)

D   (door)

Action模块

-delete

将查找出的文件删除

-exec

对匹配的文件执行该参数所给出的shelll命令(*)

-ok

和-exec作用相同,但在执行每个命令之前,都会让用户先确定是否执行

-prune

使用这一项可以使find命令不在当前指定的目录中查找

-print

将匹配的文件输出到标准输出(默认功能,使用中可省略)

OPERATORS

Find支持逻辑运算符

!

取反(*)

-a

取交集,全拼为and(*)

-o

取并集,全拼为or(*)

【使用范例】

 

查找指定时间内修改过的文件

 

[root@web01 ~]# find . -atime -2
.
./.bashrc
./dir2
./test1
./test1/d
./test1/e
./test1/b
./test1/a
./test1/g
View Code

 

用-name指定关键字查找

 

[root@web01 ~]# find /var/log -mtime +3 -name '*.log'
/var/log/anaconda.storage.log
/var/log/anaconda.log
/var/log/dracut.log
/var/log/anaconda.program.log
/var/log/anaconda.ifcfg.log
/var/log/anaconda.yum.log

 

利用“!”反向查找

 

[root@web01 test]# find . ! -type d
./file2.txt
./dir5/yhh04
./dir5/file6.txt
./dir5/yhh02
[root@web01 test]# find .  -type d 
.
./dir2
./dir5
./dir5/sub1
./dir5/sub1/test
View Code

 

按照目录或文件的权限来查找文件

 

[root@web01 test]# find /data/ -perm 755
/data/
/data/dir2
/data/dir3

 

按大小查找文件

 

[root@web01 test]# find . -size +1000c
.
./dir2
./dir5
./dir5/sub1
./dir5/sub1/test
./dir5/dir3
./dir4
./dir4/sub1
./dir4/sub1/test
View Code

 

查找文件时希望忽略某个目录

 

[root@web01 test]# find /data -path "/data/dir3" -prune -o -print 
/data
/data/dir2
/data/file2.txt
/data/file1.txt
View Code

 

忽略多个目录

 

[root@web01 test]# find /data \( -path /data/dir2 -o -path /data/dir3  \) -prune -o -print
/data
/data/file2.txt
/data/file1.txt
View Code

 

使用user和nouser选项

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

[root@web01 test]# chown nobody:nobody file2.txt
[root@web01 test]# ll -h file2.txt
-rw-r--r-- 1 nobody nobody 0 1月  18 15:48 file2.txt
[root@web01 test]# find . -user nobody
./file2.txt
[root@web01 test]# chown 555 file2.txt 
[root@web01 test]# ll -h file2.txt 
-rw-r--r-- 1 555 nobody 0 1月  18 15:48 file2.txt
[root@web01 test]# find . -nouser
./file2.txt
View Code

 

使用group和nogroup选项

 

[root@web01 test]# find . -group nobody
./file2.txt
[root@web01 test]# chown .555 file2.txt
[root@web01 test]# ll -h file2.txt
-rw-r--r-- 1 555 555 0 1月  18 15:48 file2.txt
[root@web01 test]# find . -nogroup
./file2.txt
View Code

 

查找比某个文件新或旧的文件

 

[root@web01 test]# find . -newer file2.txt   
.
./dir2
./dir5
./dir5/yhh04
./dir5/sub1
./dir5/sub1/test
./dir5/dir3
./dir5/yhh02
./dir5/yhh03
./dir5/yhh01
./dir5/yhh05
./dir5/yhh00
./.file4.txt
./dir4
./dir4/sub1
./dir4/sub1/test
[root@web01 test]# find . -newer file2.txt ! -newer file3.txt
View Code

 

逻辑操作符的使用

 

[root@web01 test]# find . -maxdepth 1 -type d  
.
./dir2
./dir5
./dir4
[root@web01 test]# find . -maxdepth 1 -type d ! -name "."
./dir2
./dir5
./dir4
[root@web01 test]# find . -maxdepth 1 -type d ! -name "." -o -name "dir2"
./dir2
View Code

 

Find正则表达式

 

[root@web01 ~]# find / -regex ".*find"
/usr/bin/oldfind
/usr/bin/find
/usr/bin/gst-typefind
/bin/find
[root@web01 ~]# find / -regex ".*/find"
/usr/bin/find
/bin/find
View Code

 

正则表达式的其他类型

 

root@web01 ~]# cd /data
[root@web01 data]# find . -regextype "posix-egrep" -name '*[0-9]'
./dir2
./dir3

 

命令ls  -l 放在find命令的 -exec选项中执行

 

[root@web01 data]# find . -type f -exec ls -l {} \;
-rw-r--r-- 1 root root 0 1月  19 09:20 ./file2.txt
-rw-r--r-- 1 root root 0 1月  19 09:20 ./file1.txt

 

在目录中查找到更改时间在n天以前的文件,并删除它们

 

[root@web01 data]# find . -type f -mtime +14 -exec rm {} \;

 

使用exec的安全模式-ok

 

[root@web01 data]# find /var/log -name "*.log" -mtime +5 -ok rm {} \;
< rm ... /var/log/anaconda.storage.log > ? n
< rm ... /var/log/anaconda.log > ? n
< rm ... /var/log/dracut.log > ? ^C

 

命令ls  -l 命令放在find命令的xargs 后

 

[root@web01 data]# find . -type f|xargs ls -l
-rw-r--r-- 1 root root 0 1月  19 09:20 ./file1.txt
-rw-r--r-- 1 root root 0 1月  19 09:20 ./file2.txt

 

使用xargs 执行mv 命令

 

[root@web01 data]# find . -name "*.txt"|xargs -i mv {} dir2/
[root@web01 data]# ls
dir2  dir3
[root@web01 data]# ls dir2/
file1.txt  file2.txt

 

 命令find 结合xargs的-p选项的使用示例

 

[root@web01 data]# find dir2/ -name "file*"|xargs -p rm -f
rm -f dir2/file2.txt dir2/file1.txt ?...y
View Code

 

【技巧性范例】

进入目录/root目录下data目录,删除file5.txt

 

[root@web01 data]# cd /data
[root@web01 data]# ls
dir2  dir3  file5.txt
[root@web01 data]# rm file5.txt 

 

 

[root@web01 ~]# find /data -type f -name "*file5.txt"|xargs rm -f

 

[root@web01 ~]# find /data -type f -name "*file5.txt" -exec rm {} \;

1.4  xargs:将标准输入转换成命令行参数

【功能说明】

命令xargs是向其他命令传递命令参数一个过滤器,能够将管道或者标准输入传递的数据传递换成xargs命令后跟随的命令的命令行参数。

【语法格式】

 

xargs         [option]
xargs         [选项]

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

【选项说明】

 

命令xargs 参数选项及说明

 

参数选项

解释说明(带*的为重点)

-n

指定每行的最大数量n,可以将标准输入的文本划分为多行,每行n个参数,默认空格分离(*)

-d

自定义分隔符

-I

指定一个符号替代前面结界,而不用-i参数默认的{}

-p

提示让用户确认是否执行后面的命令,y执行,n不执行

-0(数字0)

用null代替空格作为分隔符,配合find命令的 -print()选项的输出使用

 

 

【使用范例】

 

多行输入变单行的例子

 

[root@web01 ~]# xargs < test.txt
1 2 3 4 5 6 7 8 9 10 11
[root@web01 ~]# cat test.txt    
1 2 3 4 5 6
7 8 9
10 11
[root@web01 ~]# xargs < test.txt
1 2 3 4 5 6 7 8 9 10 11
View Code

 

通过-n指定每行的输出个数的例子

 

[root@web01 ~]# xargs  -n 3 < test.txt
1 2 3
4 5 6
7 8 9
10 11
View Code

 

自定义分隔符(使用-d功能)的例子

 

[root@web01 ~]# echo splitXsplitXsplitXsplitXsplitX
splitXsplitXsplitXsplitXsplitX
[root@web01 ~]# echo splitXsplitXsplitXsplitXsplitX|xargs -d X
split split split split split 
[root@web01 ~]# echo splitXsplitXsplitXsplitXsplitX|xargs -d X -n 2
split split
split split
split 
View Code

 

参数-I可以指定一个替换的字符串

 

[root@web01 data]# find . -name "*.log"|xargs -i mv {} dir1/
[root@web01 data]# ls
dir2  dir3  file5.txt
[root@web01 data]# find . -name "file*"|xargs -I [] cp [] dir2
cp: "./dir2/file5.txt" 与"dir2/file5.txt" 为同一文件
View Code

 

结合find使用xargs的特殊案例

 

[root@web01 tmp]# touch "hello word.txt"
[root@web01 tmp]# ls
hello word.txt
[root@web01 tmp]# touch hello\ everyone.txt
[root@web01 tmp]# ls
hello everyone.txt  hello word.txt
[root@web01 tmp]# find . -type f -name "*.txt"|xargs rm
rm: 无法删除"./hello": 没有那个文件或目录
rm: 无法删除"word.txt": 没有那个文件或目录
rm: 无法删除"./hello": 没有那个文件或目录
rm: 无法删除"everyone.txt": 没有那个文件或目录
[root@web01 tmp]# find . -type f -name "*.txt" -print |xargs -0  rm -f 
[root@web01 tmp]# ls
hello everyone.txt  hello word.txt
[root@web01 tmp]# find . -type f -name "*.txt" -print0 |xargs -0  rm -f 
[root@web01 tmp]# ls
View Code

 

1.5 rename:重命名文件

 

【功能说明】

 

命令rename通过字符串替换的方式批量修改文件名

 

【语法格式】

 

rename     from         to         file ...

 

【使用案例】

 

[root@web01 data]# touch yhh{00..05}.txt
[root@web01 data]# ls
dir2  dir3  file5.txt  yhh00.txt  yhh01.txt  yhh02.txt  yhh03.txt  yhh04.txt  yhh05.txt
[root@web01 data]# ls
dir2  dir3  file5.txt  yhh00.txt  yhh01.txt  yhh02.txt  yhh03.txt  yhh04.txt  yhh05.txt
[root@web01 data]# rename "hh0" " " *
[root@web01 data]# ls
dir2  dir3  file5.txt  y 0.txt  y 1.txt  y 2.txt  y 3.txt  y 4.txt  y 5.txt
View Code

 

批量修改扩展名案例

 

[root@web01 data]# ls
dir2  dir3  file5.txt  y 0.txt  y 1.txt  y 2.txt  y 3.txt  y 4.txt  y 5.txt
[root@web01 data]# touch yhh{06..10}.jpk
[root@web01 data]# ls
dir2  file5.txt  y 1.txt  y 3.txt  y 5.txt    yhh07.jpk  yhh09.jpk
dir3  y 0.txt    y 2.txt  y 4.txt  yhh06.jpk  yhh08.jpk  yhh10.jpk
[root@web01 data]# rename .jpk .txt *.jpk
[root@web01 data]# ls
dir2  file5.txt  y 1.txt  y 3.txt  y 5.txt    yhh07.txt  yhh09.txt
dir3  y 0.txt    y 2.txt  y 4.txt  yhh06.txt  yhh08.txt  yhh10.txt
View Code

 

1.6 basename:显示文件名或目录名

 

 

 

 

 

 

 

 

【功能说明】

 

命令basename 用于显示去除路径和文件后缀部分的文件名或目录名

 

【语法格式】

 

basename         [NAME]         [SUFFIX]
basename         [文件或目录]     [后缀]

 

【使用范例】

 

[root@web01 ~]# ls  /data/dir1/file1.txt     
/data/dir1/file1.txt
[root@web01 ~]# basename /data/dir1/file1.txt
file1.txt

 

1.7 dirname:显示文件或目录路径

【功能说明】

命令dirname 用于显示文件或目录

【语法格式】

 

dirname         [NAME]
dirname         [文件或目录]

 

【使用范例】

显示文件或目录路径

 

[root@web01 ~]# dirname /data/dir1/file1.txt 
/data/dir1
[root@web01 ~]# cd /data/dir1
[root@web01 dir1]# dirname file1.txt

 

1.8 chattr:改变文件的扩展属性

【功能说明】

命令chattr用于改变文件的扩展属性。与chmod这个命令相比,chmod只是改变文件的读、写、执行权限,更底层的属性由chattr来改变。

【语法格式】

 

 Chattr      [ option ]         [ mode ]         files...
 chattr       [ 选项 ]         [模式 ]          [文件或目录]...

 

【参数说明】

参数选项

解释说明(带*的为重点)

-R

递归更改目录属性

-V

显示命令执行过程

mode

+

增加参数

-

移除参数

=

更新为指定参数

A

告诉系统不要修改这个文件的最后访问时间

a

只能向文件中添加数据,而不能删除,多用于服务器日志文件安全(*)

i

设定文件不能被删除,改名,写入,或新增加内容。

 

 

 

 

 

 

 

【使用范例】

 

设置只能往文件里追加内容,但不能删除文件

 

[root@web01 dir1]# touch test
[root@web01 dir1]# ls
file1.txt  test
[root@web01 dir1]# lsattr test
-------------e- test
[root@web01 dir1]# chattr +a test
[root@web01 dir1]# lsattr test   
-----a-------e- test
[root@web01 dir1]# rm -f test
rm: 无法删除"test": 不允许的操作
[root@web01 dir1]# echo 111 >>test
[root@web01 dir1]# cat test
111
[root@web01 dir1]# echo 222 > test
-bash: test: 不允许的操作
View Code

 

【安全优化实例】

历史记录.bash_history采用只增加不减少

 

[root@web01 ~]# chattr +a .bash_history
[root@web01 ~]# lsattr .bash_history
-----a-------e- .bash_history

 

锁定一些文件不能被修改

 

[root@web01 ~]# chattr +i /etc/inittab
[root@web01 ~]# lsattr /etc/inittab
----i--------e- /etc/inittab

 

1.9 lsattr:查看文件扩展属性

【功能说明】

命令lsattr用于查看文件扩展属性

【语法格式】

 

lsattr         [ options]         [ files...  ]
lsattr          [选项]         [ 文件或目录 ]

 

【选项说明】

参数选项

解释说明(带*的为重点)

-R

递归查看目录的扩展性

-a

显示所有文件包括隐藏文件的扩展属性

-d

显示目录的扩展属性

【使用范例】

查看文件的扩展属性

 

[root@web01 ~]# lsattr aa 
-------------e- aa

 

查看目录扩展属性

 

[root@web01 ~]# ll -d dir2
d-wx-wx-wx 2 root root 4096 1月  18 12:18 dir2
[root@web01 ~]# lsattr -d dir2
-------------e- dir2

 

1.10 file:显示文件的类型

【功能说明】

命令file用于显示文件的类型

【语法格式】

 

 file         [options]        [file]
 file         [选项]        [文件或目录]

 

【选项说明】

命令file的参数选项及说明

参数选项

解释说明(带*的为重点)

-b

输出信息使用精简格式,不输出文件名

【使用范例】

 

[root@web01 ~]# file aa
aa: empty
[root@web01 ~]# echo 111 >> aa
[root@web01 ~]# file aa       
aa: ASCII text

 

1.11 md5sum:计算和校验文件的md5的值

【功能说明】

它是一种不可逆的件算法

【语法格式】

 

md5sum         [OPTION]...             [FILE]...
md5sum         [选项]...             [文件或目录]...

 

【选项说明】

参数选项

解释说明(带*的为重点)

-b

二进制模式读取文件

-c

从指定文件读取md5校验值,并进行校验

-t

文本模式读取文件,这是默认模式

--quiet

校验文件使用的参数,验证通过不输出OK

--status

校验文件使用的参数,不输出任何信息,可以通过命令的返回值来判断

【使用范例】

生成一个文件的md5值

 

[root@web01 ~]# md5sum aa
1181c1834012245d785120e3505ed169  aa

 

校验是否发生改变

 

[root@web01 ~]# md5sum aa
1181c1834012245d785120e3505ed169  aa
[root@web01 ~]# md5sum aa > md5.log
[root@web01 ~]# cat md5.log
1181c1834012245d785120e3505ed169  aa
[root@web01 ~]# md5sum -c md5.log
aa: 确定
[root@web01 ~]# echo 222 >> aa
[root@web01 ~]# md5sum -c md5.log
aa: 失败
md5sum: 警告:1/1 生成的校验和不匹配

 

1.12 chown:改变文件或目录的用户和用户组

【功能说明】

命令chown 用于改变文件或目录的用户和用户组

【语法格式】

 

chown         [OPTION]...         [OWNER][:[GROUP]]         FILE...
chown         [选项]...              [用户][:[用户组]]        《文件或目录》...

 

常用格式:

 

chown        用户            文件或目录        #仅仅授权用户
chown        :组          文件或目录        #仅仅授权组
chown        用户:组        文件或目录        #表示授权用户和组

 

【使用说明】

命令chown 的参数选项及说明

参数选项

解释说明(带*的为重点)

-R

递归更改目录的用户和用户组(*)

【使用范例】

更改文件所属的用户属性

 

[root@web01 data]# touch file1.txt
[root@web01 data]# ll file1.txt
-rw-r--r-- 1 root root 0 1月  20 19:41 file1.txt
[root@web01 data]# chown 777 file1.txt
[root@web01 data]# ll file1.txt
-rw-r--r-- 1 777 root 0 1月  20 19:41 file1.txt

 

更改文件所属组(用户组)属性

 

[root@web01 data]# chown .yhh file1.txt
[root@web01 data]# ll file1.txt
-rw-r--r-- 1 777 yhh 0 1月  20 19:41 file1.txt

 

同时更改文件所属的用户和组属性

 

[root@web01 data]# chown root:root file1.txt
[root@web01 data]# ll file1.txt
-rw-r--r-- 1 root root 0 1月  20 19:41 file1.txt

 

递归更改目录及目录下所有子目录及文件的用户和用户组属性

 

[root@web01 data]# useradd yhh -g yhh    
[root@web01 data]# chown -R  yhh.yhh dir1/
chown: 正在更改"dir1/test" 的所有者: 不允许的操作
[root@web01 data]# ll  dir1  
总用量 4
-rw-r--r-- 1 yhh  yhh  0 1月  20 13:16 file1.txt
-rw-r--r-- 1 root root 4 1月  20 13:39 test

 

1.13 chmod:改变文件或目录权限

【功能说明】

命令chmod用来改变目录或文件权限命令的。

【语法格式】

 

chmod         [OPTION]...             [MODE]...             FILE...
chmod         [选项]...             [模式]...             《文件或目录》...

 

【选项说明】

参数选项

解释说明(带*的为重点)

-R

递归更改目录的用户和用户组(*)

【使用范例】

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

[root@web01 data]# ll file1.txt
---------- 1 root root 0 1月  20 19:41 file1.txt
[root@web01 data]# chmod u+x file1.txt
[root@web01 data]# ll file1.txt
---x------ 1 root root 0 1月  20 19:41 file1.txt
[root@web01 data]# chmod g+w file1.txt
[root@web01 data]# ll file1.txt
---x-w---- 1 root root 0 1月  20 19:41 file1.txt
[root@web01 data]# chmod o+r file1.txt
[root@web01 data]# ll file1.txt
---x-w-r-- 1 root root 0 1月  20 19:41 file1.txt
[root@web01 data]# chmod u=rwx,g=rw,o=r file1.txt
[root@web01 data]# ll file1.txt
-rwxrw-r-- 1 root root 0 1月  20 19:41 file1.txt
View Code

 

文件的数字权限授权案例

 

[root@web01 data]# chmod 000 file1.txt
[root@web01 data]# ll file1.txt
---------- 1 root root 0 1月  20 19:41 file1.txt
[root@web01 data]# chmod 753 file1.txt
[root@web01 data]# ll file1.txt
-rwxr-x-wx 1 root root 0 1月  20 19:41 file1.txt
View Code

 

使用-R 参数递归授权权限案例

 

 

 

[root@web01 data]# ll dir2/
总用量 0
-rw-r--r-- 1 root root 0 1月  19 17:15 file5.txt
-rw-r--r-- 1 root root 0 1月  20 20:33 yhh00
-rw-r--r-- 1 root root 0 1月  20 20:33 yhh01
-rw-r--r-- 1 root root 0 1月  20 20:33 yhh02
-rw-r--r-- 1 root root 0 1月  20 20:33 yhh03
-rw-r--r-- 1 root root 0 1月  20 20:33 yhh04
-rw-r--r-- 1 root root 0 1月  20 20:33 yhh05
[root@web01 data]# chmod -R  777 dir2/
[root@web01 data]# ll dir2/
总用量 0
-rwxrwxrwx 1 root root 0 1月  19 17:15 file5.txt
-rwxrwxrwx 1 root root 0 1月  20 20:33 yhh00
-rwxrwxrwx 1 root root 0 1月  20 20:33 yhh01
-rwxrwxrwx 1 root root 0 1月  20 20:33 yhh02
-rwxrwxrwx 1 root root 0 1月  20 20:33 yhh03
-rwxrwxrwx 1 root root 0 1月  20 20:33 yhh04
-rwxrwxrwx 1 root root 0 1月  20 20:33 yhh05
View Code

 

1.14 chgrp:更改文件用户组

【功能说明】

命令chgrp只用于更改文件的用户,功能被chown取代了,了解一下即可

【语法格式】

 

 chgrp         [OPTION]...         GROUP         FILE...
 chgrp         [选项]...             用户组        <文件或目录>...

 

【选项说明】

参数选项

解释说明(带*的为重点)

-R

递归更改目录的用户和用户组(*)

【使用范例】

 

[root@web01 data]# ll file5.txt
-rw-r--r-- 1 root root 0 1月  19 16:32 file5.txt
[root@web01 data]# chgrp yhh file5.txt
[root@web01 data]# ll file5.txt
-rw-r--r-- 1 root yhh 0 1月  19 16:32 file5.txt
View Code

 

递归修改文件

 

[root@web01 data]# ll dir2
总用量 0
-rwxrwxrwx 1 root root 0 1月  19 17:15 file5.txt
-rwxrwxrwx 1 root root 0 1月  20 20:33 yhh00
-rwxrwxrwx 1 root root 0 1月  20 20:33 yhh01
-rwxrwxrwx 1 root root 0 1月  20 20:33 yhh02
-rwxrwxrwx 1 root root 0 1月  20 20:33 yhh03
-rwxrwxrwx 1 root root 0 1月  20 20:33 yhh04
-rwxrwxrwx 1 root root 0 1月  20 20:33 yhh05
[root@web01 data]# chgrp -R yhh dir2/
[root@web01 data]# ll dir2
总用量 0
-rwxrwxrwx 1 root yhh 0 1月  19 17:15 file5.txt
-rwxrwxrwx 1 root yhh 0 1月  20 20:33 yhh00
-rwxrwxrwx 1 root yhh 0 1月  20 20:33 yhh01
-rwxrwxrwx 1 root yhh 0 1月  20 20:33 yhh02
-rwxrwxrwx 1 root yhh 0 1月  20 20:33 yhh03
-rwxrwxrwx 1 root yhh 0 1月  20 20:33 yhh04
-rwxrwxrwx 1 root yhh 0 1月  20 20:33 yhh05
View Code

 

1.15 umask:显示或设置权限密码

【功能说明】

命令umask 通过八进制的数值来定义用户创建或目录的默认权限

【语法格式】

 

umask         [option]   [mode]
umask         [选项]    [模式]

 

参数选项

解释说明(带*的为重点)

-P

输出的权限掩码可直接作为命令执行

-S

以字符方式输出默认掩码

【权限计算】

假设umask的值为:022

 

6    6    6            #文件的起始权限
0    2    2 -            #umask的值
-------------------
6    4    4

 

假设umask的值为:045(其他用户组位奇数)

 

6    6    6            #文件的起始权限
0    4    5 -            #umask的值
-------------------
6    2    1
0    0     1 +
-----------------------
6    2    2             #真是文件权限

 

目录权限计算(没有奇偶之分)

 

7    7    7            #文件的起始权限
0    2    2 -            #umask的值
-------------------
7    5    5

 

【使用范例】

系统umask默认值

 

[root@web01 ~]# umask
0022
[root@web01 ~]# su yhh
[yhh@web01 root]$ umask
0002

 

参数-p与-S的作用

 

[root@web01 ~]# umask -p
umask 0022
[root@web01 ~]# umask -S
u=rwx,g=rx,o=rx

 

验证修改umaks值对文件权限的影响

 

[root@web01 ~]# umask
0022
[root@web01 ~]# touch file1;ll -h file1
-rw-r--r-- 1 root root 0 1月  20 21:22 file1
[root@web01 ~]# umask 044
[root@web01 ~]# touch file2;ll -h file2
-rw--w--w- 1 root root 0 1月  20 21:22 file2
[root@web01 ~]# umask 034
[root@web01 ~]# touch file3;ll -h file3
-rw-r---w- 1 root root 0 1月  20 21:23 file3
View Code

 

验证修改umask永久生效

 

[root@web01 ~]# umask
0022
drwxr-xr-x 3 root root 4096 1月  18 12:16 yan
[root@web01 ~]# mkdir yanhuihuang;ll -d yanhuihuang
drwxr-xr-x 2 root root 4096 1月  20 21:25 yanhuihuang
[root@web01 ~]# umask 044
[root@web01 ~]# mkdir yanhuihuang1;ll -d yanhuihuang1
drwx-wx-wx 2 root root 4096 1月  20 21:25 yanhuihuang1
[root@web01 ~]# umask 055
[root@web01 ~]# mkdir yanhuihuang2;ll -d yanhuihuang2 
drwx-w--w- 2 root root 4096 1月  20 21:25 yanhuihuang2
View Code

 

修改配置文件使得umask永久生效

[root@web01 ~]# sed -n '61,69p' /etc/bashrc
    # By default, we want umask to get set. This sets it for non-login shell.
    # Current threshold for system reserved uid/gids is 200
    # You could check uidgid reservation validity in
    # /usr/share/doc/setup-*/uidgid file
    if [ $UID -gt 199 ] && [ "`id -gn`" = "`id -un`" ]; then
       umask 002
    else
       umask 022
Fi
[root@web01 ~]# sed -n '57,64p' /etc/profile
# By default, we want umask to get set. This sets it for login shell
# Current threshold for system reserved uid/gids is 200
# You could check uidgid reservation validity in
# /usr/share/doc/setup-*/uidgid file
if [ $UID -gt 199 ] && [ "`id -gn`" = "`id -un`" ]; then
    umask 002
else
    umask 022
View Code

1.16 从畏手畏脚到力挽狂澜的心法

 

 

 

 

 

 

 

 

 

 

 

 

 

执着:屡败屡战,不死不休。

 

专注:只有它,无她

 

自信:我就是未来的linux大牛

 

心态:空杯心态,基础不牢,地动山摇

 

标签:实战,文件,操作命令,data,web01,LINUX,20,txt,root
来源: https://www.cnblogs.com/huihuangyan/p/13651974.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有