Marco 2016 new Linux+python high-end op Viban-linux grep regular expression exercises, and the Find command

Source: Internet
Author: User
Tags echo command grep regular expression uppercase letter alphanumeric characters

Content of this week's job:

1. Display the default shell of root, fedora or User1 user on the current system;

[[email protected] home]# grep-e "^ (root|fedora|user1) \>"/etc/passwd |cut-d:-f1,7 root:/bin/bash  User1:/bin/bash Fedora:/bin/bash #本题使用扩展的正则表达式 #^: First line |: or (): Bind one or more characters together as a whole to handle \>: Indicates the final anchoring #cut-D: f1,7 represents the 1th and 7th fields by using the: number as the delimiter. Represents the user name and the default login shell, respectively



2. Find the line with a set of parentheses after a word in the/etc/rc.d/init.d/functions file, such as: Hello ();

    [[email protected] /]# grep -E -o  "[_[:alpha:]]+\ (\)"  /etc/rc.d/init.d/functions     fstab_decode_str ()      Checkpid ()     __readlink ()     __fgrep ()     __ Umount_loop ()     __umount_loopback_loop ()     __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 ()   &nbsP; strstr ()     confirm ()     get_numeric_dev ()      is_ignored_file ()     is_true ()     is_false ()      apply_sysctl ()     key_is_random ()     find_crypto_mount_point ()     init_crypto ()      #使用grep  -E  or Egrep extended regular expression,-o  Represents a match only to the character     #[_[:alpha:]] denotes a character that begins with an _ or uppercase letter; + indicates that the preceding character appears one or more times;     # To match the row containing (), the parentheses need to be escaped when filtering: \ (\)

3. Use the echo command to output an absolute path, using grep to remove its base name;

[Email protected]/]# echo "/tmp/ljohn/useradd.sh" | GREP-E-O "[^/]+$] useradd.sh [[email protected] ~]# echo"/tmp/ljohn/useradd.sh/"|grep-eo" [^/]+/?$ "|cut-d '/' -F1 useradd.sh

#基名basename/tmp/ljohn/useradd.sh

[Email protected] ~]# basename/tmp/ljohn/useradd.sh useradd.sh [[email protected] ~]# Basename/tmp/ljohn/useradd. sh/useradd.sh
#这样可以看出第二种同basename命令. #行尾的字符串 [^/] any content except/Slash, + represents this/at least 1 times #$: Indicates end of line anchoring

Extension: Take out its path name

[Email protected]/]# echo "/tmp/ljohn/useradd.sh" | GREP-E-O "^/.*/"/tmp/ljohn/[[email protected] ~]# echo "/tmp/ljohn/useradd.sh" |grep-eo "^/.*/" |grep-eo "^/.* [^/] "/tmp/ljohn #目录名dirname/tmp/ljohn/useradd.sh [[email protected] ~]# dirname/tmp/ljohn/useradd.sh/tmp/l John [[email protected] ~]# dirname/tmp/ljohn/useradd.sh//tmp/ljohn
#这样可以看出第二种同dirname命令 #^/.*/represents a/start, middle any length character, with/to the end. #.*: Any character of any length #[^/]: anything but a slash

4, find out the results of the ifconfig command between 1-255 numbers;

    [[email protected] /]# ifconfig | grep -E -o  " \< ([1-9]|[ 1-9][0-9]|1[0-9][0-9]|2[0-9][0-9]) \> " | sort     -n -u     5    8    19    20     29    30    31    38     39    41    42    45    64     100    127    128    137     150    164    168    192     255    #[1-9] means that 1-9;[1-9][0-9] represents 10-99;1[0-9][0-9] 100-199;2[0-9][ 0-9]) represents the 200-299     #使用-o parameter indicates that only matching characters are displayed;     #sort  -n -u  Sort by number size and go heavy; 

5, Challenge: Write a mode, can match the reasonable IP address;
# Class A address range: 1.0.0.1-126.255.255.254 Class B address range: 128.0.0.1-191.255.255.254
# Class C address range: 192.0.0.1-223.255.255.254 Class D address range: 224.0.0.1-239.255.255.254
# Class E address range: 240.0.0.1-255.255.255.254 127.x.x.x is a reserved address, used for cyclic testing
# Match Range is 1-255.0-255.0-255.1-254

[Email protected]/]# Ifconfig | GREP-E-O "(2[0-5]{2}|1[0-9]{2}|[ 1-9][0-9]| [1-9]) \. ((2[0-5]{2}|1[0-9]{2}| [1-9] [0-9]| [0-9]) \.) {2}\< (2[0-5][0-4]|1[0-9]{2}|[ 1-9][0-9]| [1-9]) \> "192.168.137.30 127.0.0.1

#此题参考http://ld0381.blog.51cto.com/3318114/1845601; http://2368360.blog.51cto.com/2358360/1846091
#详细解析:
^pattern$ for pattern matching entire row
{2} indicates that the preceding character appears exactly two times
| That means, or
() The parentheses above are not small enough to match the whole string in parentheses, and several () in the expression indicate that there are several corresponding matching strings
[0-4] represents any number of 0~4
[0-9] represents any number of 0~9
[1-9] means any number between the 1~9
[1-9] [0-9] means any number between the 10~99
1[0-9]{2} means any number between 100~199.
2[0-4][0-9] means any number between 200~249.
25[0-5] means any number between 250~255.
The meaning of the. Point to be escaped as a normal character

6, Challenge: Write a pattern, can match out all the email address;

