Linux文件与目录管理

目录与路径

相对路径与绝对路径

  • 绝对路径
    从根目录/开始写起,如:/usr/local/bin(windows下绝对路径从每个盘符开始,如:C:/programe files)。一般而言,绝对路径的正确度比较好,建议写程序(shell脚本)来管理系统,使用绝对路径
  • 相对路径
    相对于目前工作目录的路径,如当前目录在/usr/local/bin下,要前往/root,则可以写作cd ../../../root

目录相关操作

  • .:代表此层目录
  • ..:代表上一层目录
  • -:代表前一个工作目录
  • ~:代表目前使用者身份所在的家目录
  • ~accoutn:代表account(账号名称)这个使用者的家目录
    注意:Linux下,在所有目录下面都会存在两个目录,...,分别代表此层和上一层目录。使用ls -al可以查看。
  • cd:切换目录
  • pwd:显示当前目录
  • mkdir:建立一个新目录
  • rmdir:删除一个空目录

cd(change directory,切换工作目录)

[jiang@jiangguobiao ~]$ su - root  # 切换到root用户下,也可直接写成su -
Password:
Last login: Sun Jul 12 22:11:11 CST 2020 on pts/0

# 回到家目录
[root@jiangguobiao ~]# cd ~
[root@jiangguobiao ~]# cd
[root@jiangguobiao ~]# cd #HOME
[root@jiangguobiao ~]# cd ~jiang
[root@jiangguobiao jiang]#

# 回到上一层目录
[root@jiangguobiao ~]# cd ..
[root@jiangguobiao /]#

# 回到前一个工作目录
[root@jiangguobiao /]# cd -
/root
[root@jiangguobiao ~]#

# 相对路径
cd ../root

# 绝对路径
cd /var/spool/mail

pwd(print working directory,显示目前所在目录)

pwd [-P]
-P:显示真正的路径,而非使用链接路径
[root@jiangguobiao mail]# ll /var/mail
lrwxrwxrwx. 1 root root 10 Nov  6  2019 /var/mail -> spool/mail
[root@jiangguobiao mail]# pwd
/var/mail
[root@jiangguobiao mail]# pwd -P
/var/spool/mail

mkdir(建立新的目录)

mkdir [-mp] 目录名称
-m:设置文件的权限,直接设置,不使用默认权限(unmask)
-p:递归创建

范例:

# 建立一个新的test目录
mkdir test

# 建立test1/test2/test3
mkdir -p test1/test2/test3 

# 建立权限为rwx--x--x的目录
mkdir -m 711 test2

rmdir(删除目录)

rmdir [-p] 目录名称
-p:递归删除,连同上层空的目录一起删除

范例:

# 创建一个新的test1空目录,并删除
[root@jiangguobiao test]# mkdir test1
[root@jiangguobiao test]# ls
test1
[root@jiangguobiao test]# rmdir test1
[root@jiangguobiao test]# ls


# 创建test1/test2/test3,并删除
[root@jiangguobiao test]# mkdir -p test1/test2/test3
[root@jiangguobiao test]# ls test1
test2
[root@jiangguobiao test]# ls test1/test2
test3
[root@jiangguobiao test]# rmdir test1
rmdir: failed to remove ‘test1’: Directory not empty
[root@jiangguobiao test]# rmdir -p test1
rmdir: failed to remove ‘test1’: Directory not empty
[root@jiangguobiao test]# rmdir -p test1/test2/test3
[root@jiangguobiao test]# ls

执行文件执行路径($PATH)

当执行一个命令,系统会按照PATH设置去每个PATH定义的目录下查找文件名为执行路径文件名的可执行文件。如果在PATH中找到多个文件名则先找到的先被执行。

[root@jiangguobiao test]# echo $PATH
/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
  • 不同身份的用户默认的PATH不同,默认能够随意执行的命令也不同
  • PATH可以修改
  • 使用绝对路径或相对路径直接指定某个命令的文件名来执行,会比查找PATH来的正确
  • 命令应该要放置在正确的目录下,执行才会比较方便
  • 本目录(.)最好不要放到PATH中

文件与目录管理

文件与目录查看:【ls】

ls [-aAdfFhilnrRSt] 文件名或目录名称
ls [--color={never, auto, always}] 文件名或目录名称
ls [--full-time] 文件名称或目录名称

-a:全部文件,连同隐藏文件(包含.以及..)一起列出来
-A:全部文件,连同隐藏文件,不包括【.】和【..】
-d:仅列出目录本身,而不是列出目录内的文件数据
-f:直接列出结果,而不进行排序
-h:以人类较易读的方式列出
-i:列出inode号码
-l:长列表,详细信息显示,包含文件的属性和权限等数据
-r:将排序结果反向输出
-R:连同子目录内容一起列出来
--full-time:以完整时间模式(包含年、月、日、时、分)

