Linux Find Usage

Source: Internet
Author: User

How to recursively count files in a Linux directory?

    • Find Dir_name-type F | Wc-l
    • Find Dir_name-type F-print | Wc-l

Find include alias of the string *.conf of the file

Find/-name "*.conf" | Xargs grep "Alias"

1. grep-r-L Some can also be implemented, but through pipelines | is more efficient than simply using grep's complex parameters.

2. Xargs is used to expand the results obtained by the find as a parameter to grep.

    • find/home/fizz-type f-printf '%ty-%tm-%td%tt%p\n ' | sort Sort by modified time
    • find.-maxdepth 1-size 0c-delete Remove all 0-size files from the current folder (non-recursive)
    • find.-name "*.py"-exec grep-n-h-e "^ (import|from) math" {} \; Find all the used Python module
    • find.-name ' *pdf* '-print0 | xargs-0 ls-lt | head-20 find the latest modified file
    • find./-empty-type d-print Show all Empty folders
    • find.-type f-name "*"-mtime +10-print-exec gzip {} \; compress files up to 10 days ago
    • find directory-maxdepth 1-type f | xargs ls-l | awk ' BEGIN {sum=0} {sum+=$5} END {print sum/2^20} ' Calculate the sum of the file sizes in the catalog
    • find.-type d-maxdepth 1 List Directories only
    • find/var/scripts-name ' backup* '-exec {} '; ' Run multiple Scripts
    • find.-type f | awk-f '. ' ' {print $NF} ' | Sort| uniq-c | Sort-g List all extensions

$ find. -type F | Awk-f '. ' ' {print $NF} ' | Sort| uniq-c | Sort-g

1 CSV

Jmx

here is a detailed introduction to the powerful " Find all methods and uses of the command.

    • By file name Lookup method:

This method is said to be WINDOWS It's as easy to understand as finding a file. If you put this file in a single folder, as long as the common "ls" command can be easily found, then use the "find" command to find it can not impress you, After all, the powerful function of the "find" command is more than that. If you know the file name of a file, you do not know which folder to put the file into, or even a nested folder in layers. For example, suppose you forget httpd.conf The file is in the directory of the system, even if it is not known somewhere in the system, it is possible to use the following command:

Find/-name httpd.conf

This command syntax seems to be easy to understand, that is, directly in the Find write on the back. -name , which indicates that the system is required to search by file name and finally httpd.conf the target file name can be. Hold on a second. The system displays a list of search results on the computer screen:

Etc/httpd/conf/httpd.conf

this is httpd.conf This file is Linux the full path in the system. Find success.

if the system does not show results after entering the above lookup command, do not assume that the system is not performing find/-name httpd.conf command, and may not be installed on your system Apache server, as soon as you install the Apache Web server, and then use Find/-name httpd.conf will be able to find this configuration file.

No Error finding tips:

in the Linux system in the " Find "command is a command that most system users can use, not ROOT system Administrator's patent. But it is also possible for ordinary users to use the "find" command, which is the system administrator ROOT in the Linux system Some file directories can be set to block access mode. This way, ordinary users do not have the right to use the "find" command to query these directories or files. When ordinary users use the "find" command to query these file directories, the "permissiondenied." will often appear. ( No access ) words. The system will not be able to query the file you want. To avoid such errors, we tried to find the file by using the method of transfer error, enter

Find/-name Access_log 2>/dev/null

This method is to transfer the lookup error prompt to a specific directory. After the system executes this command, the information that encounters the error is delivered directly to Stderrstream 2 ,access_log 2 indicates that the system will send the error message to Stderrstream 2 ,/dev/null is a special file that indicates an empty or incorrect message, so that the error message that is queried will be shifted and will not be displayed again.

in theLinuxThe system finds a file that also encounters such a practical problem. If we are on the entire hard drive, it will take a long time to find a file in this system, especially for largeLinuxThe system and the larger capacity of the hard disk, the file placed in a deep nested directory. If we know that this file is stored in a large directory, you can save a lot of time by looking down in this directory. UseFind/etc-name httpd.confcan solve this problem. The above command means that theetcQuery in Directoryhttpd.confthis file. Here's another explanation./ "The meaning of this function symbol, if the input"find/"That means the request .Linuxsystem in the entireROOTDirectory to find files on the entire hard disk, and "find/etc"is only inetcdirectory to find the file. Because "find/etc"indicates that only theetcdirectory to find files, so the speed of the search is much faster.

    • Find methods based on partial file names:

This method and the WINDOWS find the known file name method in the same way. However , it is much more powerful to find a file in Linux based on a partial file name than in a similar lookup method in WINDOWS. For example, we know that a file contains 3 Letters of SRM, so it is possible to find all the files in the system that contain these 3 letters. Input:

Find/etc-name ' *srm* '

This command indicates the Linux The system will be / etc The entire directory finds all the included SRM it 3 letter files, such as Absrmyz , TIBC.SRM and so on, the eligible files can be displayed. If you also know that this file starts with SRM 3 Letters, then we can omit the first asterisk, the command is as follows:

Find/etc-name ' srm* '

This is only like Srmyz such a document was found, like Absrmyz or ABSRM Such files do not meet the requirements, not be displayed, so that the efficiency and reliability of the search file is greatly enhanced.

Based on the characteristics of the file Query method:

If you only know the size of a file, the modified date and other features can also use theFind"command to find out, which andWINDOWSin the system"Search"functions are basically the same. In Microsoft's"Search"inWINDOWSin the"Search Assistant"makes it easier to search for files and folders, printers, users, and other computers on your network. It even makes aInterneteasier to search on. "Search Assistant"It also includes an Indexing service that maintains an index of all the files in the computer, making the search faster. Use"Search Assistant", users can specify multiple search criteria. For example, users can search for files and folders by name, type, and size. Users can even search for files that contain specific text. If the user is usingActive Directory, you can also search for a printer with a specific name or location.

