6 Examples to Find Files in Linux with Find Command

发布时间:2026-02-27 07:33

使用防盗软件,如Apple的Find My或安卓的Find My Device。 #生活技巧# #数码产品使用技巧# #数码产品安全防护#

The Linux find command is a powerful tool that enables system administrators to locate and manage files and directories based on a wide range of search criteria. 

It can find directories and files by their name, their type, or extension, size, permissions, etc.

Table of Contents

Find Command SyntaxHow to Use find Command in LinuxAdvanced options in Find commandFind Files with Name in LinuxFind Files with Permission in LinuxExcluding Files and Directories in Find commandCombine options together in Linux find commandFind Files with User and Group in LinuxFind Files and Directories Based on Date and Time in LinuxFind Files and Directories Based on Size in LinuxFind Files with Not operator in Linux

Find Command Syntax

The general syntax for the find command is as follows:

find [options] [path...] [expression]

The options attribute controls the treatment of the symbolic links, debugging options, and optimization method. The path… attribute defines the starting directory or directories where find will search the files. The expression attribute is made up of options, search patterns, and actions separated by operators.

Find all the files in the /var/log directory that were modified in the last 7 days:

find /var/log -mtime -7

How to Use find Command in Linux

For example, if you want to find all of the files that have the word “file” in their name, you can run the following command:

find . -name '*file*'

This command will search through the current directory and all of its subdirectories for files that have the word “file” in their name.

See also: Mastering the Linux Command Line — Your Complete Free Training Guide

If you want to find all of the files that have the word “file” at the beginning of their name, you can use the following command:

find . -name 'file*'

This command will search through the current directory and all of its subdirectories for files that have the word “file” at the beginning of their name.

Advanced options in Find command

The “find” command also allows you to use advanced search options to filter results.

find command with the “-type” option to search for files of a specific type. find command with the “-mtime” option to search for files that have been modified in a certain amount of time.  find command with the “-maxdepth” option to specify how deep you want to search into directories.

Find all the files in the /var/log directory that were modified in the last 7 days:

find /var/log -mtime -7

Find Files with Name in Linux

To find files with a specific name in Linux, you can use the find command with the -name option. Here’s the basic syntax:

find [path] -name [filename]

Where path is the directory to search, and filename is the name of the file you want to find. Here are some examples:

To find all files named index.html in the current directory and its subdirectories:

find . -name index.html

To find all files named config.json in the /etc directory and its subdirectories:

find /etc -name config.json

To find all files named file.txt in the /home directory and its subdirectories, but only show the filename and not the full path:

find /home -name file.txt -printf "%f\n"

To find all files in the current directory and its subdirectories with a name that starts with file and ends with .txt:

find . -name "file*.txt"

These are just a few examples of how to use the find command to find files with a specific name in Linux. The possibilities are almost endless, as you can use various options to refine your search based on different criteria.

CommandDescriptionfind . -name howtouselinux.txtFind Files Using Name in Current Directoryfind /home -name howtouselinux.txtFind Files Under Home Directoryfind / -type d -name howtouselinuxFind Directories Using Namefind . -type f -name “*.txt”Find all txt Files in Current Directory

Find Files with Permission in Linux

To find files with a specific permission in Linux, you can use the find command with the -perm option. Here’s the basic syntax:

find [path] -perm [permission]

Where path is the directory to search, and permission is the permission code of the file you want to find. The permission code can be specified in either octal or symbolic notation.

Here are some examples:

To find all files in the current directory and its subdirectories with 777 permissions:

find . -type f -perm 0777

To find all files in the /var directory and its subdirectories that do not have 777 permissions:

find /var -type f ! -perm 0777

To find all directories in the /home directory and its subdirectories with 755 permissions:

find /home -type d -perm 0755

CommandDescriptionfind . -type f -perm 0777 -printFind Files With 777 Permissionsfind / -type f ! -perm 777Find Files Without 777 Permissionsfind / -perm 2644Find SGID Files with 644 Permissionsfind / -perm 1551Find Sticky Bit Files with 551 Permissions

Excluding Files and Directories in Find command

You can use the “find” command with the “-exclude” option to exclude certain files from your search.

For example, if you want to find all of the files that have the word “file” in their name, but you want to exclude all of the PDF files, you can run the following command:

find . -name 'file*' -exclude *.pdf

This command will search through the current directory and all of its subdirectories for files that have the word “file” in their name, but it will exclude all of the PDF files.

You can also use the “-exclude-dir” option to exclude certain directories from your search. For example, if you want to find all of the files that have the word “file” in their name, but you want to exclude all of the files in the “tmp” directory, you can run the following command:

find . -name 'file*' -exclude-dir tmp

This command will search through the current directory and all of its subdirectories for files that have the word “file” in their name, but it will exclude all of the files in the “tmp” directory.

Combine options together in Linux find command

You can use multiple options together with the find command to perform a more specific search. Here’s an example that combines several options:

find /home -type f -name "*.txt" -size +10M -user john -mtime -30

This command will find all files with the .txt extension that are larger than 10 megabytes, owned by the user john, and have been modified within the last 30 days, in the /home directory and its subdirectories.

Let’s break down the options used in this command:

OptionDescription-type fSpecifies that we’re only interested in regular files (not directories, symbolic links, etc.)-name “*.txt”Specifies that we’re looking for files with names that end with .txt-size +10MSpecifies that we’re looking for files that are larger than 10 megabytes-user johnSpecifies that we’re only interested in files owned by the user john-mtime -30Specifies that we’re looking for files that have been modified within the last 30 days

By combining these options, we can perform a very specific search for files that meet our criteria. You can also use other options with the find command to further refine your search based on other criteria such as permissions, groups, and access times.

Find Files with User and Group in Linux

CommandDescriptionfind /home -user howtouselinuxFind all Files Based on Userfind /home -group howtouselinuxFind all Files Based on Group

Find Files and Directories Based on Date and Time in Linux

CommandDescriptionfind / -mtime 50Find Last 50 Days Modified Filesfind / -atime 50Find Last 50 Days Accessed Filesfind / -mtime +50 –mtime -100Find Last 50-100 Days Modified Filesfind / -cmin -60Find Changed Files in Last 1 Hour

Find Files and Directories Based on Size in Linux

CommandDescriptionfind / -size 50MFind 50MB Filesfind / -size +50M -size -100MFind Size between 50MB – 100MBfind / -type f -size +100MFind larger than 100MB filesfind / -type f -name *.mp3 -size +10MFind all .mp3 files with more than 10MB

Find Files with Not operator in Linux

CommandDescriptionfind . -type f -not -name “*.html”Find files not ending with the .html file extensionfind . -type f ! -name “*.html”Find files not ending with the .html file extension

Related:

Find File By Name in Linux 20 Advanced Linux Find Command Examples How to use Find Command in Linux 3 Ways to find the largest files in Linux 10 Linux Find Exec examples – Advanced Part

网址:6 Examples to Find Files in Linux with Find Command https://c.klqsh.com/news/view/344075

相关内容

Find your files in Windows
How to Get Help with File Explorer in Windows 11/10
How run clang from command line on Windows?
Get Help With File Explorer in Windows 11 & 10 (Ultimate Guide)
Get Help with File Explorer in Windows 11
How to use file explorer in Windows 11
How to find your lost iPhone or iPad
Linux find 命令
File Explorer in Windows
Use AI Actions in File Explorer in Windows 11

随便看看