范例:

# 列出/etc下的所有文件,包含隐藏文件
ls -al /etc

# 显示家目录下的所有文件,以完整时间显示
ls -al --full-time ~

复制、移动和删除:cp、mv、rm

cp(copy)复制文件或目录

cp [-adfilprsu] 源文件(source) 目标文件(destination)
cp [options] source1 source2 source3 ... directory  # 移动多个文件
-d:若源文件为链接文件的属性,则复制链接文件属性而非文件本身
-f:强制(force),若目标文件已存在而且无法开启,则删除后再尝试一次
-i:若目标文件已存在,在覆盖时会先询问操作的进行
-l:进行硬链接的链接文件建立,而非复制文件本身
-a:相当于-dr --preserve=all
-p:连同文件的属性(权限、用户、时间)一起复制过去,而非使用默认属性(备份常用)
-s:复制成为符号链接文件
-u:目标文件比源文件旧才更新目标文件,或目标文件不存在的情况下才复制。
--preserve=all:除了-p的权限相关参数外,还加入SELinux的数据线,links、xattr等也复制。
注意:如果源文件有多个,则目标文件位置需要是“目录”

范例

# 复制/var/spool/mail到当前目录
cp -r /var/spool/mail .

# 将文件的所有特性一起复制过来
cp -a /var/log/wtmp wtmp_2

# cp的源文件和目标文件的权限是不同的,目标文件的拥有者通常会是命令操作者本身
[root@jiangguobiao ~]# ll test.txt
-rwxrwxr-x. 1 jiang jiang 21 Nov 17  2019 test.txt
[root@jiangguobiao ~]# cp test.txt ./testjaing
[root@jiangguobiao ~]# ll ./testjaing
-rwxr-xr-x. 1 root root 21 Jul 12 23:03 ./testjaing

# 复制为链接文件
[root@jiangguobiao ~]# cp -l test.txt ./testjiang/testlink
[root@jiangguobiao ~]# ll ./testjiang/
total 4
drwxr-xr-x. 2 root  root  117 Jul 12 23:00 mail
-rwxrwxr-x. 2 jiang jiang  21 Nov 17  2019 testlink
[root@jiangguobiao ~]# cp -s test.txt ./testjiang/testsoftlink
cp: ‘./testjiang/testsoftlink’: can make relative symbolic links only in current directory
[root@jiangguobiao ~]# cp -s test.txt ./testsoftlink
[root@jiangguobiao ~]# ll testsoftlink
lrwxrwxrwx. 1 root root 8 Jul 12 23:07 testsoftlink -> test.txt

cp复制注意点:

  • 是否需要完整的保留源文件的信息
  • 源文件是否为符号链接文件
  • 源文件是否为特殊的文件,如FIFO、socket
  • 源文件是否为目录

rm(删除文件或目录)

rm [-fir] 文件或目录
-f:强制删除,忽略不存在的文件,不发出警告信息
-i:交互模式,删除前询问使用者
-r:递归删除,常用户目录的删除

范例

# 删除文件前询问
rm -i bashrc  # ctrl-c终止命令执行

# 递归删除/tmp/etc
rm -rf /tmp/etc

# 命令前加上\,可以忽略alias的指定选项
\rm -r /tmp/etc

# 删除带有- 开头的文件,文件名最好不要用【-】开头,因为【-】后面接的是选项,单纯使用【rm -aa-】系统的命令会误判
rm -- -aa-
rm ./-aa-

mv(移动文件、目录或重命名)

mv [-fiu] source destination
mv [options] source1 source2 source2 ... directory

-f: 强制,如果目标文件已存在,不会询问直接覆盖
-i:若目标文件已存在,就会询问是否覆盖
-u:若目标文件已存在而且source比较新,才会更新

范例

# 移动文件
mv test.txt ./testjiang

# 重命名
mv test.txt test1.txt

# 移动并重命名
mv test.txt ./testjiang/test1.txt

# 移动多个文件
mv test.txt test2.txt ./testjiang

获取路径的文件名和目录名称

# 获取文件名
[root@jiangguobiao ~]# basename /etc/sysconfig/network-scripts/ifcfg-ens33
ifcfg-ens33

# 获取目录名称
[root@jiangguobiao ~]# dirname /etc/sysconfig/network-scripts/
/etc/sysconfig

