• About
  • Contact
  • Cookie
  • Disclaimer
  • Privacy Policy
  • Change the purpose of use

Tnhelearning.edu.vn - Various useful general information portal

  • Photo
  • Bio
  • How To
  • Tech

How to Find Files in Linux

February 4, 2024 by admin Category: How To

You are viewing the article How to Find Files in Linux  at Tnhelearning.edu.vn you can quickly access the necessary information in the table of contents of the article below.

X

This article is co-authored by a team of editors and trained researchers who confirm the accuracy and completeness of the article.

The wikiHow Content Management team carefully monitors the work of editors to ensure that every article is up to a high standard of quality.

This article has been viewed 2,478 times.

Without knowing how, finding files in Linux can be quite difficult. Here, the best practice is to use several different terminal commands. They are much more powerful than the simple search feature found on other operating systems, and when used well, you gain complete control over your files.

Table of Contents

  • Steps
    • Use the “find” command
    • Use the “locate” command
    • Find files containing certain text

Steps

Use the “find” command

Image titled 690519 1

Image titled 690519 1

{“smallUrl”:”https://www.wikihow.com/images_en/thumb/4/48/690519-1.jpg/v4-728px-690519-1.jpg”,”bigUrl”:”https:// www.wikihow.com/images/thumb/4/48/690519-1.jpg/v4-728px-690519-1.jpg”,”smallWidth”:460,”smallHeight”:345,”bigWidth”:728,” bigHeight”:546,”licensing”:”<div class=”mw-parser-output”></div>”}
Find files by name. This is the most basic search method you can perform with the find command. The command below will find the file in the current directory and all its subdirectories. [1] X Research Source

 find -iname "filename"
  • Use -iname instead of -name to ignore uppercase and lowercase elements in your query. The -name command takes this into account.
Image titled 690519 2

Image titled 690519 2

{“smallUrl”:”https://www.wikihow.com/images_en/thumb/4/4b/690519-2.jpg/v4-728px-690519-2.jpg”,”bigUrl”:”https:// www.wikihow.com/images/thumb/4/4b/690519-2.jpg/v4-728px-690519-2.jpg”,”smallWidth”:460,”smallHeight”:345,”bigWidth”:728,” bigHeight”:546,”licensing”:”<div class=”mw-parser-output”></div>”}
Set to search to be started from the root directory. To search the entire computer, you can add the modifier / to the query. Thanks to that, the find command will recognize and search every directory from the root directory.

 find / -iname "filename"
  • You can start a search in a specific directory by replacing the / sign with a path, such as /home/pat .
  • You can use . instead of / to perform a search only on the current directory and its subdirectories.
Image titled 690519 3

Image titled 690519 3

{“smallUrl”:”https://www.wikihow.com/images_en/thumb/9/9a/690519-3.jpg/v4-728px-690519-3.jpg”,”bigUrl”:”https:// www.wikihow.com/images/thumb/9/9a/690519-3.jpg/v4-728px-690519-3.jpg”,”smallWidth”:460,”smallHeight”:345,”bigWidth”:728,” bigHeight”:546,”licensing”:”<div class=”mw-parser-output”></div>”}
Use wildcard . * to find every file containing parts of the query. This * character can be useful in finding items where you don’t know the full name or when you want to find every file with a certain extension.

 find /home/pat -iname "*.conf"
  • The above command will return all .conf files in Pat’s user directory (and subdirectories).
  • You can also use it to find all files that contain part of a filename. For example, in case there are a lot of wikiHow-related documents, you can find them all by typing "*wiki*" .
READ More:   How to Save a Wet Phone
Image titled 690519 4

Image titled 690519 4

{“smallUrl”:”https://www.wikihow.com/images_en/thumb/3/35/690519-4.jpg/v4-728px-690519-4.jpg”,”bigUrl”:”https:// www.wikihow.com/images/thumb/3/35/690519-4.jpg/v4-728px-690519-4.jpg”,”smallWidth”:460,”smallHeight”:345,”bigWidth”:728,” bigHeight”:546,”licensing”:”<div class=”mw-parser-output”></div>”}
Simplify return results. It can be difficult to filter when so many results are returned. At this point, use the | . character and send the search results to the “less” screening program. You can then browse and filter the results a lot easier.

 find /home/pat -iname "*.conf" | less
Image titled 690519 5

Image titled 690519 5

{“smallUrl”:”https://www.wikihow.com/images_en/thumb/4/46/690519-5.jpg/v4-728px-690519-5.jpg”,”bigUrl”:”https:// www.wikihow.com/images/thumb/4/46/690519-5.jpg/v4-728px-690519-5.jpg”,”smallWidth”:460,”smallHeight”:345,”bigWidth”:728,” bigHeight”:546,”licensing”:”<div class=”mw-parser-output”></div>”}
Find a specific type of search result. You can use modifier characters to get certain types of search results. You can find regular files ( f ), directories ( d ), symbolic links ( l ), control devices ( c ), and block devices ( b ) with the matching modifier character.

 find / -type f -iname "filename" 
Image titled 690519 6

Image titled 690519 6

{“smallUrl”:”https://www.wikihow.com/images_en/thumb/9/90/690519-6.jpg/v4-728px-690519-6.jpg”,”bigUrl”:”https:// www.wikihow.com/images/thumb/9/90/690519-6.jpg/v4-728px-690519-6.jpg”,”smallWidth”:460,”smallHeight”:345,”bigWidth”:728,” bigHeight”:546,”licensing”:”<div class=”mw-parser-output”></div>”}
Filter search results by size. When there are multiple files with the same name and you know the file size you are looking for, you can filter the search results by this criterion.

 find / -size +50M -iname "filename"
  • The above command will return files of 50 MB or more. You can use the + or - characters to find files that are larger or smaller. When these characters are not used, the search will return a file that is exactly the same size as the requested size
  • You can filter by bytes ( c ), kilobytes ( k ), megabytes ( M ), gigabytes ( G ), or 512-byte blocks ( b ). Note that this section is case sensitive.
Image titled 690519 7

Image titled 690519 7

{“smallUrl”:”https://www.wikihow.com/images_en/thumb/7/75/690519-7.jpg/v4-728px-690519-7.jpg”,”bigUrl”:”https:// www.wikihow.com/images/thumb/7/75/690519-7.jpg/v4-728px-690519-7.jpg”,”smallWidth”:460,”smallHeight”:345,”bigWidth”:728,” bigHeight”:546,”licensing”:”<div class=”mw-parser-output”></div>”}
Use logical operators to combine filter search types. You can use the -and (and), -or (or) and -not (zero) operators to combine search types. [2] X Research Source

 find /image_travel_type -type f -size +200k -not -iname "*2015*"
  • The above command will look for files located in the “travel_pictures” folder that are larger than 200 kilobytes and have a name that doesn’t contain “2015”.
Image titled 690519 8

Image titled 690519 8

{“smallUrl”:”https://www.wikihow.com/images_en/thumb/f/f3/690519-8.jpg/v4-728px-690519-8.jpg”,”bigUrl”:”https:// www.wikihow.com/images/thumb/f/f3/690519-8.jpg/v4-728px-690519-8.jpg”,”smallWidth”:460,”smallHeight”:345,”bigWidth”:728,” bigHeight”:546,”licensing”:”<div class=”mw-parser-output”></div>”}
Find files by owner or access. If you’re looking for someone’s files or files with certain permissions, you can narrow down your search.

 find / -user pat -iname "filename"
find / -group users -iname "filename"
find / -perm 777 -iname "filename"
  • The above examples query with certain users, groups, and access rights in turn. You can also omit the filename to get any files of the type stated. Such as find / -perm 777 will return every file with 777 (unlimited) access. [3] X Research Sources
READ More:   How to Clean Plaque on Teeth
Image titled 690519 9

Image titled 690519 9

{“smallUrl”:”https://www.wikihow.com/images_en/thumb/6/61/690519-9.jpg/v4-728px-690519-9.jpg”,”bigUrl”:”https:// www.wikihow.com/images/thumb/6/61/690519-9.jpg/v4-728px-690519-9.jpg”,”smallWidth”:460,”smallHeight”:345,”bigWidth”:728,” bigHeight”:546,”licensing”:”<div class=”mw-parser-output”></div>”}
Combine commands to perform processing after finding the file. You can combine the find command with other commands to execute these commands on the returned files. Separate the find command and the second command with -exec and end the command line with {} ;

 find . -type f -perm 777 -exec chmod 755 {} ;
  • The above command combination will find all files with 777 access in the current directory (and subdirectories) and then use the chmod command to change that access to 755.

Use the “locate” command

Image titled 690519 10

Image titled 690519 10

{“smallUrl”:”https://www.wikihow.com/images_en/thumb/c/c4/690519-10.jpg/v4-728px-690519-10.jpg”,”bigUrl”:”https:// www.wikihow.com/images/thumb/c/c4/690519-10.jpg/v4-728px-690519-10.jpg”,”smallWidth”:460,”smallHeight”:345,”bigWidth”:728,” bigHeight”:546,”licensing”:”<div class=”mw-parser-output”></div>”}
Install feature . locate . In general, the locate command runs faster than the find command by working on your database file structure. Not all Linux distributions have this feature available. Therefore, you need the following commands to try to install them:

  • Type sudo apt-get update and press ↵ Enter .
  • You can install on Debian and Ubuntu by: Type sudo apt-get install mlocate and press ↵ Enter . If locate is already installed, the following message will appear: mlocate is already the newest version .
  • In Linux Arch, use the pacman package manager: pacman -Syu mlocate
  • With Gentoo, use emerge: emerge mlocate
Image titled 690519 11

Image titled 690519 11

{“smallUrl”:”https://www.wikihow.com/images_en/thumb/a/a9/690519-11.jpg/v4-728px-690519-11.jpg”,”bigUrl”:”https:// www.wikihow.com/images/thumb/a/a9/690519-11.jpg/v4-728px-690519-11.jpg”,”smallWidth”:460,”smallHeight”:345,”bigWidth”:728,” bigHeight”:546,”licensing”:”<div class=”mw-parser-output”></div>”}
Update database . locate your. The locate command will not be able to find anything until its database is built and updated. Although this task is run automatically every day, you can still do it yourself and you will have to do so to use locate right away. [4] X Research Sources

  • Type sudo updatedb and press ↵ Enter .
Image titled 690519 12

Image titled 690519 12

{“smallUrl”:”https://www.wikihow.com/images_en/thumb/0/04/690519-12.jpg/v4-728px-690519-12.jpg”,”bigUrl”:”https:// www.wikihow.com/images/thumb/0/04/690519-12.jpg/v4-728px-690519-12.jpg”,”smallWidth”:460,”smallHeight”:345,”bigWidth”:728,” bigHeight”:546,”licensing”:”<div class=”mw-parser-output”></div>”}
Use . locate to execute simple queries. Although fast, the locate command doesn’t have as many options as the find command. The basic search execution in this command is very similar to the basic search in the find command.

 locate -i "*.jpg"
  • The above command looks for .jpg files on the entire system. The wildcard * plays the same role as in the find command.
  • Like the find command, -i does not consider uppercase and lowercase elements in your query.
Image titled 690519 13

Image titled 690519 13

{“smallUrl”:”https://www.wikihow.com/images_en/thumb/3/3c/690519-13.jpg/v4-728px-690519-13.jpg”,”bigUrl”:”https:// www.wikihow.com/images/thumb/3/3c/690519-13.jpg/v4-728px-690519-13.jpg”,”smallWidth”:460,”smallHeight”:345,”bigWidth”:728,” bigHeight”:546,”licensing”:”<div class=”mw-parser-output”></div>”}
Limit search results. If the search returns too many results, you can narrow them down using the -n option, followed by the number of results you want to display.

 locate -n 20 -i "*.jpg"
  • Only the first 20 search results that match the query will be displayed.
  • You can also use | send results to less for easier browsing.
READ More:   How to Clean Electric Kettle

Find files containing certain text

Image titled 690519 14

Image titled 690519 14

{“smallUrl”:”https://www.wikihow.com/images_en/thumb/a/a0/690519-14.jpg/v4-728px-690519-14.jpg”,”bigUrl”:”https:// www.wikihow.com/images/thumb/a/a0/690519-14.jpg/v4-728px-690519-14.jpg”,”smallWidth”:460,”smallHeight”:345,”bigWidth”:728,” bigHeight”:546,”licensing”:”<div class=”mw-parser-output”></div>”}
Use command . grep to find files containing certain textual content. To find a file that contains a certain phrase or string of characters, you can use the grep command. The basic grep command has the following format:

 grep -r -i "search query" /path/to/directory/
  • -r sets a “recursive” search, which means that any files containing the keyword in the current directory and all its subdirectories will be searched.
  • -i indicates that the above query is case insensitive. If you want to be case sensitive, omit the -i operator.
Image titled 690519 15

Image titled 690519 15

{“smallUrl”:”https://www.wikihow.com/images_en/thumb/7/71/690519-15.jpg/v4-728px-690519-15.jpg”,”bigUrl”:”https:// www.wikihow.com/images/thumb/7/71/690519-15.jpg/v4-728px-690519-15.jpg”,”smallWidth”:460,”smallHeight”:345,”bigWidth”:728,” bigHeight”:546,”licensing”:”<div class=”mw-parser-output”></div>”}
Remove text content. When you execute a grep search with the same structure as above, you will get results that include the filename and highlighted text that matches the query content. You can hide this matching text, showing only the name and path of the file by adding the following to the command:

 grep -r -i "search query" /path/to/directory/ | cut -d: -f1
  • Image titled 690519 16

    Image titled 690519 16

    {“smallUrl”:”https://www.wikihow.com/images_en/thumb/0/02/690519-16.jpg/v4-728px-690519-16.jpg”,”bigUrl”:”https:// www.wikihow.com/images/thumb/0/02/690519-16.jpg/v4-728px-690519-16.jpg”,”smallWidth”:460,”smallHeight”:345,”bigWidth”:728,” bigHeight”:546,”licensing”:”<div class=”mw-parser-output”></div>”}
    Hide error messages. The grep command will return an error when trying to access a certain directory without the appropriate permissions or performing a search for an empty directory. You can send an error message to /dev/null to hide at the output. [5] X Research Sources

     grep -r -i "search query" /path/to/directory/ 2 >/dev/null
    
  • X

    This article is co-authored by a team of editors and trained researchers who confirm the accuracy and completeness of the article.

    The wikiHow Content Management team carefully monitors the work of editors to ensure that every article is up to a high standard of quality.

    This article has been viewed 2,478 times.

    Without knowing how, finding files in Linux can be quite difficult. Here, the best practice is to use several different terminal commands. They are much more powerful than the simple search feature found on other operating systems, and when used well, you gain complete control over your files.

    Thank you for reading this post How to Find Files in Linux at Tnhelearning.edu.vn You can comment, see more related articles below and hope to help you with interesting information.

    Related Search:

    Related Posts

    How to Create Curved Text in Photoshop
    How to fall asleep faster
    How to Install FBReader to Read eBooks

    Category: How To

    Previous Post: « What is Clove Oil? Benefits and usage
    Next Post: What is Oil Pulling? What is the most effective way to perform oil pulling? »

    Copyright © 2025 · Tnhelearning.edu.vn - Useful Knowledge