Home » Categories » Linux Command Line

How To Find a File In Linux From the Command Line

Locate Linux Files by Their Name or Extension

Type find into the command line to track down a particular file by its name or extension. If you want to look for *.err files in the /home/username/ directory and all sub-directories, try this: 
 
find /home/username/ -name "*.err"

Typical Linux Find Commands and Syntax

find command expressions look like this:

find command options starting/path expression

Let’s break down a Linux find command where we don’t just want Linux find file by name:

find -O3 -L /var/www/ -name "*.html"

It enables the top-level optimization (-O3) and permits find to follow symbolic links (-L). The find command in Linux searches through the whole directory hierarchy under /var/www/ for files that have .html on the end.

Basic Examples

1. find . -name thisfile.txt

If you need to know how to find a file in Linux called thisfile.txt, it will look for it in current and sub-directories.

2. find /home -name *.jpg

Look for all .jpg files in the /home and directories below it.

3. find . -type f -empty

Look for an empty file inside the current directory.

4. find /home -user randomperson-mtime 6 -iname ".db"

Look for all .db files (ignoring text case) that have been changed in the preceding 6 days by a user called randomperson.

Options and Optimization for Find Command for Linux

find is configured to ignore symbolic links (shortcut files) by default. If you’d like the find command to follow and show symbolic links, just add the -L option to the command, as we did in this example.

find can help Linux find file by name. The Linux find command enhances its approach to filtering so that performance is optimised. The user can find a file in Linux by selecting three stages of optimisation-O1, -O2, and -O3. -O1 is the standard setting and it causes find to filter according to filename before it runs any other tests.

-O2 filters by name and type of file before carrying on with more demanding filters to find a file in Linux. Level -O3 reorders all tests according to their relative expense and how likely they are to succeed.

  • -O1 – (Default) filter based on file name first
  • -O2 – File name first, then file-type
  • -O3 – Allow find to automatically re-order the search based on efficient use of resources and likelihood of success
  • -maxdepth X – Search this directory along with all sub-directories to a level of X
  • -iname – Search while ignoring text case.
  • -not – Only produce results that don’t match the test case
  • -type f – Look for files
  • -type d – Look for directories

 

Article Rating (No Votes)
Rate this article
  • Icon PDFExport to PDF
  • Icon MS-WordExport to MS Word
 
Attachments Attachments
There are no attachments for this article.
Comments Comments
There are no comments for this article. Be the first to post a comment.
Related Articles RSS Feed
How to remove all files starting with a certain string in Linux
Viewed 769 times since Wed, Feb 1, 2023
Internet Speed Test in Linux
Viewed 388 times since Thu, Oct 8, 2020
How do I find all files containing specific text on Linux?
Viewed 347 times since Mon, Jan 17, 2022
How To Extract .tar.gz Files using Linux Command Line
Viewed 278 times since Sun, Sep 6, 2020
How to Exclude Specific Packages from Yum Update #2
Viewed 640 times since Wed, Nov 8, 2023
’Argument list too long’ error while copying a large number of files
Viewed 346 times since Tue, Sep 1, 2020
Add cron jobs directly to Scheduled Cron Jobs
Viewed 328 times since Tue, Jul 19, 2022
How to Get the Size of a Directory in Linux
Viewed 366 times since Tue, Sep 1, 2020
Tell yum to ignore a single dependency - nodeps
Viewed 837 times since Thu, Feb 2, 2023