文件内容查看

  • cat:从第一行开始显示文件内容
  • tac:从最后一行开始显示
  • nl:显示内容同时输出行号
  • more:分页显示
  • less:同more,但可以往前翻页
  • head:只看前面几行
  • tail:只看后面几行
  • od:以二进制方式读取文件内容

cat

cat [-AbEnTv] 文件名

-b:列出行号,仅针对非空白行显示,空白行不标行号
-n:打印行号,非空白行和空白行都显示
-T:将【tab】键以^I显示出来
-v:列出一些看不出来的特殊字符
-E:将结尾的换行符$显示出来
-A:相当于-vET

范例

# cat显示/etc/issue内容
[root@jiangguobiao ~]# cat /etc/issue
\S
Kernel \r on an \m

# 显示内容并打印行号
[root@jiangguobiao ~]# cat -n /etc/issue
     1  \S
     2  Kernel \r on an \m
     3

# 非空白行不打印行号
[root@jiangguobiao ~]# cat -b /etc/issue
     1  \S
     2  Kernel \r on an \m

# 将【tab】键显示
[root@jiangguobiao ~]# cat -T /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:
# --------------------------------------------------------
# MANDATORY_MANPATH^I^I^Imanpath_element
# MANPATH_MAP^I^Ipath_element^Imanpath_element
# MANDB_MAP^I^Iglobal_manpath^I[relative_catpath]
#---------------------------------------------------------
# every automatically generated MANPATH includes these fields
#
#MANDATORY_MANPATH ^I^I^I/usr/src/pvm3/man
#
MANDATORY_MANPATH^I^I^I/usr/man
.......................

tac

反向列示

nl(添加行号打印)

nl [-bnw] 文件名

-b:指定行号指定的方式
		-b a:不论是否为空行,同样列出行号
		-b t:如果有空行,空的那一行不要列出行号
-n:列出行号表示的方法
		-n ln:行号在屏幕的最左方显示
		-n rn:行号在自己栏位的最右方显示,而且不加0
		-n rz:行号在自己栏位的最右方显示,而且加0
-w:行号栏位的占用的字符数

范例

[root@jiangguobiao ~]# nl /etc/issue
     1  \S
     2  Kernel \r on an \m


# 不论空行同样显示行号
[root@jiangguobiao ~]# nl -b a /etc/issue
     1  \S
     2  Kernel \r on an \m
     3
     
# 行号前面自动补0
[root@jiangguobiao ~]# nl -b a -n rz /etc/issue
000001  \S
000002  Kernel \r on an \m
000003

# 指定栏位
[root@jiangguobiao ~]# nl -b a -n rz -w 3 /etc/issue
001     \S
002     Kernel \r on an \m
003

more(翻页查看)

  • 空格键(space):代表向下翻一页
  • Enter:向下翻一行
  • /string:向下查找string字符串
  • :f:立刻显示出文件名以及目前显示的行数
  • q:立刻离开more,不在显示文件内容
  • b或ctrl-b:往回翻页,只对文件有用,对管道无用

less

  • 空格键(space):代表向下翻一页
  • 【pagedown】:向下翻一页
  • 【pageup】:向上翻一页
  • Enter:向下翻一行
  • /string:向下查找string字符串
  • ?String:向上查找String字符串
  • n:重复前一个查找【在/string】后,按【n】往下查找下一个string
  • N:反向重复前一个查找,在【/string】后,按【n】往上查找前一个string
  • G:到最后一行
  • q:立刻离开more,不在显示文件内容

head

head [-n number] 文件
-n:后面接数字,代表显示几行

范例

# 显示5行
[root@jiangguobiao ~]# head -n 5 /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.

# 也可写成
[root@jiangguobiao ~]# head -5 /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.

# 后面接负数,代表列出前面所有的行数,但不包括后面的n行
[root@jiangguobiao ~]# cat -n /etc/man_db.conf | head -n -100
     1  #
     2  #
     3  # This file is used by the man-db package to configure the man and cat paths.
     4  # It is also used to provide a manpath for those without one by examining
     5  # their PATH environment variable. For details see the manpath(5) man page.
     6  #
     7  # Lines beginning with `#' are comments and are ignored. Any combination of
     8  # tabs or spaces may be used as `whitespace' separators.
     9  #
    10  # There are three mappings allowed in this file:
    11  # --------------------------------------------------------
    12  # MANDATORY_MANPATH                     manpath_element
    13  # MANPATH_MAP           path_element    manpath_element
    14  # MANDB_MAP             global_manpath  [relative_catpath]
    15  #---------------------------------------------------------
    16  # every automatically generated MANPATH includes these fields
    17  #
    18  #MANDATORY_MANPATH                      /usr/src/pvm3/man
    19  #
    20  MANDATORY_MANPATH                       /usr/man
    21  MANDATORY_MANPATH                       /usr/share/man
    22  MANDATORY_MANPATH                       /usr/local/share/man
    23  #---------------------------------------------------------
    24  # set up PATH to MANPATH mapping
    25  # ie. what man tree holds man pages for what binary directory.
    26  #
    27  #               *PATH*        ->        *MANPATH*
    28  #
    29  MANPATH_MAP     /bin                    /usr/share/man
    30  MANPATH_MAP     /usr/bin                /usr/share/man
    31  MANPATH_MAP     /sbin                   /usr/share/man

