How to find a file in Linux

Author: Ellen Moore
Date Of Creation: 17 January 2021
Update Date: 29 June 2024
Anonim
How to Find Files in Linux | Learning Terminal
Video: How to Find Files in Linux | Learning Terminal

Content

Finding a file on a Linux system is quite difficult if you don't know how to do it. It is best to use different commands that are entered in the Terminal. Having mastered such commands, you will have complete control over the files; also, these commands are more functional than similar search engines in other operating systems.

Steps

Method 1 of 3: The find utility

  1. 1 Find the file by its name. Such a simple search is performed using the utility find... The command below will search for a file in the current directory and all of its subdirectories.

    find -iname "filename"

    • Enter -iname instead of -nameto ignore case in the entered filename. Team -name is case sensitive.
  2. 2 Start searching in the root directory. To start a system-wide search, add the modifier to the query /... In this case, the command find will search for the file in all directories starting from the root.

    find / -iname "filename"

    • You can start searching in a specific directory; to do this, replace / to a directory path, for example / home / max.
    • Can be used . instead of /to search for the file only in the current directory and its subdirectories.
  3. 3 Use a generalization symbol.*to find the files whose name matches the part of the request. Using the generalization symbol * you can find a file whose full name is unknown, or you can find all files with a specific extension.

    find / home / max -iname " *. conf"

    • This command will find all files with a .conf extension in the Max user folder (and its subfolders).
    • Use this command to find all files that match part of the query. For example, if you have a lot of WikiHow-related files on your computer, find all the files by typing " * wiki *".
  4. 4 Make it easier to manage search results. If there are too many search results, it is difficult to find the file you are looking for. Use the symbol |to filter the search results with less. This will make it easier to view and filter your search results.

    find / home / max -iname " *. conf" | less

  5. 5 Find specific items. Use modifiers to show only specific items in search results. You can search for regular files (f), directories (d), symbolic links (l), character I / O devices (with) and block devices (b).

    find / -type f -iname "filename"

  6. 6 Filter your search results by file size. If you have many files with similar names on your computer, but you know the size of the file you are looking for, filter the search results by file size.

    find / -size + 50M -iname "filename"

    • This command will find all files larger than 50 MB. Use a modifier + or -to indicate an increase or decrease in size. If the modifier + or - no, the command will find files that are exactly the same size as the specified size.
    • Search results can be filtered by bytes (c), kilobytes (k), megabytes (M), gigabytes (G) or blocks of 512 bytes (b). Note that the modifiers shown are case sensitive.
  7. 7 Use logical operators (boolean operators) to combine search filters. Operators can be used -and, -or, -notto combine different search terms into a single query.

    find / travelphotos -type f -size + 200k -not -iname " * 2015 *"

    • This command will find files in the "Travelphotos" folder that are larger than 200 KB and which do not have the number 2015 in their names.
  8. 8 Find files by owner or permissions. If you need to find a file owned by a specific user or a file with specific access rights, you can narrow your search.

    find / -user max -iname "filename" find / -group users -iname "filename" find / -perm 777 -iname "filename"

    • The above commands will find the file for a specific user, group, or with specific access rights. You can also omit the file name in the query to find all files that match the specified criteria. For example, the command find / -perm 777 will find all files with permissions 777 (unlimited).
  9. 9 Combine the commands to perform specific actions after the file search is complete. The team find can be combined with other commands that will process the found files. To do this, between the team find and with the second command enter -execand at the end of the line enter {} ;

    find. -type f -perm 777 -exec chmod 755 {} ;

    • This command will find in the current directory (and its subdirectories) all files with permissions 777, and then using the command chmod the access rights will change to 755.

Method 2 of 3: locate utility

  1. 1 Install the utility.locate... This utility is faster than utility findbecause it doesn't really scan the file system. However, not all Linux distributions come with the utility. locateso enter the following commands to install it:
    • Enter sudo apt-get update and press ↵ Enter.
    • On Debian and Ubuntu, do the following: enter sudo apt-get install mlocate and press ↵ Enter... If locate already installed, a message is displayed mlocate is already the newest version (Latest version installed).
    • On Arch Linux, use the pacman package manager: pacman -Syu mlocate
    • On Gentoo, use emerge: emerge mlocate
  2. 2 Update the utility database.locate... This utility will not be able to find anything without a previously created and updated database (which stores a semblance of a snapshot from the file system). The database is updated daily in automatic mode, but it can be done manually. Update the database manually to immediately start working with locate.
    • Enter sudo updatedb and press ↵ Enter.
  3. 3 Use.locateto perform simple searches. Utility locate works quickly, but it is not as functional as the utility find... Team locate handles simple search queries similarly to the command find.

    locate -i " *. jpg"

    • This command will find (in the whole system) all files with the extension .webp... Here the generalization symbol * works the same as the team find.
    • Like the team find, modifier -i ignores the case of the search term.
  4. .
  5. 4 Limit the number of search results. If there are too many search results, shorten them with the modifier -n and a number that determines the number of search results displayed.

    locate -n 20 -i " *. jpg"

    • This command will display the first 20 results that match your search term.
    • You can also use the symbol |to filter the search results with less. This will make it easier to see the search results.

Method 3 of 3: Find text in files

  1. 1 Use the command.grepto search for text in files. Do this to find a file that contains a specific phrase or line. Basic command format grep as follows:

    grep -r -i "search query" / path / to / directory /

    • Modifier -r makes the search recursive, so any file that contains a string from the search term will be found in the current directory (and all subdirectories).
    • Modifier -i indicates that the request is not case-sensitive. To be case sensitive, do not enter the modifier -i.
  2. 2 Hide excess text. When executing the command grep (as described above) the screen will display the file name and text with the highlighted phrase or string specified in the search query. You can hide such text to display only the file name and path. To do this, enter the following command:

    grep -r -i "search query" / path / to / directory / | cut -d: -f1

  3. 3 Hide error messages. Team grep displays an error message if it tries to access folders without proper permissions or ends up in empty folders. Such messages can be sent to / dev / null so that they do not appear on the screen.

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