Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the find command to find files in Linux

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

How do I use the find command to find files in Linux? Many novices are not very clear about this. In order to help you solve this problem, the following small series will explain it in detail for everyone. Those who have this need can come to learn. I hope you can gain something.

Syntax: find + target directory (path) + + parameters

List of common options

Option Meaning-perm Find files or directories that match the specified permission values-type Find only files that match the specified file type-name Specify a string as a template for finding files or directories-expty Find files with a file size of 0 Byte, or empty directories without any subdirectories or files under the directory-ls Assuming the return value of the find command is true, list file or directory names to standard output-maxdepth Set maximum directory level-mindepth Set minimum directory level-exec Execute the find command assuming the return value is true-ok This parameter has the same effect as specifying-exec, but asks the user before executing the command, and aborts execution if the answer is y or Y

example

First, give the file structure used for the demo:

Example 1: Find all files and folders in the current directory and its subdirectories

find .

Example 2: Find file names ending in.txt in the/testLinux directory

//need to write the full path find /tmp/cg/testLinux -name "*.txt"

Example 3: Combines to find files whose filenames begin with file1 (AND, OR, NOT) file2

/** * Combination search syntax: * -a and (take intersection)* -o or (union)* -not Not (same!) * ! Not (same as not)*/find . -name "file1*" -a -name "file2*"find . -name "file1*" -o -name "file2*"find . -name "file1*" -not -name "file2*"find . -name "file1*" ! -name "file2*"

Example 4: Searching by File Type

/** * Find common files in the current directory and all subdirectories */find . -type f

Example 5: Searching Based on Directory Depth

/** * Limit maximum depth to 3 */find . -maxdepth 3 -type f/** * Limit maximum depth to 2 */find . -maxdepth 2 -type f

Example 6: Searching based on file permissions

/** * Search for files with permissions 777 */find . -type f -perm 777/** * Search for files in.txt format with permissions other than 777 */find . -type f -name "*.txt" ! -perm 777

Example 7: Print all.txt files in the current directory and subdirectories as File: filename with the-exec command.

find . -type f -name "*.txt" -exec printf "File: %s\n" {} \;

Example 8: With the help of the-exec command, copy a copy of all the files in the.txt format 3 days ago in the current directory and subdirectories to the old directory.

find . -type f -mtime +3 -name "*.txt" -exec cp {} old \;

File type parameter list

file type parameter meaning f ordinary file l symbolic link d directory c character device b block device s socket pFifo

List of file size units

file size unit meaning b block (512 bytes) c bytes w word (2 bytes) k kilobytes M megabytes G gigabytes

list of options

option meaning-amin Find files or directories that have been accessed at a specified time in minutes-atime Find files or directories that have been accessed at a specified time in 24 hours-cmin Find files or directories that have been changed at a specified time-ctime Find files or directories that have been changed at a specified time Calculate in 24 hours-anewer Find files or directories whose access time is closer to the current one than the access time of the specified file or directory-cnewer Find files or directories whose change time is closer to the current one than the change time of the specified file or directory-daystart Calculate time from today-depth Start from the deepest subdirectory of the specified directory Find-expty Find files with a file size of 0 Byte or empty directory without any subdirectories or files in the directory-exec Assuming that the return value of the find command is true, execute the command-false Set all return values of the find command to false-fls This parameter has the same effect as specifying the-ls parameter, but saves the result as the specified list file-follow Excludes symbolic links-fprint This parameter has the same effect as specifying the-print parameter, but saves the result as the specified list file-fprint0 This parameter has the same effect as specifying the-print0 parameter. but saves the result as a specified list file-fprintf This parameter has the same effect as specifying the-printf parameter, But saves the results as a specified list File-fstype Finds only files or directories of that file system type-gid Finds files or directories that match the specified group ID-groupFinds files or directories that match the specified group name-help or--helpOnline Help-name Specifies a string as a template for finding files or directories Style-iname This parameter has the same effect as specifying the-name parameter, ignore case differences-ilname This parameter has the same effect as specifying the-lname parameter, but ignores case differences-inum Finds files or directories that match the specified inode number-path Specifies a string as a template style for finding directories-ipath This parameter has the same effect as specifying the-path parameter, but ignores case differences-iregex This parameter has the same effect as specifying the-regexe parameter, ignore case differences-links Find files or directories that match the specified number of hard links-ls Assuming the return value of the find directive is true, list file or directory names to standard output-maxdepth Set maximum directory level-mindepth Set minimum directory level-mmin Find files or directories that have been changed at a specified time, -mount This parameter has the same effect as specifying-xdev-mtime Find files or directories that have changed at the specified time, Units are calculated in 24 hours-newer Find files or directories whose change time is closer to the present than the specified file or directory-nogroupFind files or directories that do not belong to the localhost group ID-noleaf Do not consider directories At least two hard connections must exist-nouser Find files or directories that do not belong to the localhost user ID-ok This parameter has the same effect as specifying-exec, But before executing the command, the user will be asked first. If the answer is y or Y, the command will be abandoned. -perm Find files or directories that match the specified permission value. -print Assuming the return value of the find command is true, the file or directory names will be listed in the standard output. The format is one name per column, and each name is preceded by./. String-print0 Assuming that the return value of the find command is true, the file or directory name is listed on the standard output, in the format that all names are on the same line-printf Assuming that the return value of the find command is true, the file or directory name is listed on the standard output, Format can be specified-prune does not find strings as templates for finding files or directories style-regex specifies strings as templates for finding files or directories style-size finds files that match the specified file size-true sets all return values of the find directive to true-type only finds files that match the specified file type-uid finds files or directories that match the specified user identifier-used Finds files or directories that have been accessed at a specified time since the file or directory was changed, in days-user Finders files or directories with a specified owner name-version or--version Displays version information-xdev Limits scope to antecedent file systems-xtype This parameter has the same effect as specifying the-type parameter, except that it checks for symbolic links. If you still want to have further understanding of related knowledge or read more related articles, please pay attention to the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report