tail

tail [-n number] 文件
-n: 后面接数字,代表显示几行
-f:持续刷新显示后面所接文件中的内容,要等到按【ctrl-c】才结束

范例

[root@jiangguobiao ~]# tail -n 5 /etc/man_db.conf
#
#---------------------------------------------------------
# Flags.
# NOCACHE keeps man from creating cat pages.
#NOCACHE

# 后面接+number表示只列出number行之后的数据
[root@jiangguobiao ~]# cat -n /etc/man_db.conf | tail -n +100
   100  #---------------------------------------------------------
   101  # Section names. Manual sections will be searched in the order listed here;
   102  # the default is 1, n, l, 8, 3, 0, 2, 5, 4, 9, 6, 7. Multiple SECTION
   103  # directives may be given for clarity, and will be concatenated together in
   104  # the expected way.
   105  # If a particular extension is not in this list (say, 1mh), it will be
   106  # displayed with the rest of the section it belongs to. The effect of this
   107  # is that you only need to explicitly list extensions if you want to force a
   108  # particular order. Sections with extensions should usually be adjacent to
   109  # their main section (e.g. "1 1mh 8 ...").
   110  #
   111  SECTION         1 1p 8 2 3 3p 4 5 6 7 9 0p n l p o 1x 2x 3x 4x 5x 6x 7x 8x
   112  #
   113  #---------------------------------------------------------
   114  # Range of terminal widths permitted when displaying cat pages. If the
   115  # terminal falls outside this range, cat pages will not be created (if
   116  # missing) or displayed.
   117  #
   118  #MINCATWIDTH    80
   119  #MAXCATWIDTH    80
   120  #
   121  # If CATWIDTH is set to a non-zero number, cat pages will always be
   122  # formatted for a terminal of the given width, regardless of the width of
   123  # the terminal actually being used. This should generally be within the
   124  # range set by MINCATWIDTH and MAXCATWIDTH.
   125  #
   126  #CATWIDTH       0
   127  #
   128  #---------------------------------------------------------
   129  # Flags.
   130  # NOCACHE keeps man from creating cat pages.
   131  #NOCACHE

# 显示文件的11到第20行
cat -n /etc/man_db.conf | head -n 20 | tail -n 10

注意:【tail -f】动态显示数据时,不可以使用vim编辑,vim编辑器会把源文件删除建立一个新文件,【tail -f】动态显示不出来。

od(非纯文本文件)

od [-t type] 文件

-t:后面可以接各种类型【type】的输出,如:
		a:利用默认的字符来输出
		c:使用ASCII字符来输出
		d[size]:利用十进制(decimal)来输出数据,每个整数占用size Bytes
		f[size]:利用浮点数值(floating)来输出数据,每个数占用size Bytes
		o[size]:利用八进制(octal)来输出数据,每个整数占用size Bytes
		x[size]:利用十六进制(hexadecimal)来输出数据,每个整数占用size Bytes

范例

