linux文件目录操作以及grep正则表达式使用

1、复制/etc/skel目录为/home/tuser1,要求/home/tuser1及其内部文件的属组和其它用户均没有任何访问权限。

    [root@www /]# cp -r /etc/skel /home/tuser1 && chmod 700 -R /home/tuser1  
    [root@www /]# echo $?
    0
    [root@www home]# ll -al /home/tuser1/
    总用量 12
    drwx------. 3 root root  74 11月 30 13:14 .
    drwxr-xr-x. 4 root root  30 11月 30 13:14 ..
    -rwx------. 1 root root  18 11月 30 13:14 .bash_logout
    -rwx------. 1 root root 193 11月 30 13:14 .bash_profile
    -rwx------. 1 root root 231 11月 30 13:14 .bashrc
    drwx------. 4 root root  37 11月 30 13:14 .mozilla

cp SRC DEST

  SRC是目录:

    此时使用选项:-r
        如果DEST不存在:则创建指定目录,复制SRC目录中所有文件至DEST中;
        如果DEST存在:
            如果DEST是文件:报错

            如果DEST是目录:


2、编辑/etc/group文件,添加组hadoop。

    [root@www /]#echo "hadoop:x:1001" >>/etc/group
    [root@www /]# cat /etc/group |grep hadoop
    hadoop:x:1001
    [root@www /]#

输出重定向:>
特性:覆盖输出
输出重定向:>>

特性:追加输出


3、手动编辑/etc/passwd文件新增一行,添加用户hadoop,其基本组ID为hadoop组的id号;其家目录为/home/hadoop。

    [root@www home]# echo "hadoop:x:1001:1001:hadoop:/home/hadoop:/bin/bash" >> /etc/passwd && tail -n 2 /etc/passwd
    user:x:1000:1000:user:/home/user:/bin/bash
    hadoop:x:1001:1001:hadoop:/home/hadoop:/bin/bash


4、复制/etc/skel目录为/home/hadoop,要求修改hadoop目录的属组和其它用户没有任何访问权限。

    [root@localhost ~]# cp -r /etc/skel /home/hadoop 
    [root@localhost ~]# chmod 700 -R /home/hadoop 
    [root@localhost ~]# ll -al /home/hadoop
    总用量 12
    drwx------. 3 root root  78 7月  15 19:38 .
    drwxr-xr-x. 5 root root  52 7月  15 19:38 ..
    -rwx------. 1 root root  18 7月  15 19:38 .bash_logout
    -rwx------. 1 root root 193 7月  15 19:38 .bash_profile
    -rwx------. 1 root root 231 7月  15 19:38 .bashrc
    drwx------. 4 root root  39 7月  15 19:38 .mozilla
    [root@localhost ~]# 


5、修改/home/hadoop目录及其内部所有文件的属主为hadoop,属组为hadoop。

    [root@localhost ~]# chown -R hadoop:hadoop /home/hadoop/
    [root@localhost ~]# ll -a /home/hadoop
    总用量 12
    d-wx------. 3 hadoop hadoop  78 7月  15 19:38 .
    drwxr-xr-x. 5 root   root    52 7月  15 19:38 ..
    -rwx------. 1 hadoop hadoop  18 7月  15 19:38 .bash_logout
    -rwx------. 1 hadoop hadoop 193 7月  15 19:38 .bash_profile
    -rwx------. 1 hadoop hadoop 231 7月  15 19:38 .bashrc
    drwx------. 4 hadoop hadoop  39 7月  15 19:38 .mozilla

grep option 选项

  •     --color=auto:对匹配到的文本着色后高亮显示;
  •     -i:ignorecase,忽略字符的大小写 
  •     -o:仅显示匹配到的字符串本身;
  •     -v, --invert-match:显示不能被模式匹配到的行;
  •     -E:支持使用扩展的正则表达式元字符;
  •     -q, --quiet, --silent:静默模式,即不输出任何信息;
  •     -A #:after, 后#行
  •     -B #:before,前#行
  •     -C #:context,前后各

6、显示/proc/meminfo文件中以大写或小写S开头的行;用两种方式;

    [root@localhost ~]# grep  ^[sS] /proc/meminfo
    SwapCached:            0 kB
    SwapTotal:       1048572 kB
    SwapFree:        1048572 kB
    Shmem:             19756 kB
    Slab:             134512 kB
    SReclaimable:      69476 kB
    SUnreclaim:        65036 kB
    [root@localhost ~]# grep  -i ^s /proc/meminfo
    SwapCached:            0 kB
    SwapTotal:       1048572 kB
    SwapFree:        1048572 kB
    Shmem:             19756 kB
    Slab:             134512 kB
    SReclaimable:      69476 kB
    SUnreclaim:        65036 kB


7、显示/etc/passwd文件中其默认shell为非/sbin/nologin的用户;

    [root@localhost ~]# grep -v "/sbin/nologin" /etc/passwd
    root:x:0:0:root:/root:/bin/bash
    sync:x:5:0:sync:/sbin:/bin/sync
    shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
    halt:x:7:0:halt:/sbin:/sbin/halt
    yedong1308:x:1000:1000:yedong1308:/home/yedong1308:/bin/bash
    hadoop:x:1001:1001:hadoop:/home/hadoop:/bin/bash


