How to Find Files in Linux

Author: Monica Porter
Date Of Creation: 20 March 2021
Update Date: 1 July 2024
Anonim
How to Find Files in Linux | Learning Terminal
Video: How to Find Files in Linux | Learning Terminal

Content

If you don't know how, finding files in Linux can be difficult.Here, the best practice is to use a number of different terminal commands. They are much more powerful than simple search on other operating systems, and when you know how to use them well, you will have complete control of your files.

Steps

Method 1 of 3: Use the "find" command

  1. Find files by name. This is the most basic search method you can do with the command find. The command below will find the file in the current directory and all its subfolders.
    • Use -iname instead of -name to ignore the upper and lower case elements in your queries. Comeinand -name take this factor into account.

  2. Setup to search is started from the root directory. To search across the computer, you can add modifiers / into query. Thanks to that, the command find will recognize and proceed to search all directories from the root directory.
    • You can start searching in a specific directory by replacing the comma / by path, such as / home / pat.
    • You can use . instead / to perform a search only on the current directory and its subdirectories.

  3. Use wildcard characters.* to find all files that contain the parts of the query. Characters * This can be useful for searching for items for which you do not know the full name or when you want to find all files with a certain extension.
    • The above command will return all .conf files in Pat's user (and subdirectories) directory.
    • You can also use it to find all files that contain part of the filename. If, for example, there is a lot of wikiHow-related material, you can search through it all by typing " * wiki *".

  4. Simplify results. It can be difficult to screen when too many results are returned. At this point, use the character | and send the search results to a "less" screening program. You can then browse and filter the results a lot more easily.
  5. Find a specific type of search result. You can use additional characters to get specific types of search results. You can find regular files (f), folder (d), symbolic link (l), Device Drivers (c), and block device (b) with the appropriate modifier.
  6. Filter search results by size. When you have multiple files with the same name and know the file size you're looking for, you can filter the search results by this criterion.
    • The above command will return files 50 MB or more. You can use characters + or - to find files that are larger or smaller. When these characters are not used, the search will return the file with the size is true size required
    • You can filter by bytes (c), kilobyte (k), megabytes (M), gigabyte (G), or 512-byte blocks (b). Note that this section is case sensitive.
  7. Use logical operators to combine the types of refinement searches. You can use operators -and (and), -or (or) and -not (zero) to combine different types of searches.
    • The above command will look for files located in the "calendar" directory that are larger than 200 kilobytes in size and whose name does not contain "2015".
  8. Find files by owner or access. If you're looking for someone's file or a file with certain permissions, you can minimize the scope of the search.
    • The above examples in turn query for certain users, groups, and permissions. You can also omit the filename to get all files of the listed type. Such as find / -perm 777 will return any files with 777 (unlimited) access.
  9. Combine commands to proceed after finding the file. You can mix commands find with other commands to execute these commands on the returned files. Split order find and the second is equal to -exec and end the command line with an accent {} ;
    • The above command combination will find all files with 777 access in the current directory (and subdirectories) and then use the command chmod to change that access to 755.
    advertisement

Method 2 of 3: Use the command "locate"

  1. Feature settings.locate. In general, command locate run faster than command find thanks for working on your database file structure. This feature is not available in all Linux distributions. 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:.
    • In Linux Arch, use the pacman package manager: pacman -Syu mlocate
    • For Gentoo, use emerge: emerge mlocate
  2. Database update.locateyour. Comeinand locate Nothing will be found until its database is built and updated. Even though this task is run automatically every day, you can do it yourself and you will have to do it to get it going locate right.
    • Type sudo updatedb and press ↵ Enter.
  3. Use.locateto execute simple queries. Though fast, but command locate There are not as many options as commands find. Executing the basic search in this command is very similar to the basic search used in the command find.
    • The above command looks for the file with the extension .webp on the entire system. Repserentative character * takes the same role as in the command find.
    • Like commands find, -i do not consider the capitalization factor, the lower case in your query.
  4. Limit search results. If the search returns too many results, you can narrow them down using options -n, followed by the number of results you want to display.
    • Only the first 20 search results that match the query will be displayed.
    • You can also use accents | send results to less for easier browsing.
    advertisement

Method 3 of 3: Find the file that contains certain text

  1. Use commands.grep to find files that contain certain text content. To find files that contain certain phrases or strings, you can use the command grep. Comeinand grep Basic format is as follows:
    • -r establishes a search "recursive", meaning that any file containing the keyword in the current directory and all its subfolders will be searched.
    • -i indicates that the above query is not case sensitive. If you want to be case sensitive, omit the operator -i.
  2. Remove textual content. When executing a search grep With the same structure as above, you will get results that include filename and highlighted text matching the query content. You can hide this matching text, displaying only the file name and path, by adding the following to the command:
  3. Hide error messages. Comeinand grep error returns when trying to access a directory without the correct permissions or performing a search for an empty directory. You can send error message to / dev / null to hide at output. advertisement