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)
上一篇 2017-08-04 09:22
下一篇 2017-08-04 11:08

相关推荐

  • net25 第15周作业

    1、总结sed和awk的详细用法; SED sed模式空间 默认不编辑源文件,仅对模式空间中的数据做处理:而后,处理结束后,将模式空间打印屏幕 sed [options]’address+command’ file… -n:静默模式,不再默认显示模式空间的内容 -i:直接修改原文件 -e script -e script 同时执行多个脚本 -f /pat…

    Linux干货 2017-05-15
  • 马哥linux0728课程内容

    课堂学习内容   -ahistory list –.bash_histroy 保存历史列表到历史文件 -cclear history list 清空历史列表 -d deletehistory entru 清除历史列表某一条命令 -rread .bash_history 读历史文件中的命令到历史列表…

    Linux干货 2016-08-04
  • 马哥教育网络班22期+第十周课程练习

    1、请详细描述CentOS系统的启动流程(详细到每个过程系统做了哪些事情)    CentOS系统启动流程为:POST –> BootSequence(BIOS) –> BootLoader(MBR) –> Kernel(ramdisk) –> r…

    Linux干货 2016-12-13
  • Linux RPM 命令参数使用详解

    rpm 执行安装包: 二进制包(Binary)以及源代码包(Source)两种。二进制包可以直接安装在计算机中,而源代码包将会由 RPM自动编译、安装。源代码包经常以src.rpm作为后缀名。 -ivh # 安装显示安装进度–install–verbose–hash。 使用方法如下:   &n…

    2017-04-18
  • 八大排序算法

    概述 排序有内部排序和外部排序,内部排序是数据记录在内存中进行排序,而外部排序是因排序的数据很大,一次不能容纳全部的排序记录,在排序过程中需要访问外存。 我们这里说说八大排序就是内部排序。     当n较大,则应采用时间复杂度为O(nlog2n)的排序方法:快速排序、堆排序或归并排序序。    快速…

    Linux干货 2015-04-07
  • find总结及练习

    文件查找: 在文件系统上查找符合条件的文件; 文件查找:locate, find非实时查找(数据库查找):locate 实时查找:find locate: 依赖于事先构建的索引;索引的构建是在系统较为空闲时自动进行(周期性任务);手动更新数据库(updatedb); 索引构建过程需要遍历整个根文件系统,极消耗资源; 工作特点: 查找速度快; 模糊查…

    Linux干货 2016-07-22