Solaris file search and string search commands

Source: Internet
Author: User
Solaris file search and string search command 1. grep function description grep command searches for strings matching the specified mode in one or more files. If the mode contains spaces, it must be enclosed in quotation marks. The grep mode can only be a string enclosed by quotation marks or a word... solaris file search and string search command 1. grep function description grep command searches for strings matching the specified mode in one or more files. If the mode contains spaces, it must be enclosed in quotation marks. The grep mode can only be a string enclosed by quotation marks or a word, followed by parameters are treated as file names. The grep command outputs the result to the standard output without changing the source file to be searched. Command format: grep pattern filename filename2... grep has several options that are commonly used.-I searches are case-insensitive, and comparison is performed.-n shows the row to be found. the row number in the file-v shows an unmatched row. Example 1: find the row containing "bugboy" in the/etc/passwd file [bugboy @ bugboy ~] $ Grep bugboy/etc/passwd bugboy: x: 500: 500: YanDingcheng:/home/bugboy:/bin/bashgrep find each row containing bugboy in the/etc/passwd file, if it is found, the row is output to the standard output. the exit status of grep is 0. if it is not found, grep does not output any information and the exit status is 1. if the specified file does not exist, the exit status of grep is 2. The grep mode supports regular expressions. Below are some common regular expressions that match metacharacters. ^ The starting anchor of a row, for example, '^ love', matches all rows starting with love. $ End anchor, for example, 'Love $ ', matches all rows ending with love.. Match any character, such as 'L. E'. match all strings starting with "l" followed by two characters and ending with "e. * Matches 0 or multiple arbitrary characters, such as '* bug'. matches strings starting with any number of spaces followed by "bug. [] Match a character in the character set, for example, '[Bb] ook', matching Book or book. [^] Match a character that is not in the character set, for example, '[^ A-Z] low', matches all strings starting with an uppercase letter followed by "low. /<Super-start anchor, for example '/ The ending anchor, for example, 'or/>', matches all words ending with "or. X/{m/} matches m x, for example, 'o/{5} ', and matches 5 o, that is, "ooooo ". X/{m,/} matches at least m x, for example, 'o/{5,/}, matches at least 5 o, that is, "ooooo" and "Oooooooo. X/{m, n/} matches m to n x, for example, 'o/{2, 5/} '. if 2 to 5 are matched, that is, "oo ", "ooo", "oooo", "Ooooooo ". Example 2: in/etc/passwd, find the user name [bugboy @ bugboy test] $ grep ^ B/etc/passwd bin: x: 1: 1: bin:/sbin/nologin bugboy: x: 500: 500: YanDingcheng:/home/bugboy:/bin/bash Example 3: find the bash user in/etc/passwd [bugboy @ bugboy test] $ grep bash $/etc/passwd root: x: 0: 0: root:/root: /bin/bash ftp: x: 14: 50: FTP User:/var/ftp:/bin/bash netdump: x: 34: Network Crash Dump user: /var/crash:/bin/bash pvm: x: 24:/usr/share/pvm3:/bin/bash mysql: x: 27: 27: MySQL Server: /var/lib/mysql:/bin/bash S: x: 26: 26: PostgreSQL Server:/var/lib/pgsql:/bin/bash bugboy: x: 500: 500: YanDingcheng:/home/bugboy:/bin/bash Example 3: in/etc/passwd, find the line [bugboy @ bugboy test] $ grep '0/{2/} '/etc/passwd games: x: 12: 100: games:/usr/games:/sbin/nologin bugboy: x: 500: 500: YanDingcheng:/home/bugboy:/bin/bash Example 4: Search for sscanf. non-empty lines in the c file [bugboy @ bugboy test] $ cat sscanf. c # include # Include Int main (int argc, char * argv []) {const char * str = "I am bugboy"; char mystr [256]; sscanf (str, "% s ", mystr); printf ("1: % s/n", mystr); char key [64], value [64]; sscanf (str, "% s ", key, value); printf ("key: % s, value: % s/n", key, value); return 0 ;} [bugboy @ bugboy test] $ grep-v '^ * $ 'sscanf. c # include # Include Int main (int argc, char * argv []) {const char * str = "I am bugboy"; char mystr [256]; sscanf (str, "% s ", mystr); printf ("1: % s/n", mystr); char key [64], value [64]; sscanf (str, "% s ", key, value); printf ("key: % s, value: % s/n", key, value); return 0;} Example 5: Search for sscanf. the c file contains rows of "const char". Note that if the string to be searched contains more than one word, enclose it with quotation marks. [Bugboy @ bugboy test] $ grep "const char" sscanf. c const char * str = "I am bugboy"; 2. find function description find command find [path...] [option] [-exec |-OK |-print] path of the find command. -Execute the exec find command to execute a shell command for each matching file found. The command format is "-exec command {}/;". pay attention to "{}" and "/; "There is a space between them, and the last"; "Don't forget. -OK and-exec play the same role, but only request the user to confirm before executing the command to execute the command more securely. -Print outputs the searched files to the standard output. Example 1: find and delete all files with the extension ". tmp" in the/tmp directory. [Bugboy @ bugboy test] $ find/tmp-name "*. tmp "-exec rm {}/; here we use the-name option, which is to search by file name, which will be described later. Command option-name: search for files by file name. -Perm searches for files based on the file permissions. -The user searches for files based on the file owner. -The group searches for files based on the group to which the files belong. -Mtime-n + n: find the file based on the file change time.-n indicates that the file change time is earlier than n days, and + n indicates that the file change time is earlier than n days. The find Command also has the-atime and-ctime options, which are similar to the-mtime options. -Nogroup: find the file with no valid group, that is, the group to which the file belongs does not exist in/etc/groups. -Nouser: find the file without a valid owner, that is, the owner of the file does not exist in/etc/passwd. -Newer file1! -Newer file2: find files whose change time is newer than file1 but older than file2. -Type: search for a certain type of files, such as B-block device files. D-directory. C-character device file. P-MPs queue file. L-symbolic link file. F-common file. -Size n: [c] searches for files with a length of n blocks. if a file contains c, the file length is measured in bytes. -When searching for files, depth first searches for files in the current directory, and then searches for files in its subdirectories. -Fstype: searches for files in a certain type of file system. These file system types can usually be found in the configuration file/etc/fstab, this configuration file contains information about the file system in the system. -Mount does not span the mount point of the file system when searching for files. -Follow: If the find command encounters a symbolic link file, it traces the file pointed to by the link. -Cpio uses the cpio command to back up the matching files to the tape device. In addition, the following three differences: -amin n: find the files accessed in the last N minutes in the system.-atime n: find the files accessed in the last n * 24 hours in the system.-cmin n: find the files whose status is changed in the last N minutes in the system. FILE-ctime n find the file whose status has changed in the last n * 24 hours in the system-mmin n find the file whose data has been changed in the last N minutes in the system-mtime n find the last file in the system example 2 of a file whose data is changed in n * 24 hours: find the block device file in the current directory [bugboy @ bugboy dev] $ find. -type B. /fd038520. /fd0H360. /fd0H1440. /fd0D720 some results are omitted here. Example 3: Search for the files modified in the current directory within five days [bugboy @ bugboy test] $ find. -mtime-5 .. /toto. s. /toto. c. /tookit. /over. /over. s. /over. c. /tookit. example c 3: Search for the ratio of stat in the current directory. c file is newer than over. c. the old file [bugboy @ bugboy test] $ find. -newer stat. c! -Newer over. c. /toto. s. /crypt. /test. /toto. c. /strtok. /foo. h. /fstatvfs. /strsep. example 4: find a file in the current directory with 644 (user-readable, write, group-readable, and other user-readable) permissions [bugboy @ bugboy test] $ find. -perm 644. /auto/gnip-1.0.tar.gz. /auto/gnip. o. /auto /. deps/gnip. the Pofind command is used in combination with the xargs command. when the-exec option of the find command is used to process matched files, the find command passes all matched files together to exec for execution. However, some systems have limits on the length of commands that can be passed to exec, so that an overflow error will occur after the find command runs for several minutes. The error message is usually "the parameter column is too long" or "parameter column overflow ". This is the use of the xargs command, especially used with the find command. The find command passes the matching file to the xargs command, while the xargs command only obtains part of the file, not all, at a time, unlike the-exec option. In this way, it can first process the first part of the obtained files, then the next batch, and continue like this. In some systems, The-exec option is used to initiate a corresponding process for processing each matching file, rather than executing all the matching files as parameters once; in some cases, there may be too many processes and the system performance may degrade, so the efficiency is not high. The xargs command has only one process. In addition, when using the xargs command, whether to obtain all parameters at a time or obtain parameters in batches, and the number of parameters obtained each time is determined based on the options of the Command and the corresponding adjustable parameters in the system kernel. Example 5: Search for all. c file, search for the stat string in the file, and output the row and row number containing the stat [bugboy @ bugboy test] $ find. -name "*. c "| xargs grep-n stat. /stat. c: 3: # include ./Stat. c: 5: # include ./Stat. c: 9: struct statvfs fsd;./stat. c: 11: statvfs ("/", & fsd);./fstatvfs. c: 2: # include ./Fstatvfs. c: 4: # include . /Fstatvfs. c: 10: struct statvfs vfs ;. /fstatvfs. c: 14: if (fstatvfs (fd, & vfs) <0 ){. /fstatvfs. c: 15: fprintf (stderr, "fstatvfs error (). /n ");. /statl. c: 2: # include . /Statl. c: 8: struct stat statbuf ;. /statl. c: 15: stat (argv [1], & statbuf );. /statl. c: 16: if (S_ISDIR (statbuf. st_mode )){. /statl. c: 18:} else if (S_ISBLK (statbuf. st_mode )){. /test. c: 7: static int get_netmask_len (const char * netmask) 3. the which and whereiswhich commands search for executable files in the PATH environment variable and print out the full PATH of the file. For example, [bugboy @ bugboy test] $ which test/usr/bin/test [bugboy @ bugboy test] $ which ls man find alias ls = 'ls -- color = tty '/bin/ ls/usr/bin/man/usr/bin/findwhereis command to find the location of the source file, executable file, and manual file. For example, [bugboy @ bugboy test] $ whereis test: /usr/bin/test/usr/share/man/man1p/test.1p.gz/usr/share/man/man1/test.1.gz [bugboy @ bugboy test] $ whereis ls find ls: /bin/ls/usr/share/man/man1p/ls.1p.gz/usr/share/man/man1/ls.1.gz find: /usr/bin/find/usr/share/man/man1p/find.1p.gz/usr/share/man/man1/find.1.gzwhich and whereis both commands can use multiple parameters as search commands..
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.