8、显示/etc/passwd文件中其默认shell为/bin/bash的用户

    [root@localhost ~]# grep  "bin/bash" /etc/passwd
    root:x:0:0:root:/root:/bin/bash
    yedong1308:x:1000:1000:yedong1308:/home/yedong1308:/bin/bash
    hadoop:x:1001:1001:hadoop:/home/hadoop:/bin/bash

    使用cut

    [root@localhost ~]# grep  "bin/bash" /etc/passwd | cut -d: -f1
    root
    yedong1308
    hadoop


9、找出/etc/passwd文件中的一位数或两位数;

    [root@localhost ~]# grep -E "\<[0-9]{1,2}\>"  /etc/passwd  扩展正则
    [root@localhost ~]# grep "\<[0-9]\{1,2\}\>"  /etc/passwd
    root:x:0:0:root:/root:/bin/bash
    bin:x:1:1:bin:/bin:/sbin/nologin
    daemon:x:2:2:daemon:/sbin:/sbin/nologin
    adm:x:3:4:adm:/var/adm:/sbin/nologin

\<:词首锚定;

\>:词尾锚定;

\{m,n\}:匹配其前面的字符至少m次,至多n次;

扩展表达式 -E

{m,n}:至少m次,至多n次;  


10、显示/boot/grub/grub.conf中以至少一个空白字符开头的行;

    [root@centos6 ~]# grep "^[[:space:]]\+" /boot/grub/grub.conf
        root (hd0,0)
        kernel /vmlinuz-2.6.32-642.3.1.el6.x86_64 ro root=/dev/mapper/vg_centos-lv_root rd_NO_LUKS rd_LVM_LV=vg_centos/lv_swap rd_NO_MD LANG=zh_CN.UTF-8 rd_LVM_LV=vg_centos/lv_root  KEYBOARDTYPE=pc KEYTABLE=us rd_NO_DM rhgb quiet crashkernel=auto
        initrd /initramfs-2.6.32-642.3.1.el6.x86_64.img
        root (hd0,0)
        kernel /vmlinuz-2.6.32-642.el6.x86_64 ro root=/dev/mapper/vg_centos-lv_root rd_NO_LUKS rd_LVM_LV=vg_centos/lv_swap rd_NO_MD LANG=zh_CN.UTF-8 rd_LVM_LV=vg_centos/lv_root  KEYBOARDTYPE=pc KEYTABLE=us rd_NO_DM rhgb quiet
        initrd /initramfs-2.6.32-642.el6.x86_64.img


11、显示/etc/rc.d/rc.sysinit文件中以#开头,后面跟至少一个空白字符,而后又有至少一个非空白字符的行;

    [root@localhost rc.d]# grep "^#[[:space:]]\+[^[:space:]]\+" /etc/rc.d/rc.local
    # THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES
    # It is highly advisable to create own systemd services or udev rules
    # to run scripts during boot instead of using this file.
    # In contrast to previous versions due to parallel execution during boot
    # this script will NOT be run after all other services.
    # Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure
    # that this script will be executed during boot.


12、找出"netstat -tan"命令的结果中以'LISTEN'后跟空白字符结尾的行;

    [root@localhost rc.d]# netstat -tan | grep "LISTEN[[:space:]]*$"
    tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN     
    tcp        0      0 10.10.10.1:53           0.0.0.0:*               LISTEN     
    tcp        0      0 192.168.122.1:53        0.0.0.0:*               LISTEN     
    tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN     
    tcp        0      0 127.0.0.1:631           0.0.0.0:*               LISTEN     
    tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN     
    tcp6       0      0 :::111                  :::*                    LISTEN     
    tcp6       0      0 :::22                   :::*                    LISTEN     
    tcp6       0      0 ::1:631                 :::*                    LISTEN     
    tcp6       0      0 ::1:25                  :::*                    LISTEN     

13、添加用户bash, testbash, basher, nologin (此一个用户的shell为/sbin/nologin),而后找出当前系统上其用户名和默认shell相同的用户的信息;

[root@localhost rc.d]# grep  "^\([[:alnum:]]\+\>\).*\1$" /etc/passwd
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
bash:x:1002:1002::/home/bash:/bin/bash
nologin:x:1005:1005::/home/nologin:/sbin/nologin

使用()需要用\(\)转义

\1:引用第一组左括号里面的内容


使用扩展正则表达式

[root@localhost rc.d]# grep -E "^([[:alnum:]]+\>).*\1$" /etc/passwd
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
bash:x:1002:1002::/home/bash:/bin/bash
nologin:x:1005:1005::/home/nologin:/sbin/nologin
[root@localhost rc.d]# 














                
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
回答: 在Linux中,grep命令可以用于在文件中搜索匹配某个正则表达式的文本。下面是一些关于grep正则表达式的常见用法: 1. 使用`.`表示任意一个字符。例如,`grep '.' a.txt`将匹配包含任意一个字符的字符串。\[1\] 2. 使用`|`表示逻辑或操作符。例如,`grep 'fatal\|error\|critical' /var/log/nginx/error.log`将匹配包含单词"fatal"、"error"或"critical"的行。\[2\] 3. 使用`*`表示零次或多次匹配前面的字符或子表达式。例如,`grep 'se*' a.txt`将匹配包含以"s"开头,后面跟着零个或多个"e"的字符串,如"s"、"se"、"see"等。\[3\] 这些是grep正则表达式的一些基本用法,你可以根据具体的需求进行进一步的学习和使用。 #### 引用[.reference_title] - *1* *3* [Linux grep 正则表达式](https://blog.csdn.net/panguangyuu/article/details/105033383)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [Linux Grep 正则表达式](https://blog.csdn.net/ZauberC/article/details/130629591)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值