Linux相关练习及find用法

一.练习

1.定义一个对所有用户都生效的命令别名

-bash: la: command not found
[root@grub6 ~]# vim /etc/bashrc
添加 alias la='ls -a'
[root@grub6 ~]# source  /etc/bashrc
[root@grub6 ~]# la
.                .bash_history  .bashrc  .gconf      install.log           .pki     .viminfo
..               .bash_logout   .config  grub1       install.log.syslog    .tcshrc  .Xauthority
anaconda-ks.cfg  .bash_profile  .cshrc   ifcfg-eth0  linux-3.10.67.tar.xz  test

2.显示/etc/passwd文件中不以/bin/bash结尾的行

[root@yuki ~]# grep -v "/bin/bash$" /etc/passwd

3.找出/etc/passwd文件中,包含二位数字或者三位数的行

1.[root@yuki ~]# grep -E  "\<[[:digit:]]{2,3}\>" /etc/passwd
2.[root@yuki ~]# grep -E "[0-9][0-9]+[0-9]?" /etc/passwd

4.显示出/proc/meminfo文件中以大写或小写S开头的行,三种方法

1.[root@yuki ~]# grep "^[sS]" /proc/meminfo
2.[root@yuki ~]# grep -i "^s" /proc/meminfo
3.[root@yuki ~]# grep -E "^(s|S)" /proc/meminfo

5.使用echo输出一个绝对路径,使用egrep取出路径名

1.[root@yuki ~]# echo "/etc/rc.d/rc3.d/S10network" | grep -Eo ".*/"
/etc/rc.d/rc3.d/
2.echo "/etc/rc.d/rc3.d/S10network" | sed -r 's@[^/]+$@@'
/etc/rc.d/rc3.d/

6.找出ifconfig中的ip地址

1.[root@yuki ~]# ifconfig ens33 | grep -o "inet [^[:space:]]\+" | cut -d" " -f2
192.168.3.100
2.[root@yuki ~]# ifconfig ens33  | sed  -rn '/^[[:space:]]+inet /p' | sed 's@^.*inet@@' | sed 's@netmask.*@@'
 192.168.3.100  

7.vim定制自动缩进四个字符

[root@yuki ~]# echo "set tabstop=4" >> /etc/vimrc

8.编写脚本,实现自动添加三个用户,并计算其uid之和

#!/bin/bash
#Calculation uid sum


sum=0

for i in {1..3};do
    user=user$i
    useradd $user &> /dev/null
    idnumber=$(id -u $user) &> /dev/null
    sum=$[$sum+$idnumber]
done

echo "uid sum: $sum"

二.find命令

1.find命令

1.find命令:实时查找工具,通过遍历指定路径下的文件系统完成文件查找
ps:其特点为,精确查找,精准查找,速度慢

2.语法:find [OPTIONS] [查找起始路径] [查找条件] [处理动作]
1)查找起始路径:指定具体搜索目标起始路径,默认为当前目录

2)查找条件:根据文件名,大小,类型,从属关系,权限等标准进行

3)处理动作:对符合查找条件的文件作出操作,默认为标准输出

2.查找条件

2.1查找条件

1.查找指定名称文件:- name “文件名称”

[root@yuki ~]# find ./ -name repo
./repo

ps:-iname "文件名称"为不区分字符大小写

2.根据文件的属主权限查找:-user username

[root@yuki ~]# find . -user yuki
./repo

3.根据文件的属组权限查找:-group groupname

[root@yuki ~]# find . -group yuki
./repo

4.根据UID进行查找:-uid UID

[root@yuki ~]# find . -uid 1000
./repo

5.根据GID进行查找:-gid GID

[root@yuki ~]# find . -gid 1000
./repo

6.查找没有属主权限的文件:-nouser

