Title | Find Files in Linux, Using the Command Line Linode |
---|---|
Author | He He |
Course | Introduction to Sociocultural Anthropology |
Institution | Hawassa University |
Pages | 4 |
File Size | 220.5 KB |
File Type | |
Total Downloads | 2 |
Total Views | 153 |
An Access Control List (ACL) is a list of rules that control and filter traffic based on source and destination IP addresses or Port numbers. This happens by either allowing packets or blocking packets from an interface on a router, switch, firewall etc....
3/23/2020
Find Files in Linux, Using the Command Line | Linode
(https://linode.com)
Guides & Tutorials (https://www.linode.com/docs/) » Tools & Reference (https://www.linode.com/docs/tools-reference/) » Tools (https://www.linode.com/docs/tools-reference/tools/) » Find Files in Linux, Using the Command Line
Find Files in Linux, Using the Command Line Updated Monday, December 30, 2019 by Edward Angert
Written by Linode
Try this guide (https://login.linode.com/signup?promo=DOCS20AA00X1) to receive $20 at signup on a new account. Sign Up Here!
Contribute on GitHub Report an Issue (https://github.com/linode/docs/issues/new? title=Find%20Files%20in%20Linux%2c%20Using%20the%20Command%20Line%20Proposed%20Changes&body=Link%3A https%3A%2F%2Flinode.com%2fdocs%2ftoolsreference%2ftools%2ffind-files-in-linux-using-the-command-line%2f%0A%23%23%20Issue%0A%0A%23%23%20Suggested%20Fix%0A&labels=inaccurate guide) | View File (https://github.com/linode/docs/blob/master/docs/tools-reference/tools/find-files-in-linux-using-the-command-line/index.md) | Edit File (https://github.com/linode/docs/edit/develop/docs/toolsreference/tools/find-files-in-linux-using-the-command-line/index.md)
find is a command for recursively filtering objects in the file system based on a simple conditional mechanism. Use find to search for a file or directory on your file system. Using the -exec flag, files can be found and immediately processed within the same command.
Find Linux Files by Name or Extension Use
find
from the command line to locate a specific file by name or extension. The following example searches for directory and all sub-directories:
*.err
files in the
/home/username/
https://www.linode.com/docs/tools-reference/tools/find-files-in-linux-using-the-command-line/
1/7
3/23/2020
Find Files in Linux, Using the Command Line | Linode
find /home/username/ -name "*.err"
Common Linux Find Commands and Syntax find
expressions take the following form:
find options starting/path expression
The
options
attribute will control the behavior and optimization method of the
The
starting/path
The
expression
attribute will define the top level directory where
find
find
process.
begins filtering.
attribute controls the tests that search the directory hierarchy to produce output.
Consider the following example command: find -O3 -L /var/www/ -name "*.html"
This command enables the maximum optimization level (-O3) and allows directory tree beneath /var/www/ for files that end with .html .
find
to follow symbolic links ( -L ).
find
searches the entire
Basic Examples Command
Description
find . -name testfile.txt
Find a file called testfile.txt in current and sub-directories.
find /home -name *.jpg
Find all .jpg files in the /home and sub-directories.
find . -type f -empty
Find an empty file within the current directory.
find /home -user exampleuser -mtime -7 -iname
Find all .db files (ignoring text case) modified in the last 7 days by a user named exampleuser.
".db"
Options and Optimization for Find The default configuration for the
-L
find
The
find
will ignore symbolic links (shortcut files). If you want
find
to follow and return symbolic links, you can add
option to the command, as shown in the example above.
optimizes its filtering strategy to increase performance. Three user-selectable optimization levels are specified as -O1
optimization is the default and forces
find
-O1
,
-O2
, and
-O3
.
to filter based on filename before running all other tests.
Optimization at the -O2 level prioritizes file name filters, as in resource-intensive conditions. Level -O3 optimization allows their relative expense and the likelihood of their success.
Command
Description
-O1
(Default) filter based on file name first.
-O2
File name first, then file-type.
-O1 find
, and then runs all file-type filtering before proceeding with other more to perform the most severe optimization and reorders all tests based on
https://www.linode.com/docs/tools-reference/tools/find-files-in-linux-using-the-command-line/
2/7
3/23/2020
Find Files in Linux, Using the Command Line | Linode
Command
Description
-O3
Allow find to automatically re-order the search based on efficient use of resources and likelihood. of success
-maxdepth X
Search current directory as well as all sub-directories X levels deep.
-iname
Search without regard for text case.
-not
Return only results that do not match the test case.
-type f
Search for files.
-type d
Search for directories.
Find Files by Modification Time The
find
command contains the ability to filter a directory hierarchy based on when the file was last modified:
find / -name "*conf" -mtime -7 find /home/exampleuser/ -name "*conf" -mtime -3
The first command returns a list of all files in the entire file system that end with the characters The second command filters in the previous 3 days.
exampleuser
conf
and have been modified in the last 7 days.
user’s home directory for files with names that end with the characters
conf
and have been modified
Use Grep to Find Files Based on Content The
find
command is only able to filter the directory hierarchy based on a file’s name and meta data. If you need to search based on the
content of the file, use a tool like grep (/docs/tools-reference/search-and-filter-text-with-grep). Consider the following example: find . -type f -exec grep "example" '{}' \; -print
This searches every object in the current directory hierarchy ( . ) that is a file ( -type f ) and then runs the command grep "example" for every file that satisfies the conditions. The files that match are printed on the screen ( -print ). The curly braces ( {} ) are a placeholder for the find match results. The {} are enclosed in single quotes ( ' ) to avoid handing grep a malformed file name. The -exec command is terminated with a semicolon ( ; ), which should be escaped ( \; ) to avoid interpretation by the shell. Before the implementation of the
-exec
option, this kind of command might have used the
xargs
command to generate a similar output:
find . -type f -print | xargs grep "example"
How to Find and Process Files Using the Find Command The
-exec
option runs commands against every object that matches the find expression. Consider the following example:
find . -name "rc.conf" -exec chmod o+r '{}' \;
This filters every object in the current hierarchy ( . ) for files named find
rc.conf
and runs the
chmod o+r
command to modify file permissions of the
results.
https://www.linode.com/docs/tools-reference/tools/find-files-in-linux-using-the-command-line/
3/7
3/23/2020
Find Files in Linux, Using the Command Line | Linode
The commands run with the
-exec
are executed in the root directory of the
find
process. Use
-execdir
to execute the specified command in
the directory where the match resides. This may alleviate security concerns and produce more desirable performance for some operations. The
or
-exec
execdir
with
-execdir -okdir
options run without further prompts. If you prefer to be prompted before action is taken, replace
-exec
with
-ok
or
-
.
How to Find and Delete Files in the Linux Command Line Caution Use this option with extreme caution.
Add the option
-delete
to the end of a match expression to delete all files that match. Use this option when you are certain that the results
only match the files that you wish to delete. In the following example, example,
find
find
locates all files in the hierarchy starting at the current directory and fully recursing into the directory tree. In this
will delete all files that end with the characters
.bak
:
find . -name "*.bak" -delete
Still have a few questions? Join our Community (https://www.linode.com/community/questions/) and post your questions for other Linode and Linux enthusiasts to help you out. Related Questions: Where are My Files? (https://www.linode.com/community/questions/17057/where-are-my-files) How do I check my server’s log files? (https://www.linode.com/community/questions/295/how-do-i-check-my-servers-log-files) Can I check my files through the Linode Manager? (https://www.linode.com/community/questions/11220/can-i-check-my-files-through-linodemanager)
More Information You may wish to consult the following resources for additional information on this topic. While these are provided in the hope that they will be useful, please note that we cannot vouch for the accuracy or timeliness of externally hosted materials. Online man Pages (http://man7.org/linux/man-pages/man1/find.1.html) ExplainShell (http://explainshell.com/explain/1/find)
Join our Community Find answers, ask questions, and help others. (https://www.linode.com/community/questions/)
Linode Comment Policy
Comments must be respectful, constructive, and relevant to the topic of the guide. No external links or advertisements.
15 Comments Recommend 19
Linode
🔒 Disqus' Privacy Policy
t Tweet
f Share
1
Login
Sort by Best
Join the discussion… https://www.linode.com/docs/tools-reference/tools/find-files-in-linux-using-the-command-line/
4/7...