Linux commonly used to find files or file content

Source: Internet
Author: User
Tags memcached haproxy

Example tree diagram

.
|--Test_dir
| '--Dir_test_doc.text
|--Test_dir2
| |--dir2_test_doc.txt
| '--Dir2_test_doc2.txt
|--Test_doc1.text
'--Test_doc2.txt

2 directories, 5 files

1. Find the line for the contents of the file:

grep "searched string" file name

For example:

[[email protected] demo]# grep "doc1_content" Test_doc1 <--Find "Test_doc1" in file doc1_content
Testdoc1_content

2. Find the found string contained in the text file in this level (current) directory

[[email protected] demo]# grep "my_cool2007" * <--* represents a text file in this level directory
test_doc1:my_cool2007
test_doc2:my_cool2007

3. Find the file that contains the found string in the next level directory of the current directory

[[email protected] demo]# grep "my_cool2007" */*.txt
test_dir2/dir2_test_doc.txt:my_cool2007
test_dir2/dir2_test_doc2.txt:my_cool2007

4. grep with parameters

Find the line that matches the regular expression from the file contents:
$ grep–e "Regular expression" file name

Find is case insensitive:
$ grep–i "string found" file name

To find the number of matching rows:
$ grep-c "string found" file name


Find rows that do not match the specified string from the file contents:
$ grep–v "string found" file name

  

From the root directory, look for all text files with a. log extension and find the line that contains "ERROR"
Find/-type f-name "*.log" | Xargs grep "ERROR"
Example: Starting from the current directory, find all text files with an. in extension and find the line containing "Thermcontact"
Find. -name "*.in" | Xargs grep "Thermcontact"

==================== I'm a split line ==========================

Find directories and files

Find path-command parameters [output FORM]

Parameter description:
Path: Tell find where to find the things you want,
Command parameters: Parameters Many of the following will be said
Output form: A lot of output,-print,-printf,-print0,-exec,-ok,-ls anyway a lot of themselves read the manual it.

Say exec,
The-exec find command executes the other Linux commands given by this parameter to the matching file. The corresponding command is in the form of ' command-and ' {} \;, note the space between {} and \;
-ok and-exec have the same effect, except that they will interact with the person, and you'll be sure to do it before OK executes.
Find command Main parameters:
-name finds files by file name.
-perm to find files according to file permissions.
-prune Use this option to have the Find command not be found in the currently specified directory, and if the-depth option is used at the same time, the-prune option will be ignored by the Find command.
-user Search for files according to the owner of the file.
-group finds files according to the group to which the files belong.
-mtime-n +n The file changes time to find the file,-n means that the file change time is now less than n days, +n indicates that the file change time is now N days ago. The Find command also has the-atime and-ctime options, but both of them and-mtime options
Similar, so we only introduce the-mtime option here.
-nogroup finds a file that does not have a valid owning group, that is, the group to which the file belongs does not exist in/etc/groups.
-nouser finds a file without a valid owner, that is, the owner of the file does not exist in the/etc/passwd.
-newer file1! File2 look for a file that changes time than the file File1 new but older than the file file2.
-type find a file of a certain type, such as:
B-block device files.
D-Directory.
C-character device file.
P-Pipeline file.
L-Symbolic link file.
F-Normal file.
S-socket file
-size N[c] finds files with a file length of n blocks, with C indicating the length of the file in bytes.
-depth when looking for a file, first find the file in the current directory, and then look in its subdirectories.
-fstype find files that are located in a type of file system, which are usually available in the configuration file
/etc/fstab, this configuration file contains information about the file system in the system.
-mount does not cross the file system mount point when locating files.
-follow if the find command encounters a symbolic link file, it tracks to the file that the link points to.
-cpio Use the Cpio command on the matching files to back up these files to the tape device.


Example 1

A, by name to find

[Email protected] ~]$ Find ~-name memcached.pid-print   #查找home目录下文件名为memcache. pid file/home/zhangy/memcached/ Memcached.pid[[email protected] ~]$ find. -name "*.pid"-print      #. Represents the current directory, looking for all files that end with a PID./memcached/memcached.pid./.tencent/qq/95219454.pid[[email Protected] ~]$ Find ~-name "[0-9]*.pid"-print   #查找以数字开头的所有pid文件, here to say [0-9]* cannot match 23, it is not the same as the general language class, Inside the shell * can represent all characters (single, multiple lines), if you want to match 2,345 can only write [0-9][0-9][0-9][0-9]/home/zhangy/.tencent/qq/95219454.pid[[email Protected] ~]$ find/home/zhangy/.tencent/-name "[0-9]*.pid"-print  #在. Tencent folder find PID file/home/zhangy/.tencent/ Qq/95219454.pid