[root@yuki ~]# id user1
uid=1002(user1) gid=1003(user1)=1003(user1)
[root@yuki ~]# su - user1 -c 'touch test'
[root@yuki ~]# mv /home/user1/test .
[root@yuki ~]# ll test
-rw-rw-r--. 1 user1 user1        0 5月  26 22:30 test
[root@yuki ~]# userdel -r user1
[root@yuki ~]# ll test
-rw-rw-r--. 1 1002 1003 0 5月  26 22:30 test
[root@yuki ~]# find . -nouser
./test

7.查找没有属组权限的文件:-nogroup

[root@yuki ~]# find . -nogroup
./test

8.基于正则表达式,匹配整个路径,非基名

[root@yuki ~]# find /root -regex  ".*/test$"
/root/test

2.2组合条件查找

1.同时满足:-a

2.满足一个即可:-o

3.条件取反:-not或者!

4.不是A也不是B

-not A -a -not B=-not\(A -o B\)

5.不是A或者不是B

-not A -o -not B=-not\(A -a B\)

ps:组合条件查找中,括号需要进行转义

6.练习:找出/tmp目录下属主为非root,文件名不包含fstab字符串的文件

[root@yuki ~]# find  /tmp -not \( -user root -o -iname "*fstab*" \)

2.3根据文件类型

1.普通文件:-type f

[root@yuki ~]# find . -type f

2.目录文件:-type d

3.符号链接文件:-type l

4.块设备文件:-type b

5.字符设备文件:-type c

6.管道文件:-type p

7.套接字文件:-type s

2.4根据文件大小

1.语法:-size [+|-]#UNIT(单位):常用单位k,M,G

2.查找等于#大小的文件:-size #

[root@yuki ~]# find . -size 5k
./.subversion/README.txt

2.查找大于#大小的文件:-size +#

[root@yuki ~]# find . -size +5M | xargs ls -lh
-rw-r--r--. 1 root root 70M 5月  26 01:16 ./linux-3.10.67.1.tar.xz

3.查找小于#大小的文件:-size -#

[root@yuki ~]# find . -size -5k

4.查找范围
1)-size #范围:# -1<x<=#
5k表示:4<x<=5

2)-size +#范围:x>#
+5k表示:x>5

3)-szie -#范围:x<# -1
-5k表示:x<4

2.5根据时间戳查找

1.根据天为单位:
1)-atime [+|-]#

1.#:#<=x<#+1
-atime 7表示:7<=x<8天内访问过的文件
2.+#:#>=#+1
-atime +7表示:x>=8天内访问过的文件
3.-#:x<#
-atime -7表示:x<7内访问过的文件

2)-mtime:同理

3)-ctime:同理

2.根据分钟为单位
1)-amin

2)-mmin

3)-cmin

2.6根据权限查找

1.语法:-perm [/|-]mode

2.精确匹配:-perm mode

[root@yuki ~]# touch mode
[root@yuki ~]# chmod 542 mode
[root@yuki ~]# find . -perm 542
./mode

2.任何一类用户(u,g,o)中的任何一个权限((r,w,x))满足即可:-perm /mode

1.文件权限为222,目标权限为222
[root@yuki ~]# chmod 222 mode
[root@yuki ~]# find . -perm /222 | grep mode
./mode
2.文件权限为211,目标权限为222
[root@yuki ~]# chmod 211 mode
[root@yuki ~]# find . -perm /222 | grep mode
./mode
3.文件权限为600,目标权限为755
[root@yuki ~]# chmod 600 mode
[root@yuki ~]# find . -perm /755 | grep mode
./mode
即-perm  /mode的匹配关系为:文件权限<=目标权限

3.每一类用户中的每一位权限同时符合才满足条件:-perm -mode

1.文件权限为4755,目标权限为0744
[root@yuki ~]# chmod 4755 mode
[root@yuki ~]# find . -perm -0744 | grep mode
./mode
2.文件权限为766,目标权限为522
[root@yuki ~]# chmod 766 mode 
[root@yuki ~]# find . -perm -522 | grep mode
./mode
即-perm  -mode的匹配关系为:文件权限>=目标权限

