3 Ways to Find a File in Linux

Table of contents:

3 Ways to Find a File in Linux
3 Ways to Find a File in Linux
Anonim

Finding a file inside a Linux system can be a difficult process when you don't know how to do it. The best way to locate content is to use some system commands. Learning to use these tools to their fullest will give you full control of your files, allowing them to prove far more powerful and effective than the simple search capabilities implemented in other operating systems.

Steps

Method 1 of 3: Using the "find" Command

690519 1
690519 1

Step 1. Search for a file based on its name

This is the simplest search system you can perform with the find command. The example command shown below searches for the indicated content within the current directory and all subfolders.

find -iname "filename"

Using the -iname parameter instead of the -name one will ignore the differences between uppercase and lowercase letters. Therefore, remember that using the -name parameter you will perform a "case-sensitive" search (that is, the exact name of the indicated file will be searched)

690519 2
690519 2

Step 2. Configure the search to start in the "root" directory

If you want to search the entire system, add the prefix / to your search string. In this way you will tell the find command to search for the element indicated in all the directories present in the system, starting from the main one.

find / -iname "filename"

  • You can start the search from a specific folder by replacing the prefix / with the path of the directory in question, for example / home / pat.
  • To limit the search within the current directory and all its subfolders, use the prefix. rather than /.
690519 3
690519 3

Step 3. Use the special character

* to locate all items that match the partial search string you provided. The special character * is very useful in all searches where you do not know the exact name of the element to be found, or to search for content characterized by a specific extension.

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

  • This command shows the list of all files with the extension ".conf" present in the user's "Pat" folder (including all subfolders).
  • You can also use it to find any element whose name or portion of the name matches the search string used. For example, if you have a large number of documents that contain the word wikiHow in their name, you can locate them all using the following search string "* wiki *".
690519 4
690519 4

Step 4. Simplify the management of search results

If you've gotten a large number of results, managing them effectively may be difficult. Use the special character | and the "less" parameter. This command makes it easier to browse and filter the results.

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

690519 5
690519 5

Step 5. Identify a specific type of results

You can use specific parameters to get only a certain set of results. You can search for files (f), directories (d), symbolic links (l), character devices (c), and block devices (b) using their parameter.

find / -type f -iname "filename"

690519 6
690519 6

Step 6. Filter the search results by size

If you need to search through many similarly named files, but know the size of what you are looking for, you can filter your results based on this information.

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

  • This command only displays results whose size equals or exceeds 50MB. To include results that are larger or smaller than indicated, you can use the + or - parameters. Omitting the + or - symbol will search for files that are exactly the specified size.
  • You can filter your search by bytes (c), kilobytes (k), megabytes (M), gigabytes (G), or blocks of 512 bytes (b). Note that this type of indicators are case-sensitive.
690519 7
690519 7

Step 7. Use Boolean operators to refine your search

To combine multiple search criteria together, you can use the -and, -or and -not operators.

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

This command searches for those files in the "travelphotos" folder that are larger than 200 kB and do not have the string "2015" in their name

690519 8
690519 8

Step 8. Search for files based on owner or read and write permissions

If you need to search for a specific file created by a particular user or that has a specific set of permissions, you can do so by performing a targeted search.

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

The example commands search in order based on the user, group, or permissions of the indicated file. If you want to get the complete list of all items that match the type you are looking for, you can also omit the filename. For example, the find / -perm 777 command will show the complete list of all files that have an access permission 777 (ie that can be viewed and edited by anyone)

690519 9
690519 9

Step 9. When your search gets an exact match, combine it with other commands to perform specific actions

You can combine the find command with other commands so that, once the target file is found, specific actions are performed. To separate the find command from the second command, use the -exec parameter, then end the string with the character sequence {};.

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

This example command searches for all files in the current directory (including all subfolders) that have an access permission of 777. Then, for each of the files found, the chmod command will be run to set the new passcode to 755

Method 2 of 3: Using the "locate" Command

690519 10
690519 10

Step 1. Install the feature

locate.

Normally the locate command runs faster than the find command because it doesn't use the database related to your file structure. Not all Linux distributions come with the locate command pre-installed. If this is the case for you, follow these instructions to try to install it:

  • Type the command sudo apt-get update and press the Enter key.
  • Type the command sudo apt-get install mlocate and press the Enter key. If the locate command is already installed, you will see the following message mlocate is already the newest version.
  • In Arch Linux, use the pacman package manager: pacman -Syu mlocate
  • For Gentoo, use emerge: emerge mlocate
690519 11
690519 11

Step 2. Update the command database

locate.

Until the database of the locate command is created and populated with system information, it is unusable. This is done automatically every day, but you can also update manually. If you want to start using the locate command immediately, you need to perform the update procedure yourself.

Type the command sudo updatedb and press the Enter key

690519 12
690519 12

Step 3. Use the command

locate just to perform simple searches.

The locate command is very fast, but it does not have all the search capabilities provided by the find command. You can perform simple file searches in much the same way as they are done with the find command.

locate -i "*.jpg"

  • This command searches for all files with the-j.webp" />
  • As in the find command, the -i parameter ignores uppercase and lowercase letters in the string to be searched.
690519 13
690519 13

Step 4. Limit the result set

If your search has too many hits, you can reduce its size by using the -n parameter followed by the number of items you want to appear.

locate -n 20 -i "*.jpg"

  • In this case, only the first 20 results that meet the criteria specified in the search will be shown.
  • You can also use the special character | to use the less parameter and consult the results list more easily and efficiently.

Method 3 of 3: Search for Text Within Files

690519 14
690519 14

Step 1. To search for text strings within files, use the command

grep.

If you are looking for a specific text file, which contains a certain phrase or character string, you can use the grep command. The syntax of a simple grep command is as follows

grep -r -i "search string" / path / where / to search /

  • The -r parameter sets a "recursive" search, ie the indicated text will be searched within all the files present in the current folder and in all subfolders.
  • The -i parameter indicates that the specified search string is not case-sensitive. If you want to do a case-sensitive search, just omit the -i operator.
690519 15
690519 15

Step 2. Delete the additional text from the search results

When you perform a search such as the example, the grep command displays the name of the found file as a result, followed by the highlighted text that matches the specified search string. To hide this last information and thus show only the names of the files found and the relative path, use the following command:

grep -r -i "search string" / path / where / to search / | cut -d: -f1

690519 16
690519 16

Step 3. Hide error messages

The grep command displays an error message when it cannot access a particular directory due to lack of necessary permissions, or if it is an empty folder. To prevent this error message from appearing on the screen, you can redirect it to the / dev / null device.

grep -r -i "search string" / path / where to / search / 2> / dev / null

Recommended: