Common methods for searching files under Linux

Source: Internet
Author: User
Tags file permissions

    • Use the which command to find the absolute path to the file

This command is more commonly used, such as finding the absolute path of bin, Python, vim, find, and cat

[Email protected]:~# which find /usr/bin/Find [email protected]-172-31-8-212:~# which bash /bin/bash [email protected]-172-31-8-212:~# which python /usr/bin/ Python [email protected]-172-31-8-212:~# which vim /usr/bin/vim

    • Whereis find the file names in the pre-generated file list library with the associated files in the format: Whereis [-bms] File name

-B: Find only binary files

-M: Fork Help Files (find files in the man directory)

-S: Fork source code file

    

[Email protected]:~#  whereis lsls:/bin/ls/usr/share/man/man1/ls.1.gz

Fuzzy lookups, whereis commands are seldom used

  

    • Locate command to find files

The Locate command also tells the user where to look for a file by looking up a pre-generated list of files, followed by a file name, and if there is no such command, install the following command:

 

Yum Install-y mlocate
    • Search for files using the Find command

This command is used in more than one format: find "path" "parameter"

        #no time created for Linux files        #Access Usage Time        #Modify Content Modification Time        #Change state changed time (permissions, owner)        #The time default is 24 hours, the current time to 24 hours ahead of 0 days, 48-72 hours ahead for 2 days        #-and and match two condition parameters to determine the time range-mtime +2-and-mtime-4        #-or or match any one of the conditionsFind/etc-name"*http*"     #Find by file nameFind. -type F#finding a file of a typeFind/-perm#Search by file permissionsFind/-user#Search by file ownerFind/-group#find a file by the group to which it belongsFind/-atime-n#file usage time is less than n daysFind/-atime +n#file usage time is before n daysFind/-mtime +n#file content Change time was n days agoFind/-ctime +n#file state change time is n days agoFind/-mmin +30#find content changes by minuteFind/-size +1000000c-Print                           #find files with file lengths larger than 1M bytesFind/etc-name"*passwd*"-execGrep"Xuesong"{} \;#find files by name pass to-exec commandFind. -name't*'-execbasename {} \;#Find file name, do not take pathFind. -type F-name"err*"-execRename err err {} \;#Batch rename (find err replaced by err {} fileFind Path-name *name1*-or-name *name2*#find any one keywordFind/var/adm-mtime +3-Print                           #In order to find files that change time before 3rd in the/var/adm directoryFind. -newer Age.awk! -newer Belts.awk-execls-l {} \;#find time to change Bai Age.awk new but older files than Belts.awk filesFind/etc-type D-Print                                 #Find all directories in/etc directoryFind/etc! -type D-Print                                 #Find all types of files except directories in/etcFind/etc-type L-Print                                 #Find all the symbolic link files under/etc


Examples are as follows:
The Find command instance simply uses the Find command to find a file under the specified directory in the following ways: #find/etc-name inittab/etc/inittab# in the current directory, look for files with the name Inittab: #find. -name Inittab./inittab# Find out the current directory, file names are not case sensitive to all files that are example: $find. -iname example./example./example# Locate the directory named TMP under the current directory: $ find. -type d-name tmp./tmp# Find all SH files in the current directory: $ find. -type f-name "*.sh"./login.sh./index.sh# find all files in the current directory with file permissions of 777: $find. -type f-perm 0777# Find out the current directory, file permissions are not 777 of all files: $ find. -type f! -perm 777# Find all read-only files in the/etc/directory: Find. -type f!  -perm/a+w# Find all the executables in your account's home directory: $ find ~-type f-perm/a+x# Find and delete the. log file in the/tmp directory: $find/tmp/-type f-name "*.log"-exec RM -f {} \; #找出当前目录下的所有空文件: $find. -type f-empty# Find all empty directories in the current directory: $find. -type d–empty# Find all hidden files in the/tmp directory: $find/tmp/-type f-name ". *" #找出 the/tmp directory, the owner is the root file and directory: $find/tmp/-user root# Locate the/tmp directory , the user group is developer files and directories: $find/tmp/-group developer# Find your account's home directory, modified files 3 days ago: $ find ~-type f-mtime 3# Find your account's home directory, All files modified 30 days ago: $ find ~-type f-mtime +30# Find all files modified within 3 days from the home directory of your account: $ find ~-type f-mtime-3# Find out the main directory of your account, 30 days prior to the change within 60 daysFiles: $ find ~-type f-mtime +30-mtime-60# Find files changed in/etc in less than one hour: Find/etc-type f–cmin-60# find files that have been accessed within an hour/etc directory: find /etc-type f-amin-60# Find out your account's home directory, the size is 50MB all files: $find ~-type f-size 50mb# Find out your account's home directory, more than 50MB less than 100MB of all files: $ find ~-type F- Size +50mb-size-100mb# Find the file in your account's home directory that is larger than 100MB and delete it: $ find ~-type f-size +100mb-exec rm–rf {} \;

Common methods for searching files under Linux

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.