[root@jiangguobiao ~]# od -t c /usr/bin/passwd
0000000 177   E   L   F 002 001 001  \0  \0  \0  \0  \0  \0  \0  \0  \0
0000020 003  \0   >  \0 001  \0  \0  \0   H   2  \0  \0  \0  \0  \0  \0
0000040   @  \0  \0  \0  \0  \0  \0  \0 220   e  \0  \0  \0  \0  \0  \0
0000060  \0  \0  \0  \0   @  \0   8  \0  \t  \0   @  \0 035  \0 034  \0
0000100 006  \0  \0  \0 005  \0  \0  \0   @  \0  \0  \0  \0  \0  \0  \0
0000120   @  \0  \0  \0  \0  \0  \0  \0   @  \0  \0  \0  \0  \0  \0  \0
0000140 370 001  \0  \0  \0  \0  \0  \0 370 001  \0  \0  \0  \0  \0  \0
0000160  \b  \0  \0  \0  \0  \0  \0  \0 003  \0  \0  \0 004  \0  \0  \0
0000200   8 002  \0  \0  \0  \0  \0  \0   8 002  \0  \0  \0  \0  \0  \0
0000220   8 002  \0  \0  \0  \0  \0  \0 034  \0  \0  \0  \0  \0  \0  \0
0000240 034  \0  \0  \0  \0  \0  \0  \0 001  \0  \0  \0  \0  \0  \0  \0
0000260 001  \0  \0  \0 005  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0
0000300  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0
0000320 314   V  \0  \0  \0  \0  \0  \0 314   V  \0  \0  \0  \0  \0  \0
0000340  \0  \0      \0  \0  \0  \0  \0 001  \0  \0  \0 006  \0  \0  \0
0000360 300   Z  \0  \0  \0  \0  \0  \0 300   Z      \0  \0  \0  \0  \0
0000400 300   Z      \0  \0  \0  \0  \0   d 005  \0  \0  \0  \0  \0  \0
0000420 220 005  \0  \0  \0  \0  \0  \0  \0  \0      \0  \0  \0  \0  \0
0000440 002  \0  \0  \0 006  \0  \0  \0 340   Z  \0  \0  \0  \0  \0  \0
0000460 340   Z      \0  \0  \0  \0  \0 340   Z      \0  \0  \0  \0  \0
0000500 200 002  \0  \0  \0  \0  \0  \0 200 002  \0  \0  \0  \0  \0  \0
0000520  \b  \0  \0  \0  \0  \0  \0  \0 004  \0  \0  \0 004  \0  \0  \0
0000540   T 002  \0  \0  \0  \0  \0  \0   T 002  \0  \0  \0  \0  \0  \0
0000560   T 002  \0  \0  \0  \0  \0  \0   D  \0  \0  \0  \0  \0  \0  \0
0000600   D  \0  \0  \0  \0  \0  \0  \0 004  \0  \0  \0  \0  \0  \0  \0
0000620   P 345   t   d 004  \0  \0  \0 374   Q  \0  \0  \0  \0  \0  \0
0000640 374   Q  \0  \0  \0  \0  \0  \0 374   Q  \0  \0  \0  \0  \0  \0
..................................

touch(修改文件时间或创建空文件)

文件的三个时间含义:

  • 修改时间(modification time,mtime)
    当文件【内容数据】变更时,就会更新这个时间,内容数据指的是文件的内容,而不是文件的属性或权限
  • 状态时间(status time,ctime)
    当该文件【状态(status)】改变时,就会更新这个时间,如权限与属性更改,都会更新这个时间
  • 读取时间(access time,atime)
    当该文件内容被读取时,就会更新这个读取时间。如使用cat去读取,就会更新文件的atime
touch [-acdmt] 文件

-a:仅自定义atime
-c:仅修改文件的时间,若该文件不存在则不建立新文件
-d:后面可以接欲自定义的日期而不用目前的日期,也可使用--date="日期或时间"
-m:仅修改mtime
-t:后面可以接欲自定义的时间而不用目前的时间,格式为[YYYYMMDDhhmm]

范例

# 默认情况下,ls显示的是该文件的mtime,也就是该文件的内容上次被修改的时间
[root@jiangguobiao ~]# touch testtouch
[root@jiangguobiao ~]# ll testtouch
-rw-r--r--. 1 root root 0 Jul 13 22:59 testtouch

# 文件复制过来,mtime相同,由于文件刚刚建立,状态时间(ctime)变成现在的时间
[root@jiangguobiao ~]# cp -a ./test.txt ./testtouch.txt
[root@jiangguobiao ~]# ll test.txt
-rwxrwxr-x. 2 jiang jiang 21 Nov 17  2019 test.txt
[root@jiangguobiao ~]# ll testtouch.txt
-rwxrwxr-x. 1 jiang jiang 21 Nov 17  2019 testtouch.txt
[root@jiangguobiao ~]# ll --time=atime testtouch.txt
-rwxrwxr-x. 1 jiang jiang 21 Jul 12 23:03 testtouch.txt
[root@jiangguobiao ~]# ll --time=ctime testtouch.txt
-rwxrwxr-x. 1 jiang jiang 21 Jul 13 23:00 testtouch.txt

# 将日期调整为两天前
[root@jiangguobiao ~]# touch -d "2 days ago" testtouch.txt
[root@jiangguobiao ~]# ll testtouch.txt
-rwxrwxr-x. 1 jiang jiang 21 Jul 11 23:02 testtouch.txt