For example, we know aLinuxFile size is1,500 bytes, then we use the following command to queryFind/-size 1500c, characterCIndicates that the size of the file to be looked up is inbytesas the unit. If we don't even know the exact size of this file, then inLinuxcan also be used to solve the fuzzy Lookup method. For example, we enterfind/-size +10000000cThis command indicates that we have specified the system in the root directory to find more than10000000bytes of the file and display it. In the command, "+"is to ask the system to list only files larger than the specified size, and use the"-"indicates that the system is required to list files smaller than the specified size. The following list is theLinuxuse different "Find "after the command system to make the search action, from which we can easily see inLinuxin use "Find "The way of the command is a lot, "Find "command to find files as long as flexible application, no need toWINDOWSPoor search ability.

Find/-amin-10 # find in the system last Ten minutes of accessed files

Find/-atime-2 # find in the system last - hours of access to files

Find/-empty # find files or folders that are empty in the system

Find/-group Cat # find in systems that belong to Groupcat of the file

Find/-mmin-5 # find in the system last 5 files that have been modified in minutes

Find/-mtime-1 # find in the system last - modified files in the hour

Find/-nouser # find files in the system that belong to obsolete users

Find/-user Fred # find in systems that belong to FRED This user's file

The following list is the Find A command can specify some of the criteria for a file's characteristics to be found. There are no search criteria listed here, so refer to the Find function of all find commands for Linux - related books .

-amin N

find the system in the last N minutes of accessed files

-atime N

find the system in the last n*24 hours of access to files

-cmin N

find the system in the last N minutes changed state of the file

-ctime N

find the system in the last n*24 hours changed state of the file

-empty

Find a blank file in the system, or a blank file directory, or a folder with no subdirectories in the directory

-false

Find files that are always wrong in your system

-fstype type

find files in the system that exist on the specified file system, for example: ext2.

-gid N

find a group of file numbers in the system ID to be N of the file

-group Gname

Locate the files in the system that belong to Gnam Filegroup , and specify the group and ID of the file

Find description of the control options for the command:

Find The command also gives the user some unique options to control the find operation. The following table summarizes the most basic, most commonly used find Command control options and their usage.

Options

Description of Use

-daystart

. the test system starts today - less than the hours of the file, usage similar -amin

-depth

How to use the depth-level lookup process , first find the contents of a file in a certain level of specified directory

-follow

follow the linked connection method to find ; In addition, you can ignore the linked connection mode query

-help

Show Command Summary

-maxdepth levels

Find by descending method in a hierarchy of directories

-mount

not found in the file system directory, Similar usage -xdev.

-noleaf

ban on non- Unux file System, MS-DOS System, CD-ROM optimal search in the file system

-version

Print version number

Use -follow option, Find the command is followed by a wildcard linked, unless you specify this option, otherwise generally Find the command ignores the wildcard linked connection for file lookups.

-maxdepth The purpose of the option is to limit Find command to search for files more than a certain level or search for too many directories in a directory in descending order, resulting in slower lookups and too much time spent searching. For example, we want to have the current (.) find a directory in the tricks subdirectory named Fred file, we can use the following command

Find. -maxdepth 2-name Fred

if this Fred file in ./sub1/fred directory, then this command will directly locate the file and find it easy to succeed. If this file is in the ./sub1/sub2/fred directory, then this command cannot be found. Because the find command is already given the largest query directory level in the directory at 2, only files in the 2 -tier directorycan be found . The purpose of this is to get The Find command to locate the file more precisely, if you already know the file directory of the file is probably in the series, then add -maxdepth n will soon be able to find success in the specified directory.

Find a file using a hybrid lookup method

Find command can use a hybrid lookup method, for example, we want to / tmp directory in which the lookup is greater than 100000000 bytes and in - a file that is modified within the hour, we can use -and to combine the two look-and-choose items together into a hybrid look-up method.

Find/tmp-size +10000000c-and-mtime +2

friends who have studied computer language know that in computer languages, using and, or The relationship between "and" and "or" is expressed respectively. Common in Search commands for Linux systems.

There are examples of this,

Find/-user Fred-or-user George

we can explain it to the / tmp the directory in which the lookup belongs to Fred or George files for these two users.

in the Find The "non" relationship can also be used in the command to find the file if we want to / tmp directory to find all that do not belong to Panda the file, using a simple

Find/tmp! -user Panda

The command will be solved. Very simple.

Ways to find and display files

finding a file is our goal, we would like to know more information and properties of the file found, if we take the present find file in the use LS command to view the file information is quite cumbersome, and now we can also combine the two commands to use.

Find/-name "httpd.conf"-ls

the system finds httpd.conf immediately after the file is displayed on the screen httpd.conf file information.

12063 34-rw-r--r--1 root root 33545 Dec 15:36/etc/httpd/conf/httpd.conf

The following table is a list of commonly used find files and displays the parameters and usage of file information

Options

Description of Use

-exec command;

Find and Execute commands

-fprint file

Print file full filename

-fprint0 file

Print files full file name including empty files

-fprintf file format

Print file format

-OK command;

perform actions on user commands, depending on the user's Y Confirm Input Execution

-printf format

Print file format

-ls

print files of the same file format .

Summary: So far we have learned this much about Find The use of commands, also lists a number of commonly used Find command of the option if we can skillfully master the Linux in Find How to use the command, then in the Linux It's not a difficult thing to find files in.

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.