grep,find用法-2

1、显示当前系统上root、fedora或user1用户的默认shell;

grep -E “^(root|fedora|user1)>” /etc/passwd | cut -d: -f1,7
[root@bogon Desktop]# grep -E "^(root|fedora|user1)\>" /etc/passwd | cut -d: -f1,7
root:/bin/bash
fedora:/bin/bash
user1:/bin/bash

2、找出/etc/rc.d/init.d/functions文件中某单词后面跟一组小括号的行,形如:hello();

grep “[[:alpha:]]+()” /etc/rc.d/init.d/functions
[root@bogon Desktop]# grep "[[:alpha:]]+()" /etc/rc.d/init.d/functions
checkpid() {
__pids_var_run() {
__pids_pidof() {
daemon() {
killproc() {
pidfileofproc() {
pidofproc() {
status() {
echo_success() {
echo_failure() {
echo_passed() {
echo_warning() {
update_boot_stage() {
success() {
failure() {
passed() {
warning() {
action() {
strstr() {
is_ignored_file() {
is_true() {
is_false() {
apply_sysctl() {

3、使用echo命令输出一个绝对路径,使用grep取出其基名;扩展:取出其路径名;

echo /etc/passwd | grep -o “[^/]+/\?$” passwd
[root@bogon ~]# echo /etc/passwd | grep -o "[^/]\+/\?$"
passwd
[root@bogon ~]#
echo /etc/passwd | grep -oP ‘/.*/(?=.?\b)’
[root@bogon ~]# echo /etc/passwd | grep -oP '/.*/(?=.?\b)'
/etc/
[root@bogon ~]#

4、找出ifconfig命令结果中的1-255之间数字;

ifconfig | grep -E ‘\<([0-9]|([0-9][0-9])|(1[0-9][0-9])|(2[1-4][0-9])|(25[1-5]))>’
[root@bogon ~]# ifconfig | grep -E '\<([0-9]|([0-9][0-9])|(1[0-9][0-9])|(2[1-4][0-9])|(25[1-5]))\>'
        inet 192.168.101.15  netmask 255.255.255.0  broadcast 192.168.101.255
        inet6 fe80::20c:29ff:fe00:b610  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:00:b6:10  txqueuelen 1000  (Ethernet)
        RX packets 90557  bytes 114145934 (108.8 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 32344  bytes 15212308 (14.5 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 0  (Local Loopback)
        RX packets 4  bytes 340 (340.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 4  bytes 340 (340.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
        inet 192.168.122.1  netmask 255.255.255.0  broadcast 192.168.122.255
        ether 00:00:00:00:00:00  txqueuelen 0  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
[root@bogon ~]#

5、挑战题:写一个模式,能匹配合理的IP地址;

ifconfig | grep -o -E “(([0-9]|[0-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5]).){3}([0-9]|[0-9][0-9]|1[0-9])”
[root@bogon ~]# ifconfig | grep -o -E "(([0-9]|[0-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[0-9][0-9]|1[0-9])"
192.168.101.15
255.255.255.0
192.168.101.25
127.0.0.1
255.0.0.0
192.168.122.1
255.255.255.0
192.168.122.25
[root@bogon ~]#

6、挑战题:写一个模式,能匹配出所有的邮件地址;

grep -E “([[:alph^C]]|[0-9]|[[:punct:]]*|.|\@)”

7、查找/var目录下属主为root,且属组为mail的所有文件或目录;

find /var/ -user root -group mail -ls
[root@bogon ~]# find /var/ -user root -group mail -ls
100664302    4 drwxrwxr-x   2 root     mail         4096 Aug  2 22:57 /var/spool/mail
[root@bogon ~]#

8、查找当前系统上没有属主或属组的文件;

find / -nouser -nogroup -ls
[root@bogon ~]# find / -nouser -nogroup -ls
find: ‘/proc/11927/task/11927/fd/6’: No such file or directory
find: ‘/proc/11927/task/11927/fdinfo/6’: No such file or directory
find: ‘/proc/11927/fd/6’: No such file or directory
find: ‘/proc/11927/fdinfo/6’: No such file or directory
find: ‘/run/user/1000/gvfs’: Permission denied
   137    0 drwxr-xr-x   2 2006     2006            6 Aug  3 20:44 /root/abc
35461830    0 drwxr-xr-x   2 2006     2006            6 Aug  3 20:44 /root/asdf
68898945    0 drwx------   3 2006     2006           74 Aug  3 20:44 /home/qqq
102702695    0 drwxr-xr-x   4 2006     2006           37 Jul 11 07:27 /home/qqq/.mozilla
1622887    0 drwxr-xr-x   2 2006     2006            6 Jun  9  2014 /home/qqq/.mozilla/extensions
35461829    0 drwxr-xr-x   2 2006     2006            6 Jun  9  2014 /home/qqq/.mozilla/plugins
68898946    4 -rw-r--r--   1 2006     2006           18 Nov 20  2015 /home/qqq/.bash_logout
68898949    4 -rw-r--r--   1 2006     2006          193 Nov 20  2015 /home/qqq/.bash_profile
68898950    4 -rw-r--r--   1 2006     2006          231 Nov 20  2015 /home/qqq/.bashrc
[root@bogon ~]#

9、查找/etc目录下所有用户都有写权限的文件;

find /etc -prem -222 -ls
[root@bogon ~]# find /etc/ -perm -222 -ls
33790768    0 lrwxrwxrwx   1 root     root           10 Jul 11 07:27 /etc/rc6.d -> rc.d/rc6.d
100801421    0 lrwxrwxrwx   1 root     root           16 Jul 11 07:27 /etc/ssl/certs -> ../pki/tls/certs
36511098    0 lrwxrwxrwx   1 root     root           38 Jul 11 07:36 /etc/localtime -> ../usr/share/zoneinfo/America/New_York
33902848    0 lrwxrwxrwx   1 root     root           56 Jul 11 07:27 /etc/favicon.png -> /usr/share/icons/hicolor/16x16/apps/fedora-logo-icon.png
34570567    0 lrwxrwxrwx   1 root     root           25 Jul 11 07:32 /etc/pam.d/smtp -> /etc/alternatives/mta-pam
33904308    0 lrwxrwxrwx   1 root     root           14 Jul 11 07:36 /etc/pam.d/system-auth -> system-auth-ac
33904306    0 lrwxrwxrwx   1 root     root           12 Jul 11 07:36 /etc/pam.d/postlogin -> postlogin-ac
33904305    0 lrwxrwxrwx   1 root     root           16 Jul 11 07:36 /etc/pam.d/password-auth -> password-auth-ac
33904303    0 lrwxrwxrwx   1 root     root           19 Jul 11 07:36 /etc/pam.d/fingerprint-auth -> fingerprint-auth-ac
33904307    0 lrwxrwxrwx   1 root     root           17 Jul 11 07:36 /etc/pam.d/smartcard-auth -> smartcard-auth-ac
1660802    0 lrwxrwxrwx   1 root     root           14 Jul 11 07:29 /etc/libvirt/qemu/networks/autostart/default.xml -> ../default.xml
34181389    0 lrwxrwxrwx   1 root     root           19 Aug  1 21:15 /etc/httpd/logs -> ../../var/log/httpd
34181390    0 lrwxrwxrwx   1 root     root           29 Aug  1 21:15 /etc/httpd/modules -> ../../usr/lib64/httpd/modules
34181391    0 lrwxrwxrwx   1 root     root           10 Aug  1 21:15 /etc/httpd/run -> /run/httpd
101468179    0 lrwxrwxrwx   1 root     root           35 Jul 11 07:32 /etc/cifs-utils/idmap-plugin -> /etc/alternatives/cifs-idmap-plugin
71753217    0 lrwxrwxrwx   1 root     root           21 Jul 11 07:31 /etc/gdm/Xsession -> ../X11/xinit/Xsession
33556151    0 lrwxrwxrwx   1 root     root           22 Jul 11 07:32 /etc/grub2.cfg -> ../boot/grub2/grub.cfg
36134996    0 lrwxrwxrwx   1 root     root            8 Jul 11 07:32 /etc/scl/conf -> prefixes

10、查找/etc目录下大于1M,且类型为普通文件的所有文件;

find /etc -size +1M -ls
[root@bogon ~]# find /etc/ -size +1M -ls
72502453 3772 -rw-r--r--   1 root     root      3858924 Nov 20  2015 /etc/selinux/targeted/policy/policy.29
36328392 6852 -r--r--r--   1 root     root      7014922 Jul 11 07:37 /etc/udev/hwdb.bin
101554968 1336 -rw-r--r--   1 root     root      1367395 Mar  5  2015 /etc/brltty/zh-tw.ctb
[root@bogon ~]#

原创文章,作者:N27_Danry,如若转载,请注明出处:http://www.178linux.com/83620

(0)
N27_DanryN27_Danry
上一篇 2017-08-04
下一篇 2017-08-04

相关推荐

  • CentOS7下重置root密码

    CentOS7下重置root密码          Linux系统、UNIX系统和其他类UNIX系统中,存在唯一的超级用户root。普通用户密码忘掉可以用root用户重置,但是一旦root密码忘掉,事情就复杂起来了。本文主要介绍root密码忘掉之后,重置密码的过程。   &nbs…

    Linux干货 2017-03-30
  • 集中练习6-bash脚本

    集中练习6-bash脚本

    Linux干货 2017-12-05
  • DNS服务器搭建示例

    DNS服务器搭建示例 负责解析magedu.com域名,能够对一些主机名进行正向解析和逆向解析 配置主配置文件 [root@slave1 etc]# vim /etc/named.conf options { listen-on port 53 { 192.168.91.132; }; // listen-on-v6 port 53 { ::1; }; di…

    2017-09-16
  • tab键 history 命令的相关巧用

    Tab键的妙用       1.command没有完全输入时: 点击tab,如果能唯一匹配到命令,则直接补全,否则需要点击两下tab来显示能够匹配到的命令。                2./2tab : 显…

    2017-02-18
  • 优质代码的十诫

    1.- DRY: Don’t repeat yourself. DRY 是一个最简单的法则,也是最容易被理解的。但它也可能是最难被应用的(因为要做到这样,我们需要在泛型设计上做相当的努力,这并不是一件容易的事)。它意味着,当我们在两个或多个地方的时候发现一些相似的代码的时候,我们需要把他们的共性抽象出来形一个唯一的新方法,并且改变现有的地方的代码让…

    Linux干货 2015-04-01
  • 马哥linux0803作业内容

    1. 创建sysadmins组 将用户user1,user2,user3加入sysadmins组中 将user3设置为sysadmins的管理员 用user3登录,将user2从组中移除 设置sysadmins的密码centos 设置user1 在创建新文件时,文件的所属组为sysadmins 删除user1…3 删除sysadmins 2、三种权限rwx对…

    Linux干货 2016-08-08