# -t改变了mtime和atime,但ctime还是原本的时间
[root@jiangguobiao ~]# touch -t 202007150715 testtouch.txt
[root@jiangguobiao ~]# ll testtouch.txt
-rwxrwxr-x. 1 jiang jiang 21 Jul 15  2020 testtouch.txt
[root@jiangguobiao ~]# ll --time=atime testtouch.txt
-rwxrwxr-x. 1 jiang jiang 21 Jul 15  2020 testtouch.txt
[root@jiangguobiao ~]# ll --time=ctime testtouch.txt
-rwxrwxr-x. 1 jiang jiang 21 Jul 13 23:09 testtouch.txt

文件与目录的默认值权限与隐藏权限

文件默认权限:umask

umask是指定目前用户在建立文件或目录时的权限默认值。

# umask的数字指该默认值需要减掉的权限
[root@jiangguobiao ~]# umask
0022   ---->与一般权限有关的是后面三个数字

# -S(Symbolic)以符号类型的方式显示权限
[root@jiangguobiao ~]# umask -S
u=rwx,g=rx,o=rx

在默认权限的属性上,目录与文件是不一样的,因为x权限对于目录非常重要。但一般文件的简历则不应该有执行权限,因为一般文件通常用于数据的记录,当然不需要执行的权限。因此,默认情况下:

  • 若用户建立为文件则默认没有可执行(x)权限,即只有rw,也就是最大为666,即-rw-rw-rw-
  • 若用户建立为目录,则由于x与是否可以进入此目录有关,因此默认所有权限均开放,即777,drwxrwxrwx

于是,因为umask为022,所以user并没有拿掉任何权限,group和others去掉了2(也就是w权限),那么当用户建立:

  • 文件:(-rw-rw-rw-)-(-----w–w-)=(-rw-r–r--)
  • 目录:(drwxrwxrwx)-(d----w–w-)=(drwxr-xr-x)
[root@jiangguobiao ~]# umask
0022
[root@jiangguobiao ~]# touch testumask
[root@jiangguobiao ~]# ll testumask
-rw-r--r--. 1 root root 0 Jul 14 22:51 testumask
[root@jiangguobiao ~]# mkdir testuu
[root@jiangguobiao ~]# ll testuu
total 0
[root@jiangguobiao ~]# ls -ld testuu
drwxr-xr-x. 2 root root 6 Jul 14 22:52 testuu

root的umask通常为0022,至于一般用户,通常umask为0002

[jiang@jiangguobiao ~]$ umask
0002

文件隐藏属性

文件的隐藏属性与系统安全(Security)有很大帮助,chattr可以配置文件隐藏属性,但chattr命令只能在ext2、ext3、ext4的Linux文件系统上面完整生效。

chattr

chattr [+-=] [ASacdistu] 文件或目录名称

+:增加某一个特殊参数,其他原本存在的参数不变
-:删除某一个特殊参数,其他原本存在的参数不变
=:直接设置参数,而且仅有后面接的参数
A:存取文件(或目录)时,存取时间atime将不会修改
S:修改文件,会同步写入磁盘(sync)
a:设置后,文件只能增加数据,而不能删除也不能修改数据,只有root可以设置这个属性
c:会自动将文件压缩,在读取是将会自动解压缩
d:当dump程序被执行,可以使该文件不被dump备份
i:可以让一个文件【不能被删除、改名、设置链接也无法写入或新增数据】
s:如该文件被删除,它将会被完全的从硬盘删除,如果误删,完全无法恢复
u:与s相反

范例

[jiang@jiangguobiao ~]$ chattr +i attrtest
chattr: Operation not permitted while setting flags on attrtest
[jiang@jiangguobiao ~]$ su - root
Password:
Last login: Tue Jul 14 22:36:58 CST 2020 from 192.168.0.112 on pts/0
[root@jiangguobiao ~]# touch attrtest
[root@jiangguobiao ~]# chattr +i attrtest
[root@jiangguobiao ~]# rm -rf attrtest
rm: cannot remove ‘attrtest’: Operation not permitted

对于logfile这样的日志文件,可以设置+a这个属性可以增加但不能修改旧数据与删除。

lsattr(显示文件隐藏属性)

lsattr [-adR] 文件或目录

-a:将隐藏文件的属性也显示出来
-d:如果接的是目录,仅列出目录本身的属性而非目录内的文件名
-R:连同子目录的数据也一并列出来

范例

[root@jiangguobiao ~]# lsattr attrtest
----i----------- attrtest

文件特殊权限

[root@jiangguobiao ~]# ls -ld /tmp
drwxrwxrwt. 94 root root 8192 Jul 14 23:11 /tmp
[root@jiangguobiao ~]# ls -l /usr/bin/passwd
-rwsr-xr-x. 1 root root 27856 Aug  9  2019 /usr/bin/passwd

