头图

foreword

Linux has four commonly used find commands: locate , whereis , which and find . This article describes their differences and simple usage.

locate command

This command will check your entire filesystem and find every occurrence of that keyword. So as you can imagine, the results can be unacceptable.

 locate keyword

locate The database used is usually updated daily, so if you're searching for something recently created, it may not come back in your search. you can use

 updatedb

command to manually update the database with locate command.

 locate aircrack-ng

image.png

whereis command

In Linux, executables are called binaries, and if you want to locate a binary, whereis is more efficient than locate .

 whereis binary

This command will return the location of the binary, along with its source code and man pages, if any.

 whereis aircrack-ng

image.png

which command

The PATH variable in Linux holds the directory where the operating system looks for commands you execute on the command line.

 which binary

which command finds a binary in your PATH. If it doesn't find the binary in the current PATH, it returns nothing.

 which aircrack-ng

image.png

These directories usually include /usr/bin , but may also include /usr/sbin and a few others.

find command

The most powerful search command is the find command. You can use it to search in any specified directory, with various parameters.

The basic syntax is:

 find directory options expression

Let's say I have a file called test.txt and I need to find it but I'm not sure exactly in which directory. I can execute the following command to start searching from the top of the / .

 find / -type f -name test.txt

The specific meaning is:

  • / means start searching from the top of the filesystem.
  • -type is the type of thing you're looking for. f表示文件, b设备文件, c设备文件, d表示目录, l represents a symbolic link.
  • -name is the name of the thing you are looking for and the result will match exactly.

image.png

Searching each directory, starting from the top, takes a certain amount of time. We can speed things up by specifying the directory. Suppose I know that the file is in the home directory:

 time find /home -type f -name test.txt

Here I used the time command, so you can see how much time each command takes.

image.png

find command only displays exact name matches. If file.txt has a different extension, it will not be returned. I created another file text.conf and now if I search with just test.txt as the name, I no longer get the test.conf file back.

image.png

We can work around this limitation by using wildcards ( wildcards ). They allow us to match multiple characters and come in several different forms.

Suppose we have a directory with cat, hat, what and bat files:

  • * matches multiple characters. *at will match: cat, hat, what, and bat.
  • ? matches a single character. ?at will match: cat, hat, bat, but not what.
  • [] matches characters that appear inside square brackets. [c, b] will match cat and bat.
 find /home -type f -name test.*

image.png

find Supports a lot of tests, even operators. Let's say we want to find all files with permissions not 0600 and all directories with permissions not 0700.

 find ~ \( -type f -not -perm 0600 \) -or \( -type d -not perm 0700 \)

This command means: find all files whose permissions are not 0600 or all directories whose permissions are not 0700.

  • Look in the ~ directory (home).
  • \( -type f -not -perm 0600) The backslash is an escape for parentheses, which we use here to combine tests and operators into a larger expression. By default, find is evaluated from left to right. -not tells us that if the result is false, the test is a match. -not can be abbreviated with ! . So this part can also be \`( -type f ! -perm 0600)` .
  • -or tells us that if either test is true, it matches. It can be abbreviated as -o .
  • \( -type d -not perm 0700 \) is another test, very similar to the first, except the type is directory.

image.png

find is a powerful command with many tests, so be sure to study it more.

Summarize

That's all for an introduction to finding things in Linux :)


chuck
300 声望41 粉丝