Linux file management knowledge: find files

The previous articles introduced the LINUX process management control commands and the knowledge system at the network level. As we all know, a Linux system is composed of many files. Since there are so many files, how should we manage these files?

All data in Linux exists in the form of files, so all files are classified into different file systems. The file system is a tree structure, which is commonly understood as a directory.

In the Linux system, every operation you do and every command you execute is presented based on the underlying logic of the file system.

When it comes to managing linux files, you have to find it first! Through this section, you can understand the first step of Linux file management: find files, etc. Today's article mainly introduces the next two tools or command programs used to find files in the Linux system.

• locate – find files by name

• find – search for files in a directory hierarchy

locate - Simple way to find files

The locate command program performs a quick pathname database search process and outputs pathnames that match the given string. The locate command program can only find files by file name.

For example, to find all files whose names start with "zip":

[root@linuxprobe ~]$ locate bin/zip

The locate command program performs a search pathname database process and outputs any pathname that contains the string "bin/zip":

/usr/bin/zip

/usr/bin/zipgrep

/usr/bin/zipinfo

/usr/bin/zipsplit

Or combine with other command tools, such as the grep command, to perform a more comprehensive search:

[root@linuxprobe ~]$ locate zip | grep bin

/bin/bunzip2

/bin/bzip2

/bin/gunzip

/bin/gzip

/usr/bin/funzip

/usr/bin/gpg-zip

/usr/bin/prezip

/usr/bin/prezip-bin

/usr/bin/unzip

/usr/bin/zip

/usr/bin/zipgrep

/usr/bin/zipinfo

/usr/bin/zipsplit

Extended knowledge:

Sometimes the locate command program does not work properly, but then the next day it works normally. Solution: first change to the super user status, run the updatedb command at the prompt, you can manually run the updatedb command program.

At present, there are many evolutions of the locate command program in different versions of Linux distributions, but they all have a certain overlapping set of options. You can check the manual of the locate command to determine which version of the locate command program is installed.

find - the high-level way to find files

The find command program supports searching to find files based on various attributes given. The beauty of the find command program is its ability to find files that match certain criteria properties.

  1. The easiest way to find it:

The find command program supports receiving multiple directory names to perform search lookups.

For example:

[root@linuxprobe ~]$ find ~ // output the path name list of the home directory

As you can imagine, the search results will produce a large list. Then we can try to use the wc command program to calculate and display the number of files:

[root@linuxprobe ~]$ find ~ | wc

-l46052

Common file type test conditions supported by the find command program are as follows:

Table: find file types

file type

describe

b

block special device file

c

character special device file

d

Table of contents

f

normal file

l

symbolic link

  1. Search by file size and file name to find

For example:

Find all common files whose filename matches the wildcard pattern "*.JPG" and whose file size is greater than 1M:

[root@linuxprobe ~]$ find ~ -type f -name "*.JPG" -size +1M | wc -l

237

//Introduce the -name test condition followed by a wildcard pattern.

//The role of double quotes is to prevent the shell from expanding the path name process.

// Introduce the -size test condition followed by the string "+1M". The plus sign "+" means that you are looking for files whose file size is greater than the specified number. If it starts with a minus sign "-", it means to find files smaller than the specified number. No sign means "exactly match this number". The letter "M" indicates that the unit of measurement is megabytes. The units of measurement are specified in the following table:

Table: find size units

character

unit

b

512 byte blocks. This is the default if no units are specified.

c

byte

w

two-byte word

k

Kilobytes (1024 byte units)

M

Megabytes (1048576 byte units)

G

Gigabytes (1073741824 byte units)

The above list is just the tip of the iceberg, and the find command manual has more detailed instructions.

Summarize:

The find command program searches a directory tree to find a file or set of files. It walks the directory tree and reports all occurrences of files matching the user specification. Finder includes a very powerful search function.

The locate program scans one or more filename databases and displays any matches. This can be used as a very fast find command if the file existed during the last filename database update.

Of course, the linux file type is an integral part. In Linux, there are three basic file types:

1) Ordinary documents

Ordinary files basically include text files, source code files, executable files, etc.

2) Directory

Directories can contain ordinary files and special files, and directories are equivalent to folders in Windows and Mac systems.

3) Device file

Device files are files necessary for Linux to communicate with external devices (such as CD-ROMs, printers, etc.).

The charm of using linux system file search commands is more than that. Today’s introduction is only a small part, so I strongly suggest that you can refer to the two books "GNU Findutils 4.9.0" and "This is how you should learn linux". For linux systems The introduction of file management level is very detailed!

 

Guess you like

Origin blog.csdn.net/yaxuan88521/article/details/132099468