##SUID(Set UID)
-rwsr-xr-x

  • SUID权限仅对二进制程序有效
  • 执行者对于该程序需要具有x的可执行权限
  • 本权限仅在执行该程序的过程中有效(run-time)
  • 执行者将具有该程序拥有者(owner)的权限

如/etc/shadow不能让一般用户jiang去读写,为什么jiang还能够修改这个文件内的密码:

  1. jiang对于/usr/bin/passwd这个二进制程序具有x权限,表示jiang能执行passwd
  2. passwd的拥有者为root
  3. jiang执行passwd的过程中,暂时获得了root的权限
  4. /etc/shadow就可以被jiang所执行的passwd所修改

:SUID仅可用在二进制程序,不能够用在shell脚本上

SGID(Set GID)

当s标志在文件拥有者的x项为SUID,s在用户组的x时则成为SGID

[root@jiangguobiao ~]# ls -l /usr/bin/locate
-rwx--s--x. 1 root slocate 40520 Apr 11  2018 /usr/bin/locate

与SUID不同,SGID可以针对文件或目录来设置。对于文件,SGID有如下功能:

  • SGID对二进制程序有用
  • 程序执行者对于该程序来说,需具有x的权限
  • 执行者在执行的过程中将会获得该程序用户组的支持

对于一个目录,SGID具有如下功能:

  • 用户若对于此目录具有r与x的权限时,该用户能够进入此目录
  • 用户在此目录下的有效用户组(effective group)将会变成该目录的用户组
  • 用途:若用户在此目录下具有w权限(可以新建文件),则用户所建立的新文件,该新文件的用户组与此目录的用户组相同

##SBIT(Sticky Bit)
SBIT目前仅针对目录有效,对于文件已经没有效果,SBIT对目录的作用:

  • 当用户对于此目录具有w、x权限,即具有写入权限
  • 当用户在该目录下建立文件或目录时,仅有自己与root才有权力删除该文件。

SUID/SGID/SBIT权限设置

  • SUID为4
  • SGIT为2
  • SBIT为1
    假设要将一个文件的权限设置为【-rwsr-xr-x】,由于s在用户权限中,所以是SUID,因此,在原先的755前要加上4,也就是【chmod 4755 filename】

范例

[root@jiangguobiao ~]# touch test1
[root@jiangguobiao ~]# ls -l test1
-rw-r--r--. 1 root root 0 Jul 14 23:46 test1
[root@jiangguobiao ~]# chmod 6755 test1
[root@jiangguobiao ~]# ls -l test1
-rwsr-sr-x. 1 root root 0 Jul 14 23:46 test1

# 由于s和t都是取代x权限的,但执行7666时,user、group和others都没有x,所以S和T代表就是空的
[root@jiangguobiao ~]# chmod 7666 test1
[root@jiangguobiao ~]# ls -l test1
-rwSrwSrwT. 1 root root 0 Jul 14 23:46 test1

文件类型:file

# ASCII纯文本文件
[root@jiangguobiao ~]# file .bashrc
.bashrc: ASCII text

# 执行文件,包括文件的SUID权限,兼容Intel x86-64等级的硬件平台。使用的是LInux内核2.6.32的动态链接库
[root@jiangguobiao ~]# file /usr/bin/passwd
/usr/bin/passwd: setuid ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.32, BuildID[sha1]=471dad50eb96512f90dd9394adbd7513ae60f072, stripped

命令与文件的查找

脚本文件的查找

which [-a] command

-a:将所有由PATH目录中可以找到的命令均列出,而不止第一个被找到的命令名称

范例

[root@jiangguobiao ~]# which which
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
        /usr/bin/alias
        /usr/bin/which
[root@jiangguobiao ~]# which history
/usr/bin/which: no history in (/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin)
[root@jiangguobiao ~]# which ls
alias ls='ls --color=auto'
        /usr/bin/ls

:which命令是根据【PATH】这个环境变量所规范的路径,去查找执行文件的文件名

文件查找

whereis(由一些特定的目录中查找文件)

whereis [-bmsu] 文件或目录名

-l:可以列出whereis会去查询的几个主要目录
-b:只找binary(二进制)格式文件
-m:只找在说明文件manual路径下的文件
-s:只找source源文件
-u:查找不在上述三个项目当中的其他特殊文件

范例

[root@jiangguobiao ~]# whereis ifconfig
ifconfig: /usr/sbin/ifconfig /usr/share/man/man8/ifconfig.8.gz
[root@jiangguobiao ~]# whereis passwd
passwd: /usr/bin/passwd /etc/passwd /usr/share/man/man1/passwd.1.gz /usr/share/man/man5/passwd.5.gz