3.处理动作

1.-print:输出至标准输出,其为默认动作

2.-ls:类似于对查找到的文件执行“ls -l”命令,输出文件的详细信息

[root@yuki ~]# find -name "*ber" -ls
51092186    0 -rw-r--r--   1 root     root            0 5月 26 23:31 ./test1.number
53770734    0 -rw-r--r--   1 root     root            0 5月 26 23:31 ./test2.number
53777555    0 -rw-r--r--   1 root     root            0 5月 26 23:31 ./test3.number

3.-delete:删除查找到的文件;

4.-fls /PATH/TO/SOMEFILE:把查找到的所有文件的长格式信息保存至指定文件中

[root@yuki ~]# find -name "*ber" -fls number
[root@yuki ~]# cat number
51092186    0 -rw-r--r--   1 root     root            0 5月 26 23:31 ./test1.number
53770734    0 -rw-r--r--   1 root     root            0 5月 26 23:31 ./test2.number
53777555    0 -rw-r--r--   1 root     root            0 5月 26 23:31 ./test3.number
53777557    0 -rw-r--r--   1 root     root            0 5月 26 23:36 ./number

5.-ok COMMAND {} \; :对查找到的每个文件执行由COMMAND表示的命令;每次操作都由用户进行确认,{}表示引用前面查找到的所有内容

6.-exec COMMAND {} \; :对查找到的每个文件执行由COMMAND表示的命令;

[root@yuki ~]# touch test1 test2 test3
[root@yuki ~]# find . -name "test[0-9]"
./test1
./test2
./test3
[root@yuki ~]# find . -name "test[0-9]" -exec mv {} {}.number \;
[root@yuki ~]# ll *ber
-rw-r--r--. 1 root root 0 5月  26 23:31 test1.number
-rw-r--r--. 1 root root 0 5月  26 23:31 test2.number
-rw-r--r--. 1 root root 0 5月  26 23:31 test3.number

7.find:一次性查找符合条件的所有文件,并一同传送给后面的命令,但有些命令不能接受过长的参数;此时使用:find 表达式 | xargs COMMAND

8.注意:

1.find /tmp -nouser -o -nogroup -ls  "仅表示-ls -nogroup"
2.find /tmp \(-nouser -o -nogroup \) -ls "表示-ls -nouser -o -nogroup"

###  3.1xargs命令
1.xargs命令:xargs命令是给其他命令传递参数的一个过滤器,也是组合多个命令的一个工具。

1.-i选项:使用-i指定一个替换字符串{ },这个字符串在xargs扩展时会被替换掉,当-i与xargs结合使用,每一个参数命令都会被执行一次
```bash
[root@yuki ~]# find -name "*ber"
./test1.number
./test2.number
./test3.number
[root@yuki ~]# find -name "*ber" | xargs -i -t mv {} {}.yuki
mv ./test1.number ./test1.number.yuki 
mv ./test2.number ./test2.number.yuki 
mv ./test3.number ./test3.number.yuki 
[root@yuki ~]# find -name "*yuki"
./test2.number.yuki
./test1.number.yuki
./test3.number.yuki

ps:-t选项为显示过程

4.练习

1.查找/var目录下属主为root且属组为mail的所有文件或目录

find /var -user root -a -group mail

2.查找/etc目录下最近一周内其内容修改过,且属主不属于root且不属于hadoop的文件或目录

find /etc -ctime -7 -a -not \( -user root -o -user hadoop \)

3.查找/etc目录下大于1M且类型为普通文件的文件

find /etc -size +1M  -a -type f

4.查找/etc目录下所有用户都没有写权限的文件

find /etc -not -perm /222

5.查找/etc目录下至少有一类用户没有写权限

find /etc -not -perm -222

6.查找/etc/init.d目录下,所有用户都有执行权限且其他用户有写权限的文件

find /etc/init.d -perm -113
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值