b, using file permissions to find

[[email protected] css]$ Find ~-perm 755-print |more #~   represents the $home directory, looking for a file with permission 755/home/zhangy/www/css2/c_ Textshadow.html/home/zhangy/www/css2/c_textautospace.html[[email protected] css]$ find. -perm 700-name "u_*"-print |more  #查找所有以产u_开头的, and a file with a permission of 700./css2/u_length_cm.html./css2/u_length_px.html

C,prune to ignore the directory to find

[[email protected] download]$ find. -name "*.gz"  -prune-o \ (\!-name AAA \)  -print  #查找在前目录中, files not in the directory ending in AAA, not in GZ: /eaccelerator-0.9.5.3.tar./fix-crash-in-excerpts.patch./addfeed_widget_wordpress_plugin.zip./jquery China-addthis plugin 1.07.rar

D, find files according to file type

[[email protected] download]$ find. -type d-print   #查找当前目录下面的目录. /ddd[[email protected] download]$ find. ! -type d-print  #找当前目录下面的非目录文件./eaccelerator-0.9.5.3.tar./haproxy-1.3.15.7.tar.gz./ Fix-crash-in-excerpts.patch

E, find files based on the user and user group the file belongs to

[[email protected] download]$ find. -nouser-print  #查找当前目录中, no attribution file [[email protected] download]$ find/home/zhangy/download-user zhangy-group users- Print  #查找用户组为users files that belong to the user zhangy/home/zhangy/download/home/zhangy/download/eaccelerator-0.9.5.3.tar/home/ Zhangy/download/haproxy-1.3.15.7.tar.gz

F, root file size to find

[[email protected] download]$ find/home/zhangy/download-size +1000000c-print  #查找文件大小大于1000000字符的文件, note that the + sign is greater than/ Home/zhangy/download/eaccelerator-0.9.5.3.tar/home/zhangy/download/mmseg-0.7.3.tar.gz[[email protected] Download ]$ Find/home/zhangy/download-size-10-print  #查找文件大小小于10块的文件, note-number means less than, one piece equals 512b/home/zhangy/download/home/ Zhangy/download/fix-crash-in-excerpts.patch/home/zhangy/download/test.sql.zip

G, the modification time of the root file to find

[Email protected] download]$ find/home/zhangy-mtime-5-print  #5天修改过的文件,-= within/home/zhangy/www/css2/c_ Textshadow.html/home/zhangy/www/css2/c_textautospace.html[[email protected] download]$ find/home/zhangy-mtime +5- Print   #查找5天前修改过的文件, + indicates previous/home/zhangy/www/test.php[[email protected] www]# find./-mmin-5-print  # Find files that have been modified within 5 minutes./cache/index.html

H,exec explanation

[[email protected] download]$ find. -type f-size +1000000c-exec ls-al {} \;  #显示当前目录下面所有大于1000000的文件, exec executes a command behind it, {} This represents the file name-rw-r--r--1 zhangy users 3624960 2009-03-08. eaccelerator-0.9.5.3.tar-rw-r--r--1 zhangy users 3091711 12-18 13:48./mmseg-0.7.3.tar.gz-rw-r--r--1 zhangy users 11913 2009-06-26./pcre-7.9.tar.gz

I, matching

[Email protected] workspace]$ find/database/-name ' *.sql '-print    #查找以sql结尾的文件./database/28toplearning.sql[[ Email protected] workspace]$ find/database/-name ' *.sql* '-print   #查找文件名包括sql文件./database/28toplearning.sql[[ Email protected] workspace]$ find/database/-name ' 28* '-print      #查找以28开头的文件./database/28toplearning.sql

Example 2
Find./-name "null_*"-exec basename {} \; | Sort   #搜索文件 and displays only the file names in ascending order. Null_0null_1null_2null_3null_4null_5null_6null_7null_8null_9

Example 3
         Find./-type f-name "*.log" | Xargs grep-r "Error"    



Reference article:
Http://linux.51yip.com/search/find
Http://blog.chinaunix.net/uid-25266990-id-199887.html

Linux commonly used to find files or file content

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.