2.14 文件和目录权限chmod
2.15 更改所有者和所属组chown
2.16 umask
2.17 隐藏权限lsattr/chattr
一、文件和目录权限chmod
命令chmod(change mode简写)用于改变用户对文件/目录的读写执行权限;
格式:chmod xyz 文件名 (xyz是数字)
[root@linux-151 tmp]# chmod 750 111
[root@linux-151 tmp]# ls -ld 111
drwxr-x--- 2 root root 6 3月 28 23:11 111
查看目录信息
[root@linux-151 tmp]# ls -ld 111
drwxr-xr-x 2 root root 6 3月 28 23:11 111
文件或目录的详细信息分9段,第1段drwxr-xr-x包含文件的类型和所有者,所属组,其他用户对文件的权限;
r(read)可读
w(write)可写
x(executable)可执行
r=4 w=2 x=1
注:目录必须有x权限,文件没有x权限
1. chmod //修改文件和目录的权限
[root@linux-151 tmp]# ls -ld 111
drwxr-xr-x 2 root root 6 3月 28 23:11 111
[root@linux-151 tmp]# chmod 750 111
[root@linux-151 tmp]# ls -ld 111
drwxr-x--- 2 root root 6 3月 28 23:11 111
2. chmod –R //联级更改,修改目录本身及目录下所有的子目录文件权限
drwxr-xr-x 2 root root 6 3月 28 23:42 222/123
-rw-r--r-- 1 root root 0 3月 28 23:42 222/1.txt
[root@linux-151 tmp]# chmod 700 -R 222
[root@linux-151 tmp]# ls -ld 222 -l 222/1.txt -ld 222/123
drwx------ 3 root root 30 3月 28 23:42 222
drwx------ 2 root root 6 3月 28 23:42 222/123
-rwx------ 1 root root 0 3月 28 23:42 222/1.txt
3. chmod a=rwx,g=rx,o=rx 文件名(不常用)
u(user) 所有者
g(group)所属组
o(others)其他用户
[root@linux-151 tmp]# chmod u=rwx,g=rx,o=rx -R 222
[root@linux-151 tmp]# ls -ld 222 -l 222/1.txt -ld 222/123
drwxr-xr-x 3 root root 30 3月 28 23:42 222
drwxr-xr-x 2 root root 6 3月 28 23:42 222/123
-rwxr-xr-x 1 root root 0 3月 28 23:42 222/1.txt
二、 更改所有者和所属组chown
chown(change ownar)命令用来修改文件的所属主(所有者)和所属组
chown -R 作用于目录联级修改,目录本身和子目录文件。
[root@linux-151 ~]# useradd user1 \\创建用户user1[root@linux-151 ~]# groupadd group1 \\创建组 group1[root@linux-151 ~]# chown user1:group1 5.txt \\修改文件5.txt所属主为user1,所属组为group1[root@linux-151 ~]# ls -l 5.txt-rw-r--r--. 1 user1 group1 78 3月 28 23:01 5.txt
chown user1 1.txt \\修改属主
chown user1:group1 1.txt \\修改属主,属组
chown user1.group1 1.txt \\修改属主,属组
chown :group1 1.txt \\修改属组
chown .group1 1.txt \\修改属组
[root@linux-151 ~]# ls -l 1.txt-rw-r--r--. 1 root root 0 3月 28 23:44 1.txt[root@linux-151 ~]# chown lem 1.txt[root@linux-151 ~]# !lsls -l 1.txt-rw-r--r--. 1 lem root 0 3月 28 23:44 1.txt[root@linux-151 ~]# chown :lem 1.txt[root@linux-151 ~]# !lsls -l 1.txt-rw-r--r--. 1 lem lem 0 3月 28 23:44 1.txt[root@linux-151 ~]# chown root:root 1.txt[root@linux-151 ~]# ls -l 1.txt-rw-r--r--. 1 root root 0 3月 28 23:44 1.txt[root@linux-151 ~]# chown lem.lem 1.txt \\chown也可以用点来隔开
[root@linux-151 ~]# !lsls -l 1.txt-rw-r--r--. 1 lem lem 0 3月 28 23:44 1.txt
chgrp 修改文件属组 chgrp group1 1.txt
chgrp -R group1 111
!ls 表示命令历史中以ls开头距我访问最近的一条命令
chown -R XX:XX /tmp/aming 把所有主所有这组一起改
history 历史命令
[root@linux-151 ~]# history
16 midir test
17 mkdir test
18 ls
!18 !后面加上历史命令前面的序号也可以执行。
[root@linux-151 ~]# !18
ls
111.bak 222 4.txx 5.txt 6.txt anaconda-ks.cfg install.log.syslog test
1.txt 444 4.txy 666 777 install.log prem
三、umask
umask命令用于改变文件以及目录的默认权限
umask XXX(3个x表示数字,原本umask值为4位,第一位默认省略)
umask 022
[root@linux-151 ~]# umask0022
注:通常只用到后面三位022 --- -w- -w-
系统默认情况下目录权限值为755,文件权限为644,这个是有umask规定的
当umask值为022时,系统文件和目录的默认权限为
目录 文件
777 rwx rwx rwx 666 rw- rw- rw-
-022 --- -w- -w- -022 --- -w- -w-
= rwx r-x r-x rw- r-- r--
= 7 5 5 6 4 4
当umask值为003时,系统文件和目录的默认权限为
目录 文件
777 rwx rwx rwx 666 rw- rw- rw-
-003 --- --- -wx -003 --- --- -wx
= rwx rwx r-- rw- rw- r--
= 7 7 4 6 6 4
四、隐藏权限lsattr/chattr
1、lsattr 查看文件的特殊属性
lsattr -d 查看目录本身特殊属性
[root@linux-151 tmp]# lsattr 222
---------------- 222/1.txt
---------------- 222/123
[root@linux-151 tmp]# chattr +i 222/1.txt
[root@linux-151 tmp]# chattr +a 222/123/ 1.txt 2.txt
[root@linux-151 tmp]# chattr +a 222/123/
[root@linux-151 tmp]# lsattr -d 222
-----a---------- 222
lsattr -R 联级,查看目录下子目录子文件的特殊属性
[root@linux-151 tmp]# lsattr -R 222
----i----------- 222/1.txt
-----a---------- 222/123
222/123:
---------------- 222/2.txt
lsattr -a 查看目标目录所有文件(包含隐藏文件)的特殊属性
2、chattr 增加删除设定特殊属性
chattr +a 增加后只能追加,不能删除或者直接更改它的内容,非root用户不能设置该属性
chattr -a 删除追加
chattr +i 增加后不能做任何操作,不能添加,删除,重命名,设定链接,修改属主,属组。
chattr -i 删除隐藏权限
chattr +A 增加后文件或者目录的atime将不可被修改
chattr +S 增加后会将数据同步写入磁盘中
例1:给文件1.txt加上i权限,然后进行删除,修改、追加内容,修改文件属性,所有者,所属组。
[root@linux-151 tmp]# chattr +i 222/1.txt
[root@linux-151 tmp]# lsattr 222/1.txt
----i----------- 222/1.txt
[root@linux-151 tmp]# rm 222/1.txt
rm:是否删除普通空文件 "222/1.txt"?y
rm: 无法删除"222/1.txt": 不允许的操作
[root@linux-151 tmp]# echo '11111'>222/1.txt
-bash: 222/1.txt: 权限不够
[root@linux-151 tmp]# echo '11111'>>222/1.txt
-bash: 222/1.txt: 权限不够
[root@linux-151 tmp]# chmod 755 222/1.txtchmod: 更改"222/1.txt" 的权限: 不允许的操作
[root@linux-151 tmp]# chown lem:lem 222/1.txtchown: 正在更改"222/1.txt" 的所有者: 不允许的操作
删除i权限后就能正常修改了。
[root@linux-151 tmp]# chattr -i 222/1.txt
[root@linux-151 tmp]# chown lem:lem 222/1.txt
[root@linux-151 tmp]# ls -l 222/1.txt
-rw-r--r-- 1 lem lem 0 3月 28 23:42 222/1.txt
例2:给文件1.txt加上a权限,然后进行删除,修改、追加内容,修改文件属性,所有者,所属组。
[root@linux-151 tmp]# chattr +a 222/1.txt
[root@linux-151 tmp]# lsattr 222/1.txt
-----a---------- 222/1.txt
[root@linux-151 tmp]# rm 222/1.txt
rm:是否删除普通空文件 "222/1.txt"?y
rm: 无法删除"222/1.txt": 不允许的操作
[root@linux-151 tmp]# echo '1111'>222/1.txt
-bash: 222/1.txt: 不允许的操作
[root@linux-151 tmp]# echo '1111'>>222/1.txt
[root@linux-151 tmp]# cat 222/1.txt1111
[root@linux-151 tmp]# chmod 755 222/1.txtchmod: 更改"222/1.txt" 的权限: 不允许的操作
[root@linux-151 tmp]# mv 222/1.txt 222/2.txt
mv: 无法将"222/1.txt" 移动至"222/2.txt": 不允许的操作
[root@linux-151 tmp]# chown root:root 222/2.txtchown: 无法访问"222/2.txt": 没有那个文件或目录
注:1.txt加上a权限后,只能进行追加内容
给目录赋予i权限后,依旧可以对该目录下已存在的文件进行编辑
实战,这样别人就不能删除,只能追加,特别是系统日志
二周第四次课
2.18 特殊权限set_uid
2.19 特殊权限set_gid
2.20 特殊权限stick_bit
2.21 软链接文件
2.22 硬连接文件
一、 特殊权限set_uid
set_uid:该权限针对二进制可执行文件,使文件在执行阶段具有文件所有者的权限; 通俗一点讲就是,普通用户想要访问一个没有其他用户可执行权限的目录时,暂时借助二进制文件的所属主权限去访问这个目录,如果没有s权限,就不能访问; 注意:普通用户要有这个二进制文件的可执行权限。如果没有,那也是不能访问的。 我们系统中passwd命令就具有s权限。当普通用户执行passwd命令的时候,可以临时获得root权限,从而可以更改密码;
系统修改密码命令:passwd
[root@linux-151 ~]# which passwd
/usr/bin/passwd
[root@linux-151 ~]# ls -l /usr/bin/passwd
-rwsr-xr-x. 1 root root 27832 3月 29 2018 /usr/bin/passwd
系统密码文件:/etc/shadow
[root@linux-151 ~]# ls -l /etc/shadow
---------- 1 root root 663 3月 29 23:14 /etc/shadow
注意:这里所有者,所属组,其他用户都没有执行权限,但是root是超级用户,它具有至高无上的权限,所以root用户是可以修改密码的;按理说普通用户没有执行权限是没办法去修改密码的,但是我们passwd命令具有s权限,普通用户在使用命令passwd时,可以临时获取到passwd所属主的root权限,所以能修改密码。
例子:使用普通用户查看/root/目录:
[root@linux-151 ~]# ls -ld /root/ //root目录没有其他用户可执行权限;
dr-xr-x---. 3 root root 163 3月 29 23:45 /root/
[root@linux-128 ~]# su - lem //切换用户
上一次登录:一 3月 29 23:56:30 CST 2018pts/0 上
[lem@linux-151 ~]$ ls /root/
ls: 无法打开目录/root/: 权限不够 //没有权限
我们怎样才能让普通用户lem打开/root/目录呢? 解:我们给ls命令加上一个s权限,让用户lem临时拥有ls的所有者权限;例如:
[root@linux-151 ~]# which ls
alias ls='ls --color=auto'
/usr/bin/ls
[root@linux-151 ~]# chmod u+s /usr/bin/ls
[root@linux-151 ~]# ls -l /usr/bin/ls
-rwsr-xr-x. 1 root root 117656 3月 29 2018 /usr/bin/ls
[root@linux-151 ~]# su - lem
上一次登录:五 3月 29 00:18:52 CST 2018pts/0 上
[lem@linux-151 ~]$ ls /root/
anaconda-ks.cfg
添加/usr/bin/ls 以后其他用户就可以用ls 这个命令看文件
chmod u+s /usr/bin/ls 临时用户拥有所有者身份
chmod g+s /usr/bin/ls 临时用户拥有所有组身份
二进制 就是可执行文件
设置s权限:
chmod u+s 二进制文件名
chmod u=rws 二进制文件名
chmod 4755 二进制文件名 这个4是这样得出来的:
suid = 4
sgid = 2
stick_bit = 1
取消s权限:
chmod u-s
chmod u=rws
chmod 755
二、 特殊权限set_gid
set_gid:该权限可以作用在二进制可执行文件上,也可以作用在目录上。当作用在文件上时,其功能和set_uid一样,它会使文件在执行阶段具有该文件所属组的权限。作用在目录上时,任务用户在此目录下创建的文件和目录都具有和该目录所属组相同的组(假设将123目录所属组设置为user,并对123目录赋予set_gid权限,当root用户在该目录下创建子目录或子文件的所属组也均为user)。
设置s权限:
chmod g+s 二进制文件名或目录
chmod g=rws 二进制文件名或目录
chmod 2755 二进制文件名或目录
例:作用在二进制文件上
[root@linux-151 ~]# ls -l /bin/ls
-rwxr-xr-x. 1 root root 112664 3月 29 2018 /bin/ls
[root@linux-151 ~]# chmod g+s /bin/ls \\/bin/ls 加上sgid
[root@linux-151 ~]# !ls
ls -l /bin/ls
-rwxr-sr-x. 1 root root 112664 3月 29 2018 /bin/ls
[root@linux-151 ~]# su - lem
[lem@linux-151 ~]$ ls /root \\lem用户会调用命令ls属组的权限去访问/root目录
111.bak 444 5.txt 777 install.log prem
222 4.txx 6.txt anaconda-ks.cfg install.log.syslog test
例子:作用于目录上
[root@linux-151 tmp]# mkdir 555 \\创建目录555
[root@linux-151 tmp]# chmod 777 555 \\设置权限所有人可读可写可执行
[root@linux-151 tmp]# su - lem \\切换用户 lem下
[lem@linux-151 ~]$ cd /tmp/555
[lem@linux-151 555]$ mkdir dir \\创建目录dir
[lem@linux-151 555]$ touch file \\创建文件file
[lem@linux-151 555]$ logout \\退出用户到root下
[root@linux-151 tmp]# chmod g+s 555 \\将目录555添加sgid权限
[root@linux-151 tmp]# su - lem \\切换用到lem下
[lem@linux-151 tmp]$ cd 555
[lem@linux-151 555]$ mkdir dir1 \\创建目录dir1
[lem@linux-151 555]$ touch file1 \\创建文件file1
[lem@linux-151 555]$ ls -l
总用量 8
drwxrwxr-x. 2 lem lem 4096 3月 29 23:04 dir
drwxrwsr-x. 2 lem root 4096 3月 29 23:06 dir1 \\创建的目录的属组是调用的目录的属组
-rw-rw-r--. 1 lem lem 0 3月 29 23:04 file
-rw-rw-r--. 1 lem root 0 3月 29 23:06 file1 \\创建的文件的属组也是调用的目录的属组
三、特殊权限stick_bit
sticky_bit:防删除位。一个目录下的文件嫩不能被删除,不是取决于文件本身的属组属组还有其他权限, 而是在于这个文件所在目录的权限是否可读可写可执行,如果目录可写,那就意味着我们可以删除目录下的任何文件。当一个目录的权限为其他人可读可写可执行的时候,那么其他用户都可以删除这个目录下的文件,包括root文件,为了防止文件被其他用户删除,我们给这这个目录增加一种特殊权限:sticky,防删除位,要用root用户操作。这样一个用户就不能删除另一个用户创建的文件了。只有root和创建文件本人才能够删除。
防止别人删除自己的文件 root用户除外
设置t权限:
chmod o+t 目录
chmod o=rwt 目录
chmod 1755 目录
例子:
root@linux-151 ~]# cd /tmp
[root@linux-151 tmp]# chmod o+t 666
[root@linux-151 tmp]# ls -ld 666
drwxrwxrwt. 3 root root 4096 3月 30 00:42 666
[root@linux-151 tmp]# su - user1
[user1@linux-151 ~]$ cd /tmp/666
[user1@linux-151 666]$ ls -l
总用量 8
drwxrwxr-x. 2 lem lem 4096 3月 30 00:42 11
-rw-r--r--. 1 root root 337 3月 30 00:22 1.txt
-rw-r--r--. 1 root root 0 3月 30 00:22 1.xt
-rw-rw-r--. 1 lem lem 0 10月 27 15:42 2.txt
[user1@linux-151 666]$ rm -r 111rm: 无法删除"111": 没有那个文件或目录
[user1@linux-151 666]$ rm -r 11
rm:是否删除有写保护的目录 "11"?y
rm: 无法删除"11": 不允许的操作
[user1@linux-151 666]$ rm -r 1.txt
rm:是否删除有写保护的普通文件 "1.txt"?y
rm: 无法删除"1.txt": 不允许的操作
[user1@linux-151 666]$ rm -rf 2.txt
rm: 无法删除"2.txt": 不允许的操作
[user1@linux-151 666]$ logout
[root@linux-151 tmp]# ls1 22.txt 3 555 6.txt file1 test1
1.txt 2.txt 3.txt 666 dir1 test yum.log
[root@linux-151 tmp]# cd 666
[root@linux-151 666]# ls11 1.txt 1.xt 2.txt
[root@linux-151 666]# rm -rf 11
[root@linux-151 666]# ls1.txt 1.xt 2.txt
[root@linux-151 666]# su - lem
[lem@linux-151 ~]$ cd /tmp/666
[lem@linux-151 666]$ ls -l
总用量 4
-rw-r--r--. 1 root root 337 3月 30 00:22 1.txt
-rw-r--r--. 1 root root 0 3月 30 00:22 1.xt
-rw-rw-r--. 1 lem lem 0 10月 27 15:42 2.txt
[lem@linux-151 666]$ rm 2.txt \\用户lem才能删除2.txt文件
[lem@linux-151 666]$ ls -l
总用量 4
-rw-r--r--. 1 root root 337 3月 30 00:22 1.txt
-rw-r--r--. 1 root root 0 3月 30 00:22 1.xt
四、 软链接文件
软链接:是建立一个独立的文件,当读取这个链接文件时,它会把读取的行为转发到该文件所链接的文件上。
软链接 相当于一个快捷方式
软链接的大小取决于软链接源文件的路径长短
软链接支持文件和目录
软链接的源文件不能被删除,如果删除源文件,软链接文件会失效;软链接删除,源文件不受影响。 创建软链接格式:
尽量使用绝对路径来做软链接 ln -s 源文件 目标文件(链接文件)
[root@linux-151 tmp]# ln -s /tmp/111/ /root/12
[root@linux-151 tmp]# ls -l /root
总用量 4
lrwxrwxrwx 1 root root 9 3月 30 01:12 12 -> /tmp/111/
-rw-------. 1 root root 1422 3月 30 01:51 anaconda-ks.cfg
比较源文件和软链接的大小,软连接的大小与源文件的长度有关
[root@linux-151 tmp]# du -sh /tmp/111/ /root/124.0K /tmp/111/0 /root/12
du -sh 文件 \查看文件大小
df -h \查看磁盘情况
ln -s /tmp/yum.log /root/ 创建软连接把tmp下面的yum.log 放到root下面做个软连接 前面真的文件后面快捷方式
软连接不仅可以连接文件也可以连接目录还可以跨分区
例子:boot目录下aming.log要写满了,想把文件放到/目录下
先拷贝下/boot/aming.log 放到/根目录下 adming..log
然后删除/boot 目录adming.log 再创建一个软连接 ln -s /aming.log /boot/aming.log 这样 boot就个快捷方式真正的全部写到/aming.log下
Tips:当目标文件所属磁盘剩余空间不足且需继续向该文件写入数据时,可采用软链接的方式,现将目标文件拷贝至新磁盘中,再将原磁盘中该文件删除,并在原磁盘的相同路径下做软链接文件,链接至拷贝到新磁盘中的该文件即可。
五、硬连接文件
硬链接:当系统要读取一个文件时,会先读取inode信息,然后再根据inode的信息到块区域将数据取出来。而硬链接就是直接在建立一个inode链接到文件放置的块区域,即进行硬链接时,该文件的内容没有任何变化,只是增加了一个指向这个文件的inode,并不会额外占用磁盘空间。
硬链接不能夸文件系统,不同的文件系统有不同的inode table;
硬链接不能链接目录; 格式: ln 来源文件 目标文件
[root@linux-151 tmp]# ln /tmp/123.txt /root/321.txt
[root@linux-151 tmp]# ls -lih /tmp/123.txt8389029 -rw-r--r-- 2 root root 17K 3月 30 01:31 /tmp/123.txt
[root@linux-151 tmp]# ls -lih /root/321.txt8389029 -rw-r--r-- 2 root root 17K 3月 30 01:31 /root/321.txt
注意:我们创建的硬链接和源文件的inode一样,大小一样。
硬连接不能对目录做硬连接,而且不能跨分区
硬链接就像一张皮有多张皮,inode号一样,皮可以删掉
删除源文件,硬链接不受影响
[root@linux-151 tmp]# rm /tmp/123.txt
rm:是否删除普通文件 "/tmp/123.txt"?y
[root@linux-151 tmp]# ls -l /root/321.txt
-rw-r--r-- 1 root root 16816 3月 30 01:31 /root/321.txt
硬链接不能链接目录
[root@linux-151 tmp]# ln /tmp/222/ /root/23
ln: "/tmp/222/": 不允许将硬链接指向目录
二周第五次课
2.23/2.24/2.25 find命令
2.26 文件名后缀
一、find命令
搜索工具 which :用于查找可执行文件的绝对路径;
[root@linux-151 ~]# which lsalias ls='ls --color=auto'
/usr/bin/ls
whereis:通过预先生成的一个文件列表库查找与给出的文件名相关的文件;
格式: whereis [选项] 文件名
-b:只查找二进制文件
-m:只查找帮助文件
-s:只查找源代码文件
[root@linux-151 ~]# whereis -b ls
ls: /usr/bin/ls
[root@linux-151 ~]# whereis ls
ls: /usr/bin/ls /usr/share/man/man1/ls.1.gz
locate:和whereis类似,也是通过查找预先生成的文件列表库来查找文件在哪里,后面直接跟文件名;需要安装mlocate软件包:
[root@linux-151 ~]# yum install -y mlocate
[root@linux-151 ~]# updatedb
[root@linux-151 ~]# locate yum.log
/var/log/yum.log
安装好后,第一次运行locate会报错,需要运行updatedb命令立即生成或者更新这个库文件;默认情况下这个库每周更新一次;locate不适合精准查找。
find命令
格式:find [路径] [参数]
-type 文件类型:f(文件)d(目录)l(软链接)s(sock)d(块设备)c(串口设备键盘鼠标等)
-name 文件名
-size 文件大小(10k;10M等)
-mmin +- 分钟数
-inum 跟inode,通过inode来查找一个文件的硬链接
-o 或者
-exec <执行指令>:假设find指令的回传值为True,就执行该指令。
-atime +n/-n 表示访问或执行时间大于或小于n天的文件。
-ctime +n/-n 表示更改inode属性(如更改所有者,权限或者链接)的时间大于或小于n天的文件。
-mtime +n/-n 表示创建或者修改文件时间大于或小于n天的文件,该参数用得最多
-1 小于1天;1天以内
+1 大于1天;1天以前
常用
不能等于1
例子:
- 查找1天内创建或修改的后缀名为.conf的文件
[root@linux-151 ~]# find /etc/ -type f -mtime -1 -name "*.conf"
/etc/resolv.conf
- 查找root目录下1天内创建或修改的文件
[root@linux-151 ~]# find /root/ -type f -mtime -1/root/.bash_history
/root/321.txt
- 查找root目录下10分钟内创建的文件
[root@linux-151 ~]# find /root/ -type f -mmin -10
/root/11.txt
- 查找root目录下10分钟创建或修改的文件,并且显示出具体修改时间;
[root@linux-151 ~]# find /root/ -type f -mmin -10 -exec ls -l {} \;
-rw-r--r-- 1 root root 0 3月 30 22:03 /root/11.txt
tips:{}表示前面列出来的文件,\脱意符号,;换行符
- 查找目录/root/下1天内修改的文件,并且备份一份。
[root@linux-151 ~]# find /root/ -type f -mtime -1 -exec cp {} {}.bak \;
[root@linux-151 ~]# ls -al /root/
总用量 84
dr-xr-x---. 4 root root 288 2月 27 22:13 .
dr-xr-xr-x. 17 root root 265 2月 19 15:07 ..
-rw-r--r-- 1 root root 0 2月 27 22:03 11.txt-rw-r--r-- 1 root root 0 2月 27 22:13 11.txt.bak
lrwxrwxrwx 1 root root 9 2月 27 01:12 12 -> /tmp/111/
drwxr-xr-x 2 root root 6 2月 27 22:03 123
-rw-------. 2 root root 0 2月 17 03:40 1.log-rw-r--r-- 1 root root 16816 2月 27 01:31 321.txt-rw-r--r-- 1 root root 16816 2月 27 22:13 321.txt.bak-rw-------. 1 root root 1422 2月 17 03:51 anaconda-ks.cfg-rw-------. 1 root root 6339 2月 27 01:40 .bash_history-rw------- 1 root root 6339 2月 27 22:13 .bash_history.bak-rw-r--r--. 1 root root 18 3月 29 2018 .bash_logout-rw-r--r--. 1 root root 176 3月 29 2018 .bash_profile-rw-r--r--. 1 root root 176 3月 29 2018 .bashrc-rw-r--r--. 1 root root 100 3月 29 2018 .cshrc-rw------- 1 root root 41 2月 25 00:48 .lesshst
drwx------. 2 root root 80 2月 19 17:04 .ssh
-rw-r--r--. 1 root root 129 3月 29 2018 .tcshrc
- 查看root目录下小于6k的文件,并且列出它具体的大小
[root@linux-151 ~]# find /root/ -type f -size -6k -exec ls -lh {} \;
-rw-r--r--. 1 root root 18 3月 30 2018 /root/.bash_logout-rw-r--r--. 1 root root 176 3月 30 2018 /root/.bash_profile-rw-r--r--. 1 root root 176 3月 30 2018 /root/.bashrc-rw-r--r--. 1 root root 100 3月 30 2018 /root/.cshrc-rw-r--r--. 1 root root 129 3月 30 2018 /root/.tcshrc-rw-------. 1 root root 1.4K 2月 17 03:51 /root/anaconda-ks.cfg-rw-r--r--. 1 root root 806 2月 18 19:41 /root/.ssh/authorized_keys-rw-r--r-- 1 root root 176 2月 19 16:24 /root/.ssh/known_hosts-rw------- 1 root root 1.7K 2月 19 17:04 /root/.ssh/id_rsa-rw-r--r-- 1 root root 396 2月 19 17:04 /root/.ssh/id_rsa.pub-rw------- 1 root root 41 2月 25 00:48 /root/.lesshst-rw-------. 2 root root 0 2月 17 03:40 /root/1.log-rw-r--r-- 1 root root 0 2月 27 22:03 /root/11.txt-rw-r--r-- 1 root root 0 2月 27 22:13 /root/11.txt.bak
-
通过inode来查找一个文件的硬链接;
[root@linux-151 ~]# ls -i 1.log8388681 1.log
[root@linux-151 ~]# find -inum 8388681
./1.log
[root@linux-151 ~]# find / -inum 8388681
/root/1.log
/tmp/yum.log
先ls -l 查看 文件位有2 说明2个相同INODE号,然后ls -i 文件 查看号最后find / -inum 8388681 找出来
二、 文件名后缀
stat命令可以列出文件的atime,mtime,ctime;
[root@linux-128 ~]# stat 11.txt
文件:"11.txt"
大小:0 块:0 IO 块:4096 普通空文件
设备:803h/2051d Inode:16799023 硬链接:1
权限:(0644/-rw-r--r--) Uid:( 0/ root) Gid:( 0/ root)
最近访问:2018-03-30 22:13:47.084544630 +0800 //atime
最近更改:2018-03-30 22:03:41.825691647 +0800 //mtime
最近改动:2018-03-30 22:03:41.825691647 +0800 //ctime
创建时间:-
注:linux文件后缀名 在linux系统中,文件的后缀名没有具体的意义,加或者不加都无所谓。
Access: 2015-03-26 17:41:11.318175271 +0800 最近访问
Modify: 2015-03-26 17:40:05.876175331 +0800 更改文件内容时间
Change: 2015-03-26 17:40:05.876175331 +0800 更改iode时间
atime,最近访问 即文件的最近的一次访问时间,+n意思为查找n天以前的文件,-n为查找n天以内的文件
mtime,即文件数据最新的修改时间,指的就是文件内容的最新修改时间,注意和-ctime区分;
ctime; 文件状态最新改变的时间。文件有一些个基本的属性,权限,用户,组,大小,修改时间等,只要是这些信息变化了,那么ctime都会发生变化
find /etc/ -type f -o -mtime +1 -o -name "*.conf"
-1 小于1天;1天以内
+1 大于1天;1天以前
查找 /etc 指定文件类型 或者 文件修改时间 大于1天 或者名字是.*conf 结尾
find /root/ -type f -mmin -60 小知识
一小时内修改的文件
find /tmp/ -type f -mmin -60 -exec ls -l {} \;
-rw-r--r--. 1 root root 6 7月 31 15:34 /tmp/1.txt
-rw-r--r--. 1 root root 13 7月 31 15:38 /tmp/2.txt
-rw-r--r--. 1 root root 16 7月 31 15:38 /tmp/3.txt
查找tmp目录下 文件 60分钟内的文件,而且要显示出来 {} /tmp/1.txt /tmp/2.txt /tmp/3.txt 都包括里\托意 的意思
find /tmp/ -type f -mmin -150 -exec mv {} {}.bak \;
找150分内修改的文件并且改名. 后缀
find /root/ -type f -size -10k -exec ls -lh {} \; M兆
找root下 文件 小于 10K 并且 显示出来
四剑客之Find工具实战,Find工具主要用于操作系统文件、目录的查找,其语法参数格式为:
find path -option [ -print ] [ -exec -ok command ] { } /;
pathname: find命令所查找的目录路径。例如用.来表示当前目录,用/来表示系统根目
录。
print:
find命令将匹配的文件输出到标准输出。
exec:
find命令对匹配的文件执行该参数所给出的shell命令。相应命令的形式
为'command' { } \;,注意{ }和\;之间的空格。
ok:
和exec
的作用相同,只不过以一种更为安全的模式来执行该参数所给出的shell命
令,在执行每一个命令之前,都会给出提示,让用户来确定是否执行。
将查找到的文件输出到标准输出
#exec
command {} \; —–将查到的文件执行command操作,{} 和 \;之间有
空格
option 选项:
name
filename #查找名为filename的文件
perm
#按执行权限来查找
user
username #按文件属主来查找
group
groupname #按组来查找
mtime
n
+n #按文件更改时间来查找文件,n
指n天以内,+n指n天以前
atime
n
+n #按文件访问时间来查GIN: 0px">
ctime
n
+n #按文件创建时间来查找文件,n
指n天以内,+n指n天以前
nogroup
#查无有效属组的文件,即文件的属组在/etc/groups中不存在
nouser
#查无有效属主的文件,即文件的属主在/etc/passwd中不存
newer
f1 !f2 找文件,n
指n天以内,+n指n天以前
ctime
n
+n #按文件创建时间来查找文件,n
指n天以内,+n指n天以前
nogroup
#查无有效属组的文件,即文件的属组在/etc/groups中不存在
nouser
#查无有效属主的文件,即文件的属主在/etc/passwd中不存
newer
f1 !f2 #查更改时间比f1新但比f2旧的文件
type
b/d/c/p/l/f #查是块设备、目录、字符设备、管道、符号链接、普通文件
size
n[c] #查长度为n块[或n字节]的文件
depth
#使查找在进入子目录前先行查找完本目录
fstype
#查更改时间比f1新但比f2旧的文件
type
b/d/c/p/l/f #查是块设备、目录、字符设备、管道、符号链接、普通文件
size
n[c] #查长度为n块[或n字节]的文件
depth
#使查找在进入子目录前先行查找完本目录
fstype
#查位于某一类型文件系统中的文件,这些文件系统类型通常可
在/etc/fstab中找到
mount
#查文件时不跨越文件系统mount点
follow
#如果遇到符号链接文件,就跟踪链接所指的文件
cpio
%; #查位于某一类型文件系统中的文件,这些文件系统类型通常可
在/etc/fstab中找到
mount
#查文件时不跨越文件系统mount点
follow
#如果遇到符号链接文件,就跟踪链接所指的文件
cpio
#对匹配的文件使用cpio命令,将他们备份到磁带设备中
prune
#忽略某个目录
(1)Find工具-name参数案列:
find /data/ -name "*.txt" #查找/data/目录以.txt结尾的文件;
find /data/ -name "[A-Z]*" #查找/data/目录以大写字母开头的文件;
find /data/ -name "test*" #查找/data/目录以test开头的文件;
(2)Find工具-type参数案列:
find /data/ -type d #查找/data/目录下的文件夹;
find /data/ ! -type d #查找/data/目录下的非文件夹;
find /data/ -type l #查找/data/目录下的链接文件。
find /data/ -type d|xargs chmod 755 -R #查目录类型并将权限设置为755;
find /data/ -type f|xargs chmod 644 -R #查文件类型并将权限设置为644;
(3)Find工具-size参数案列:
find /data/ -size +1M #查文件大小大于1Mb的文件;
find /data/ -size 10M #查文件大小为10M的文件;
find /data/ -size -1M #查文件大小小于1Mb的文件;
(4)Find工具-perm参数案列:
find /data/ -perm 755 #查找/data/目录权限为755的文件或者目录;
find /data/ -perm -007 #与-perm 777相同,表示所有权限;
find /data/ -perm +644 #文件权限在644以上;
(5)Find工具-mtime参数案列:
atime,access time 文件被读取或者执行的时间;
ctime,change time 文件状态改变时间;
mtime,modify time 文件内容被修改的时间;
find /data/ -mtime +30 -name "*.log" #查找30天以前的log文件;
find /data/ -mtime +30 -name "*.log" #查找30天以前的log文件;
find /data/ -mtime 30 -name "*.txt"#查找第30天的log文件;
find /data/ -mmin +30-name "*.log" #查找30分钟以前修改的log文件;
find /data/ -
-30 -name "*.txt" #查找30分钟以内被访问的log文件;
find /data/ -cmin 30 -name "*.txt"#查找第30分钟改变的log文件。
(6)Find工具参数综合案列:
#查找/data目录以.log结尾,文件大于10k的文件,同时cp到/tmp目录;
find /data/ -name "*.log" –type f -size +10k -exec cp {} /tmp/ /;
#查找/data目录以.txt结尾,文件大于10k的文件,权限为644并删除该文件;
find /data/ -name "*.log" –type f -size +10k -m perm 644 -exec rm –rf {} /;
#查找/data目录以.log结尾,30天以前的文件,大小大于10M并移动到/tmp目录;
find /data/ -name "*.log" –type f -mtime +30 –size +10M -exec mv {} /tmp/ /;
后续继续补充