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

相关推荐

  • 进程管理常用命令

    Linux系统上的进程查看及管理工具:pstree,ps,pidof,pgrep,top,htop,glances,pmap,vmstat,dstat,kill,pkill,job,bg,fg,nohup,nice,renice,killall,…CentOS 5 :SysV initCentOS 6 :upstartCentOS 7 :syst…

    Linux干货 2017-05-17
  • 手动自制Mini Linux

        linux系统内核非常的精简,而且基于一切皆文件的思想,使得我们可以再现有系统上挂载一个空硬盘,利用现有系统编译一个内核和相关程序文件并拷贝到空硬盘上。我们就可以用空硬盘来单独运行一个精简的linux系统,这对于依赖于注册表的windows系统来说是不可能实现的。本文简要介绍一下一个mini linux的安装制…

    Linux干货 2016-05-03
  • centos7或centos6中如何编译安装源码程序包

    yum仓库配置、程序的编译安装

    2017-12-02
  • 第二周:文件管理练习

    1.Linux上的文件管理类命令都有哪些,其常用的使用方法及其相关示例演示 目录管理管理类命令 mkdir make directories mkdir [OPTION]… DIRECTORY… 注意:路径基名作为命令的作用对象时,基名之前的路径必须要存在;     -p:自动按需创建父目录…

    Linux干货 2016-09-23
  • 数据结构-栈和队列

    1.栈 1.1 栈的定义 栈是一种特殊的线性表。其特殊性在于限定插入和删除数据元素的操作只能在线性表的一端进行。如下所示: 结论:后进先出(Last In First Out),简称为LIFO线性表。 栈的基本运算有六种: 构造空栈:InitStack(S)、 判栈空: StackEmpty(S)、 判栈满: StackFull(S)、 …

    Linux干货 2015-04-07
  • LVS介绍及工作原理图解

    一、LVS简介 LVS是Linux Virtual Server的简称,也就是Linux虚拟服务器,是一个由章文嵩博士发起的自由软件项目,官方站点是:http://www.linuxvirtualserver.org。现在LVS已经是Linux标准内核的一部分,在Linux2.4内核以前,使用LVS时必须重新编译内核以支持LVS功能模块,但是从Linux2.…

    2017-12-06