locate

locate [-ir] keyword

-i: 忽略大小写差异
-c:不输出文件名,仅计算找到的文件数量
-l:仅输出几行的意思,如仅输出5行,-l 5
-S:输出locate所使用数据文件的相关信息,包括改数据库记录的文件/目录数量
-r:后面可接正则表达式

范例

[root@jiangguobiao ~]# locate -l 5 passwd
/etc/passwd
/etc/passwd-
/etc/pam.d/passwd
/etc/security/opasswd
/root/passwd
[root@jiangguobiao ~]# locate -S
Database /var/lib/mlocate/mlocate.db:
        22,134 directories
        226,642 files
        13,892,376 bytes in file names
        5,506,357 bytes used to store database

locate查找数据很快,是因为其寻找的数据是由建立的数据库/var/lib/mlocate里面的数据查找到的,而不用去硬盘当中读取数据。

如果建立新的文件,使用locate则会查找不到,这时需要使用【updatedb】,updatedb命令会根据/etc/updatedb.conf设置去查找系统硬盘内的文件,并更新/var/lib/mlocate内的数据库文件,然后在使用locate查找即可。

find

find [PATH] [option] [action]

1. 与时间相关:-atime、-ctime和-mtime,如-mtime
		-mtime n:n天之前的【1天之内】被修改过的文件
		-mtime +n:列出在n天之前(不含n天本身)被修改过的文件
		-mtime -n:n天之内(含n天)被修改过的文件
		-newer file:找出比file还要新的文件

2. 与 使用者或用户组名称有关
		-uid n:n为数字,账号id
		-gid n:用户组id
		-user name:使用者名称
		-group name:用户组名称
		-nouser:查找文件的拥有者不在/etc/passwd中
		-nogroup:查找文件的用户组不在./etc/group的文件

3. 与文件权限和名称有关
		-name filename:查找文件名称为filename的文件
		-size [+-]SIZE:查找比SIZE还要大【+】或小【-】的文件,SIZE的单位:c(Bytes),k代表(KB)
		-type TYPE:查找文件类型为TYPE的文件,f为一般文件,b,c为设备文件,d为目录,l为链接文件,s为socket文件,p为FIFO
		-perm mode:查找文件权限【刚好等于】mode的文件
		-perm -mode:查找文件权限【必须要全部囊括mode的权限】的文件,如查找0744,位-rwx-r--r--,则文件权限为4755时也会被列出,-rws-r-xr-x
		-perm /mode:查找文件权限【包含任一mode】的文件,如查找/755,-rwxr-xr-x,则-rw-------也会查找出来

4. -exec command:-exec后可再接额外的命令来处理找到的结果
5. -print:将结果打印到屏幕上,这个操作是默认操作

范例

# 0表示目前的时间,即从先开始到24小时前,也就是24小时内
[root@jiangguobiao ~]# find / -mtime 0
/dev
/dev/vcsa6
/dev/vcs6
/dev/vcsa5
/dev/vcs5
/dev/vcsa4
/dev/vcs4
/dev/vcsa3
/dev/vcs3
/dev/vcsa2
/dev/vcs2
/dev/vsock
/dev/dmmidi
/dev/midi
/dev/cdrom
/dev/rfkill
/dev/sg1
/dev/sg0
/dev/vmci
/dev/vhci
/dev/btrfs-control
/dev/uhid
/dev/loop-control
/dev/lp3
/dev/lp1
/dev/snd

# 找出文件比/etc/passwd新的文件
[root@jiangguobiao ~]# find /etc -newer /etc/passwd
/etc
/etc/resolv.conf
/etc/pki
/etc/pki/rpm-gpg
/etc/pki/product
/etc/pki/product-default
/etc/yum.repos.d
/etc/cups
/etc/cups/subscriptions.conf.O
/etc/cups/subscriptions.conf
/etc/group
/etc/gshadow
/etc/systemd/system/multi-user.target.wants
/etc/systemd/system/multi-user.target.wants/docker.service
/etc/shadow
/etc/bash_completion.d
/etc/sysconfig
/etc/sysconfig/docker-storage
/etc/sysconfig/docker-storage-setup

# +4代表大于等于5天的文件
# -4代表小于等4天内的文件
# 4则代表4-5那一天的文件


# 查找用户未jiang的文件
find / -user jiang

# 查找文件名包含passwd的文件
find / -name "*passwd*"

# 查找/usr/bin、/usr/sbin下木有SUID或SGID的文件
find /usr/bin /usr/sbin -perm /6000

# 查找以上文件并用ls列出
find /usr/bin /usr/sbin -perm /6000 -exec ls -l {} \;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值