How to search files or directories under CentOS


Search for files or directories:

The search for files is great! Because we often need to know where that file is, let’s talk about how to search it! There are also quite excellent ones under Linux

Search system! Usually find is not very common! Because the speed is slow, but also very hard disk! Usually we use where is or locate to check, if

I really can’t find it. I just search it with find! Why? Because where is and locate use databases to search for data, they are quite fast, and they do not

There is a real hard disk search, which saves time!

Which view the location of the executable file

Where is where to view files

Locate to cooperate with the database to view the file location

Find to actually search the hard disk to query the file name

• which


[root @ test / root] ා which [filename]

Parameter Description:


[root @test /root]# which passwd


Note: the basic function of which is to search for executable files in the path by using the content of path, the environment variable, so the basic function is to search for

Executive file!

• whereis


[root @ test / root] ා where is [- bmsu] [directory name]

Parameter Description:

-B: only binary files can be found

-M: only files found under the manual path of the instruction file

-S: only source files can be found

-U: no description file!


[root @test /root]# whereis passwd

passwd: /usr/bin/passwd /etc/passwd /usr/share/man/man1/passwd.1.bz2

List the files or directories related to passwd!

[root @test /root]# whereis -b passwd

passwd: /usr/bin/passwd /etc/passwd

Only binary files are listed!

[root @test /root]# whereis -m passwd

passwd: /usr/share/man/man1/passwd.1.bz2

Search only the directory where man page is located!


If find is too cumbersome, and time is spent a lot! (because if your hard disk is older, hehe! Some wait!) This time

Where is quite easy to use! In addition, where is can add parameters to find relevant data, for example, if you are looking for an executable (binary)

Then add – B! For example, the above example is for passwd! If you don’t add any parameters, all the numbers will be

According to the list! So what kind of Dong is used by where is? Why is the search so much faster than find?! In fact, that’s nothing! This is

Because the Linux system will record all the files in the system in a database file. When you use “where is” or “locate” below,

The content of the database file will prevail. Therefore, sometimes you will find the files that have been killed when using these two executables! This is

Because he is a “database” file! In addition, Linux will update dB (that is, the database) for Linux hosts every day

File!) You can find the related mechanism in the file / etc / cron.weekly/slocate.cron! Of course, it can also be used directly

/Usr / bin / updatedb to update database files!

• locate


[root @ test / root] ා locate [directory name]

Parameter Description:


[root @test /root]# locate root

… A lot of files about root have come [email protected][email protected]

[root @ test / root] ා updatedb < = = update database now


It’s easier to use locate! Just type in the file name you want to find! However, there are still restrictions on the use of this thing! Why? you

It will be found that using locate to find data is particularly fast, because the data that locate looks for is from the “established database / var / lib / slocate”

The data in it is found, so you don’t need to access the data directly in the hard disk. Ha ha! Very fast, of course! So what are the limitations? That is why

Because it is searched through the database, and the database is set up once a week by default, so when you create a new file, it still counts

Search for the file before updating the database, then locate will tell you, “no! Ha ha! Because the database must be updated!

So what databases should I build? Do you want to create all of them? It doesn’t seem to be necessary. At this time, you can choose to set up the archive database by yourself

The directory of! It can be set in / etc / updatedb.conf!

[root @test root]# more /etc/updatedb.conf

## Linux-Mandrake configuration.

#Where to start to query data, of course, from the root directory! So fill in / OK


#Which directory do not want to create file search data, ha ha! That’s the data you don’t want!

#Forget, remember to go back to the previous chapter to see the content, we know why so set!


#Safety level

#0 means to close the safety inspection, which is faster but less safe;

#Safe and easy startup, which is the default value of the system; security = “1”

#Do you want screen output when querying? Of course not, or how much trouble?


#Where the database is located! This is the default value, do not move at will, otherwise it will not be found!


#What file systems do we not search for and build? It’s CD-ROM, non Linux file format,

#We don’t need to build search information!


• find


[root @ test / root] ා find [path] [parameter]

Parameter Description:

1. time:

-Atime n: list the files accessed within n * 24 hours!

-CTime n: changed within n * 24 hours, i.e. changed, new files or directories printed

-Mtime n: printed by modified file within n * 24 hours

-New file: new files are listed!

2. Use name:

-GID n: find the file with group ID n

-Group name: find the file with group name name

-Uid n: find the file with owner ID n

-User name: find the file with user name

-Name file: find the name of the file named file (you can use multi characters)

-Type type: find the file whose file attribute is type. Type contains B, C, D, P, l, S,

These are the same attributes as in the previous chapter! For example, l means link and D means path!


[root @ test / root] ා find / – name testing < = = search file name is testing

[root @ test / root] ා find / – name ‘test *’, find the file name containing test!

[root @ test / root] ා find. – CTime 1  CTime 1  CTime 1  CTime 1  CTime 1  CTime 1  CTime 1  CTime 1  CTime 1  CTime 1  CTime 1  CTime 1 

[root @ test / root] ා find / home / test – newer. Bashrc < = = find new files in the / home / test directory that are newer than. Bashrc

[root @ test / root] ා find / home – user test < = = find the file with test as the owner under / home

[root @test /root]
			# find /dev -type b
< = = find the file whose file attribute is B in the directory / dev


If you are looking for a file, find is a good idea! It can search files according to different parameters!

For example, if you want to find a file named httpd.conf, you know it should be under / etc, then you can use “find / etc – name”

Httpd. Conf! What if you remember that a file name contains httpd, but you don’t know the full name?! Ha ha, just use the universal characters,

As above: “find / etc – name ‘* httpd *'” can list all files with httpd in the file name! However, because find is looking for data

After quite a hard disk! So don’t use find for nothing! There are better instructions to replace yo! That’s where is and locate!!