Linux文件目录与操作


第三章 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
 

猜你喜欢

转载自blog.csdn.net/qq_42560102/article/details/82315547
今日推荐