[Email protected]/]# cat/tmp/mail.txt [email protected] [email protected] [email protected] [email protecte d]_cmd.cn [email protected] lkjdas_#@123_.com [[email protected]/]# grep-e "^[[:alnum:]][email protected][[:a Lnum:]]+\. [[: alnum:]]+$ "/tmp/mail.txt [email protected] [email protected] [email protected] [email protected] #先创建 A mail.txt file has been edited in advance for several mailboxes #首先要了解邮箱的格式: Digital letters @ alphanumeric characters. Alphanumeric #[[:alnum:]]+ denotes any number or letter

7. Find the main root of the/var directory, and belong to all files or directories of mail;

[[email protected] ~]# find/var-user root-group mail-ls 262296 4 drwxrwxr-x 2 root mail 4096 September 4 15:35/var/spool/mail 262048 4-rw-------1 root mail 2353 September 3 17:33/var/spool/mail/root #find命 Order format: Find [OPTION] ... [Find Path] [Search Criteria] [Handling action] #find命令实现对属主, array matching, implemented using-user and-group.

8, find the current system does not belong to the main or group of files;

[[Email Protected]liu ~]# Find/-nouser-o-NOGROUP/HOME/MANDIRVA/HOME/MANDIRVA/.GNOME2/HOME/MANDIRVA/.BASHRC /home/mandirva/.mozilla/home/mandirva/.mozilla/plugins/home/mandirva/.mozilla/extensions/home/mandirva/.ba Sh_logout/home/mandirva/.bash_profile/var/spool/mail/mandirva #-nouser is not a master;-nogroup does not have an array

Further: Find files or directories that are not owned by the master or group on the current system and have been visited in the last 3 days;

[[email protected] ~]# Find/\ (-nouser-o-nogroup \)-atime-3 find: "/PROC/3759/TASK/3759/FD/5": No that file or directory find: "/PROC/3759/TASK/3759/FDINFO/5": No file or directory find: "/PROC/3759/FD/5": No file or directory find: "/PROC/3759/FDINFO/5": Without that file or Record/home/mandirva/home/mandirva/.gnome2/home/mandirva/.mozilla/home/mandirva/.mozilla/plugins/home/mandi Rva/.mozilla/extensions

9, find all the users in/etc directory have write permission files;

[Email protected] ~]# Find/etc-perm-222/etc/rc.d/rc1.d/k73winbind/etc/rc.d/rc1.d/k92iptables/etc/rc.d/rc1. D/k99rngd/etc/rc.d/rc1.d/k84wpa_supplicant/etc/rc.d/rc1.d/k89portreserve ...

#根据权限查找:
-perm [+|-]mode
MODE: Exact permission match
+mode: The permission of any class (U,g,o) object can only be matched by one match;
-mode: Each type of object must have a permission standard assigned to it at the same time;
-222: Indicates that each user has write permissions.
10. Find all files that are larger than 1M in/etc directory and are of normal file type;

[Email protected] ~]# find/etc/-type f-size +1m/etc/selinux/targeted/modules/active/policy.kern/etc/selinux/ta Rgeted/policy/policy.24/etc/gconf/gconf.xml.defaults/%gconf-tree.xml

#根据类型查找:
-type Type:
F: Normal file
D: Catalog file
L: Symbolic Link file
S: Socket file
B: Block device files
C: Character device file
P: Pipeline File
#根据文件大小查找:
-size [+|-] #UNIT
Common units: K, M, G

#UNIT: (#-1, #)
-#UNIT: [0,#-1]
+ #UNIT: (#,oo)

11, find/etc/init.d/directory, all users have execute permission, and other users have write permission files;

[[email protected] ~]# find/etc/init.d-perm-113-ls 652914 0 lrwxrwxrwx 1 root root 11 February 12 201 5/ETC/INIT.D-RC.D/INIT.D

12. Find files that do not belong to root, bin or hadoop in the/usr directory;

    [[email protected] ~]# find /usr -not \ ( -user  root -o -user bin -o -user hadoop \)  -ls      20956    8 -rwsr-xr-x   1 abrt      abrt         6700 11 Month  23  2013 / Usr/libexec/abrt-action-install-debuginfo-to-abrt-cache or:     [[email protected]  ~]# find /usr -not -user root -a -not -user bin -a  -not -user hadoop -ls     20956    8 - rwsr-xr-x   1 abrt     abrt          6700 11 Month  23  2013 /usr/libexec/ Abrt-action-install-debuginfo-to-abrt-cache

13, find the/etc/directory at least one class of users do not have write permission files;

    [[email protected] ~]# find /etc -not  -perm - 222 -ls    652802   12 drwxr-xr-x 118 root      root        12288 9 Month   4 15:35  /etc    652826    0 -rw-r--r--   1 root      root            0  1 Month  12  2010 /etc/exports    661961    4  drwxr-xr-x   2 root     root          4096 8 Month  30 23:52 /etc/glances    662033     4 -rw-r--r--   1 root     root          3288 1 Month  18  2014 /etc/glances/glances.conf 

...
14. Find files whose contents have been modified and not rooted or Hadoop for the last week in/etc directory;
Create a file and Modify permissions:

[email protected] etc]# cat > Findtest.txt <<eof > 123123 > hello!!    > How is it! > EOF [[email protected] etc]# chown liu.liu findtest.txt [[email protected] etc]# ls-l findtest.txt-rw-r--r --.    1 Liu Liu 29 September 5 05:07 findtest.txt [[email protected] etc]# find/etc-mtime-7-not-user root-not-user Hadoop /etc/findtest.txt


This article is from the "Ljohn" blog, make sure to keep this source http://ljohn.blog.51cto.com/11932290/1846386

Marco 2016 new Linux+python high-end op Viban-linux grep regular expression exercises, and the Find command

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.