• 第7章:文件处理


    7章:文件处理

    7.1、文件权限

    Linux系统中每一个文件都与多种权限类型相关联,其中文件所有权有三类(用户、用户组和其它用户),文件权限有读、写、执行。用户是文件所有者,用户组是多个用户的集合,其它用户是除用户或用户组之外的任何用户。

    7.1.1、文件详细权限

    范例:ls –l可以查看文件详细权限

    [root@cloucentos6 home]# ls -l test.sh

    -rwxr-xr-x. 1 root root  98  4月 25 22:36 test.sh

    第一个字段“-rwxr-xr-x.”代表文件权限,该字段首字符 – 代表指定该文件的类型,本范例中显示的test.sh只是一个普通文件,其它首字符代表:

    d 目录

    l 符号链接

    c 字符设备

    b 块设备

    p 管道

    s 套接字

    该字段其余部分(rwx r-x r-x)由三个三元组字符组成,第一个三元字符代表文件所有者权限,第二个代表用户组权限,第三个代表其它用户权限。r表示允许读(查看文件中的数据),w表示允许写(可以修改或删除文件),x表示允许执行(运行文件中的程序)。

    7.1.2chmod命令设置文件权限

    范例1:给test.sh文件的用户、用户组和其它用户一起添加权限,u=用户  g=用户组  o=其它用户。

    [root@cloucentos6 home]# ls -l test.sh

    -rwxr-xr-x. 1 root root 98  4月 25 22:36 test.sh

    [root@cloucentos6 home]# chmod u=rwx,g=r,o=r test.sh

    [root@cloucentos6 home]# ls -l test.sh

    -rwxr--r--. 1 root root 98  4月 25 22:36 test.sh

    范例2通过+和-给用户、用户组和其它用户分配权限

    [root@cloucentos6 home]# ls -l test.sh

    -rwxr--r--. 1 root root 98  4月 25 22:36 test.sh

    [root@cloucentos6 home]# chmod g+w test.sh      #给用户组添加写权限

    [root@cloucentos6 home]# ls -l test.sh

    -rwxrw-r--. 1 root root 98  4月 25 22:36 test.sh

    [root@cloucentos6 home]# chmod u-x test.sh       #删除用户执行权限

    [root@cloucentos6 home]# ls -l test.sh

    -rw-rw-r--. 1 root root 98  4月 25 22:36 test.sh

    [root@cloucentos6 home]# chmod +x test.sh      #统一给用户、用户组、其它用户执行权限

    [root@cloucentos6 home]# ls -l test.sh

    -rwxrwxr-x. 1 root root 98  4月 25 22:36 test.sh

    范例3通过八进制数设置权限,r=4  w =2  x=1

    [root@cloucentos6 home]# ls -l test.sh

    -r--------. 1 root root 98  4月 25 22:36 test.sh

    [root@cloucentos6 home]# chmod 755 test.sh

    [root@cloucentos6 home]# ls -l test.sh

    -rwxr-xr-x. 1 root root 98  4月 25 22:36 test.sh

    7.1.3chown命令更改所有权

    范例1chown命令格式chown user.group test.sh

    [root@cloucentos6 home]# ls -l test.sh

    -rwxr-xr-x. 1 root root 98  4月 25 22:36 test.sh

    [root@cloucentos6 home]# chown test test.sh    #修改test.sh文件用户为test用户

    [root@cloucentos6 home]# ls -l test.sh

    -rwxr-xr-x. 1 test root 98  4月 25 22:36 test.sh

    [root@cloucentos6 home]# chown testl.test test.sh  #修改test.sh文件用户和用户组为test用户

    [root@cloucentos6 home]# ls -l test.sh

    -rwxr-xr-x. 1 all all 98  4月 25 22:36 test.sh

    范例2以递归的方式设置file文件夹及子文件的所有者

    [root@cloucentos6 home]# chown -R test.test  file/

    7.1.4chattr命令创建不可删除和修改的文件

    范例1设置test.sh文件不能被删除和修改(包括超级管理员)

    [root@cloucentos6 home]# ls -l test.sh

    -rwxrwxrwx. 1 root root 12  4月 26 07:19 test.sh

    [root@cloucentos6 home]# chattr +i test.sh      #通过 +i 限制test.sh被修改和删除权限

    [root@cloucentos6 home]# ls -l test.sh

    -rwxrwxrwx. 1 root root 12  4月 26 07:19 test.sh

    [root@cloucentos6 home]# rm -rf test.sh

    rm: 无法删除"test.sh": 不允许的操作

    [root@cloucentos6 home]# chattr -i test.sh      #通过 -i 取消test.sh限制被修改和删除权限

    [root@cloucentos6 home]# rm -rf test.sh

    范例2查看是文件是否被chattr命令限制删除和修改文件

    [root@cloucentos6 home]# lsattr test.sh

    -------------e- test.sh

    [root@cloucentos6 home]# chattr +i test.sh 

    [root@cloucentos6 home]# lsattr test.sh 

    ----i--------e- test.sh

    7.2touch命令批量生成空白文件

    范例:使用for语句批量生成空白.txt文件

    [root@cloucentos6 home]# ls -l

    总用量 20

    drwx------. 2 root root 16384 12月 21 17:14 lost+found

    -rwxr-xr-x. 1 root root    58  4月 26 17:45 test.sh

    [root@cloucentos6 home]# cat test.sh

    #!/bin/bash

    for name in {1..3}.txt

    do

    touch  $name

    done

    [root@cloucentos6 home]# ./test.sh

    [root@cloucentos6 home]# ls -l

    总用量 20

    -rw-r--r--. 1 root root     0  4月 26 17:46 1.txt

    -rw-r--r--. 1 root root     0  4月 26 17:46 2.txt

    -rw-r--r--. 1 root root     0  4月 26 17:46 3.txt

    drwx------. 2 root root 16384 12月 21 17:14 lost+found

    -rwxr-xr-x. 1 root root    58  4月 26 17:45 test.sh

    7.3ln命令创建符号链接

    Linux系统的符号链接类似windows系统的快捷方式。

    范例:创建test.sh脚本符号链接到tmp目录下执行

    [root@cloucentos6 home]# ls -l test.sh

    -rwxr-xr-x. 1 root root 32  4月 26 18:15 test.sh

    [root@cloucentos6 home]# ln -s /home/test.sh /tmp/testln.sh

    [root@cloucentos6 home]# ls -l /tmp/testln.sh

    lrwxrwxrwx. 1 root root 13  4月 26 18:18 /tmp/testln.sh -> /home/test.sh

    [root@cloucentos6 home]# cat test.sh

    #!/bin/bash

    echo "hello world!"

    [root@cloucentos6 home]# /tmp/testln.sh

    hello world!

    7.4find文件查找

    find基于文件名查找

    find /home –name “*.txt” –print   #查找home目录下文件以.txt结尾的文件,不忽略大小写

    find /home –iname “*.txt” –print  #查找home目录下文件以.txt结尾的文件,忽略大小写

    find /home !  -name  "*.sh"  –print   #查找/home下不以.sh结尾的文件名

    find基于文件类型查找

    标注:b块设备、d目录、f普通文件、l符号链接、c字符设备、s套拼字

    find /home –type d  #查找home目录下的文件夹

    find基于正则表达式匹配路径查找

    find /home -regex ".*(.txt|.sh)$"  #查找home目录下.txt和.sh结尾文件,不忽略大小写find /home -iregex ".*(.txt|.sh)$"  #查找home目录下.txt和.sh结尾文件,忽略大小写

    find /home ( -name "*.txt" )  -o  ( -name "*.sh" )  #查找home目录下.txt和.sh结尾文件,不忽略大小写

    find基于排除指定文件名查找

    find /home !  -name  "*.sh"  –print   #查找/home下不以.sh结尾的文件名

    find基于目录深度查找

    find /home -mindepth 2   #查找当前目录最小深度的文件

    find /home -maxdepth 2   #查找当前目录最大深度的文件

    find基于文件时间属性查找

    标注:atime找出文件被访问时间(单位天)、ctime找出文件更改时间(单位天)、mtime找出修改数据时间(单位天)、admin找出文件被访问时间(单位分钟)、time找出文件更改时间(单位分钟)、mtime找出修改数据时间(单位分钟)

    find /home –type f –atime  -2   #查找home目录下最近2天内被访问过的所有文件,其计量单位天

    find /home –type f –atime  +2   #查找home目录下超过2天被访问过的所有文件,其计量单位天

    find /home –type f –atime  2   #查找home目录下恰好在2天前内被访问过的所有文件,其计量单位天

    find基于文件大小查找

    标注:b块(512字节)、c字节、k千字节、M兆字节、G吉字节

    find /home –type f –size  9M   #查找home目录下大小等于9M的普通文件

    find /home –type f –size  +9M   #查找home目录下大于9M的普通文件

    find /home –type f –size  -9M   #查找home目录下小于9M的普通文件

    find删除匹配的文件

    find /home -type f -name "*.txt" –delete  #查找home目录下以.txt结尾的普通文件进行删除

    find基于文件权限、所有者、组进行查找

    find /home -type f -perm 644   #查找home目录下权限644的普通文件

    find /home –type f –user test   #查找home目录下文件所有者test的普通文件

    find /home –type f ! –user root   #查找home目录下文件非所有者root的普通文件

    find /home –type f –group test   #查找home目录下文件组名test的普通文件

    find /home –type f ! –group root   #查找home目录下文件非组名root的普通文件

    find /home -maxdepth 1 -type f -user test  #查找home目录深度1且所有者test的普通文件

    find借助选项-exec与其它命令进行结合

    标注:{}  ; 是特殊字符串需要与-exec结合使用,{ }会被替换成相应的文件名。

    find /home -type f -user test -exec ls -l {} ;  #查找home目录下文件所有者test的普通文件并罗列详细信息

    find /home -type f -user abc -exec ls -l {} ; > a1.txt   # #查找home目录下文件所有者test的普通文件并罗列详细信息并输出重定向到a1.txt文本

    find跳过特定目录

    find /home ( -name "home2" -prune ) -o ( -type f -print )  #排除显示home目录下的二级目录homes目录中的所有文件的名称(路径)

    7.5locate快速寻找文件

             locate寻找文件的速度比find快得多,但find查得比较精准。locate是透过update程序将硬盘中的所有档案和目录资料先建立一个索引数据库,在 执行loacte时直接找该索引,查询速度会较快,索引数据库一般是由操作系统管理,但也可以直接下达update强迫系统立即修改索引数据库。

    范例:使用time统计find和locate第一次查找test.sh文件所花费的时间

    [root@cloucentos6 ~]# time find / -iname test.sh

    /usr/share/sgml/docbook/xsl-ns-stylesheets-1.75.2/test.sh

    /home/test.sh

    real  0m0.445s

    user 0m0.102s

    sys   0m0.202s

    [root@cloucentos6 ~]# time locate test.sh

    /home/test.sh

    /usr/share/sgml/docbook/xsl-ns-stylesheets-1.75.2/test.sh

    real  0m0.121s

    user 0m0.066s

    sys   0m0.011s

    7.6xargs命令

      xargs是一个很有用的命令,能将标准输入数据转换成命令行参数,也可以将单行或多行文本输入转换成其它格式,例如单行转多行或多行转单行。

    xargs多行文本转换成单行文本

    范例:

    [root@cloucentos6 home]# cat file.txt

    1 2 3 4 5

    6 7 8

    9 10

    [root@cloucentos6 home]# cat file.txt | xargs

    1 2 3 4 5 6 7 8 9 10

    xargs单行输入转换成多行输出

    标注:-n是指定每行最大的参数数量

    [root@cloucentos6 home]# cat file.txt

    1 2 3 4 5

    6 7 8

    9 10 11

    12 13

    14 15 16

    [root@cloucentos6 home]# cat file.txt | xargs -n 3

    1 2 3

    4 5 6

    7 8 9

    10 11 12

    13 14 15

    16

    xargs –d选项指定一个定界符

    范例:file.txt文本出现f的字符比较多,指定f为定界符

    [root@cloucentos6 home]# cat file.txt

    tomfkeyfxiaofzhang

    [root@cloucentos6 home]# cat file.txt | xargs -d f

    tom key xiao zhang

    [root@cloucentos6 home]# cat file.txt | xargs -d f -n 2

    tom key

    xiao zhang

    范例:xargs标准输入将格式化参数传递给命令

    [root@cloucentos6 home]# cat test.sh

    #!/bin/bash

    echo $*'#'

    [root@cloucentos6 home]# ./test.sh  a1

    a1#

    [root@cloucentos6 home]# ./test.sh  a1  a2

    a1 a2#

    [root@cloucentos6 home]# ./test.sh  a1  a2  a3

    a1 a2 a3#

    [root@cloucentos6 home]# cat  file.txt

    a1

    a2

    a3

    [root@cloucentos6 home]# cat file.txt | xargs -n 1 ./test.sh

    a1#

    a2#

    a3#

    [root@cloucentos6 home]# cat file.txt | xargs  ./test.sh

    a1 a2 a3#

    xargs结合find使用

    范例:删除home目录下指定以.ph结尾的普通文件,以-print0输出以xargs -0将作为输入定界符

    [root@cloucentos6 home]# find /home -type f -name "*.ph" -print0 | xargs -0 rm –f

    7.7cmp文件比较

    在文字处理上,最常出现的问题应该就是比较两个或两个以上的文件,看看它们的内容是否相同,即便文件名称不同也可以进行比较。

    范例:输出结果指出第一个不同处的位置,但剩下的不会指出。

    [root@cloucentos6 home]# cat file1.txt

    abc1

    abcd2

    abc1

    [root@cloucentos6 home]# cat file2.txt

    abc2

    abcd2

    abc123456

    [root@cloucentos6 home]# cmp file1.txt file2.txt

    file1.txt file2.txt differ: byte 4, line 1

  • 相关阅读:
    jquery获取元素各种宽高及页面宽高
    Mysql 多表联合查询效率分析及优化
    mysql连表操作是先连表还是先查询条件
    CSS实现垂直居中
    laravel中orderBy使用
    Spring官网改版后下载
    java.lang.NoClassDefFoundError: org/jaxen/JaxenException
    flex 简单跑马灯效果(竖着显示)
    Flex “跑马灯”效果(自定义“跑马灯”控件)
    当LinkButton无效时,光标不显示为手型
  • 原文地址:https://www.cnblogs.com/zoulongbin/p/6890948.html
Copyright © 2020-2023  润新知