Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The method of quickly finding Files in Linux system

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the relevant knowledge of "the method of quickly finding files in the Linux system". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Among the common Linux commands, there are some commands that can help us find binaries, help manuals or source files, and some commands that can help us find any file on disk. Today we will see how to use these commands.

Which

The which command searches for the location of a system command in the path specified by the PATH variable. For example:

Which-a which # View the location of the command which, and the-a parameter indicates that all / usr/bin/which/bin/which are found.

What does the PATH variable contain? Let's take a look (different computers may be different):

The echo $PATH/home/hyb/bin:/home/hyb/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/usr/lib/jvm/java-8-oracle/bin:/usr/lib/jvm/java-8-oracle/db/bin:/usr/lib/jvm/java-8-oracle/jre/binPATH environment variable stores some path information For example, / usr/bin, when you type a command in the shell terminal, but it is not in the path contained in PATH and is not a built-in command, it will prompt: command not found.

When you have installed a command, but can not find the command when using it, you can check the environment variable to see if there is a path where you installed the command.

So do you understand why some commands or programs need to add environment variables before they can be used directly?

Whereis

The whereis command is used to search the program's binaries, source code files, or help documentation. For example: whereis ls # if there are three of the above, then all three will be displayed. Ls: / bin/ls / usr/share/man/man1/ls.1.gzwhereis-m ls # only look at ls's help manual ls: / usr/share/man/man1/ls.1.gzwhereis-b ls # look only for ls binaries ls: / bin/lswhereis stdio.h # find stdio.h header files, just like the help manual stdio: / usr/include/stdio.h / usr/share/man/man3/stdio.3.gz, it can't find built-in commands.

Type

Type is used to view command types, which generally have the following types:

Alias: alias keyword: keyword builtin: built-in command file: external command and common parameters are as follows:-t output type name, such as file-p if it is an external command, it shows its path-a for external commands, it displays the command path For information such as command types, let's look at a few examples: type ls # ls is an alias ls is aliased to `ls-- color=auto'type cd # cd is a built-in command cd is a shell builtintype findfind is / usr/bin/findtype function # function is a shell keyword function is a shell keywordtype-a which # shows all paths which is / usr/bin/whichwhich is / bin/which

Locate

The commands mentioned above are limited to finding commands, help manuals, or source files, while locate is used to quickly find any file. It does file lookups from a system database without traversing the disk, so it is extremely fast. Usually the system database is updated once a day (you can view the / etc/cron.daily/mlocate of the system, which may vary from system to system).

The common options are as follows:-e only looks for existing files-Q quiet mode, does not show any error message-n shows at most n outputs-r uses the regular expression-I find ignores case-c prints the number of matching results assuming that the following files already exist in the current directory: locate.txt locate.log LOCATE.zip Let's look at some examples. Quick find files locate locate.txt# find locate.txt/home/hyb/workspaces/shell/locate/locate.txt find existing files delete locate.txt# before locate locate.txt# lookup although the file does not exist, it is still found / home/hyb/workspaces/shell/locate/locate.txt locate-e locate.txt#-e parameter can find only existing files (because the file does not exist So it will not be found out) find the number of calculated files locate-c locate.log # only calculate the number of found 1 ignore case search locate-I locate.zip/home/hyb/workspaces/shell/locate/LOCATE.zip use regular expression ordinary search is fuzzy matching, so as long as the target name contains the name to search, will be searched out, but we can use regular expressions to accurately find. Locate-r / locate.log$ # looks for files that end with / locate.log combined with regular expressions. Locate has a richer way to find it, which is not expanded here. One of the problems with locate lookup is that if a file has been deleted recently, it can still find it, but it can't find a file that has been added recently. In other words, its search is not real-time. Of course, we can manually execute the updatedb command to update the database (root permission may be required).

Find

The find command is a powerful lookup command under linux. Compared with the locate command, it needs to traverse disk files, so the search speed is slower, but because of this, its real-time performance is much better than locate. On the other hand, the search conditions of the find command are much richer than those of locate.

The most commonly used condition based on the name is probably the file name, involving the parameter-name,-iname, for example: looking for files starting with sort in the current directory: find. /-name "sort*". / sort4.txt./sort2.txt./sort3.txt./sort.txtfind. /-iname "SORT.txt" # ignore case. / sort.txt is conditional on permissions. Sometimes you need to find files with specific permissions. You can use the-perm parameter, such as finding files in the current directory with permission of 777: find. /-perm 777./test./sort.txt involves the parameter-type depending on the file type For example, to find symbolic link files in the current directory: find. /-type l./testls-al testlrwxrwxrwx 1 hyb hyb 8 November 24 10:10 test-> home.zip the main types are: F ordinary file d directory b block device file c character device file l symbolic link s socket p pipe file subject to file size parameter-size For example: find. /-size 1k # finds files less than 1k in the current directory. / test./sort4.txt./sort2.txt./sort3.txt./test.sh./sort.txtfind-size + 1m # finds files greater than 1m in the current directory. / test.zip commonly used units are: K kilobytes M megabytes G gigabytes c bytes b blocks, generally 512 bytes w word size, two bytes are related to the parameter-user,-nouser under the condition of attribution -group,-nogroup, etc., for example: find. /-user root # find the files of root users in the current directory find. /-nouser # find the deleted files of root users in the current directory-similar usage of group,-nogroup, but only if user groups. With time as the condition, the parameters involved-mtime,-atime,-ctime,-newer,-anewer,-cnewer,-amin,-cmin, etc. For example: find. /-mtime 3 # find files changed 3 days ago find. /-mtime-3 # find files changed within 3 days find. /-mtime 0 # find files changed today find. /-newer sort.txt # find files updated than sort.txt modification time find. /-anewer sort.txt # find files find. /-amin 5 # updated than sort.txt access time File visited 5 minutes ago Note: atime Last access time mtime Last Modification time ctime Last revision time There are many search conditions for the attribute and permission find command, and its usage is also very rich. This article is only a brief introduction, and later articles will introduce some advanced uses of find.

Summary

The which command can be used to find the command location. Whereis can find the location of commands, manuals, source files, etc. Neither which nor whereis can find the location of built-in commands. Locate advantages: find speed block; disadvantages: fuzzy matching, matching path, poor real-time performance. Find advantages: accurate search, rich functions; disadvantages: slow speed. The advanced usage of find will be introduced in a later article. "the method of finding files quickly in the Linux system" ends here. Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report