How to find a file in Linux

Table of contents:

How to find a file in Linux
How to find a file in Linux
Anonim

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

690519 1
690519 1

Step 1. Find the file by its name

This simplest search is done using the find utility. The command below will search for a file in the current directory and all of its subdirectories.

find -iname "filename"

Enter -iname instead of -name to ignore case in the entered filename. The -name command is case sensitive

690519 2
690519 2

Step 2. Start searching in the root directory

To run a system-wide search, add the / modifier to your query. In this case, the find command 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 / with the path to the directory, for example, / home / max.
  • Can be used. instead of / to search for the file only in the current directory and its subdirectories.
690519 3
690519 3

Step 3. Use a generalization symbol

* to find files whose name matches the part of the request. You can use the wildcard * character to find a file whose full name is unknown, or to find all files with a specific extension.

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

  • This command will find all.conf files 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 many WikiHow-related files on your computer, find all the files by typing "* wiki *".
690519 4
690519 4

Step 4. Make it easier to manage your search results

If there are too many search results, it is difficult to find the file you are looking for. Use the | character to filter the search results with the less command. This will make it easier to view and filter your search results.

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

690519 5
690519 5

Step 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 (c), and block devices (b).

find / -type f -iname "filename"

690519 6
690519 6

Step 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 the + or - modifier to indicate an increase or decrease in size. If there is no + or - modifier, the command will find files whose size exactly matches the specified size.
  • Search results can be filtered by bytes (c), kilobytes (k), megabytes (M), gigabytes (G), or 512-byte blocks (b). Note that the modifiers shown are case sensitive.
690519 7
690519 7

Step 7. Use logical operators (boolean operators) to combine search filters

You can use the -and, -or, -not operators to combine different searches 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

690519 8
690519 8

Step 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, find / -perm 777 will find all files with permissions 777 (no limit)

690519 9
690519 9

Step 9. Combine the commands to perform specific actions upon completion of the file search

The find command can be combined with other commands that will process the files found. To do this, between the find command and the second command, enter -exec, and at the end of the line, enter {};

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

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

Method 2 of 3: The locate utility

690519 10
690519 10

Step 1. Install the utility

locate. This utility is faster than find because it doesn't actually scan the filesystem. However, not all Linux distributions come with the locate utility, so enter the following commands to install it:

  • Type sudo apt-get update and press ↵ Enter.
  • On Debian and Ubuntu, do the following: type sudo apt-get install mlocate and press ↵ Enter. If locate is already installed, the message mlocate is already the newest version is displayed.
  • On Arch Linux, use the pacman package manager: pacman -Syu mlocate
  • On Gentoo use emerge: emerge mlocate
690519 11
690519 11

Step 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 on a daily basis in automatic mode, but this can be done manually. Update the database manually to start working with locate immediately.

Type sudo updatedb and press ↵ Enter

690519 12
690519 12

Step 3. Use

locate to perform simple searches. The locate utility is fast, but not as powerful as the find utility. The locate command handles simple searches like the find command.

locate -i "*.jpg"

  • This command will find (on the whole system) all files with the extension.jpg. Here the wildcard * works the same as for the find command.
  • Like the find command, the -i modifier ignores the case of the search query.
690519 13
690519 13

Step 4. Limit the number of search results

If there are too many search results, narrow them down by using the -n modifier 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 | character to filter the search results with the less command. This will make it easier to see the search results.

Method 3 of 3: Find text in files

690519 14
690519 14

Step 1. Use the command

grep to search for text in files. Do this to find a file that contains a specific phrase or line. The basic format of the grep command is as follows:

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

  • The -r modifier makes the search recursive, so any file containing a string from the search term will be found in the current directory (and all subdirectories).
  • The -i modifier indicates that the query is not case-sensitive. To be case sensitive, do not enter the -i modifier.
690519 15
690519 15

Step 2. Hide the extra text

When you execute the grep command (as described above), the screen will display the file name and text with the highlighted phrase or line 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

690519 16
690519 16

Step 3. Hide error messages

The grep command 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

Popular by topic