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

Introduction to the commands for finding directories and files in Linux

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

This article mainly introduces "the command introduction of finding directories and files in Linux". In the daily operation, I believe that many people have doubts about the command introduction of finding directories and files in Linux. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts of "command introduction of finding directories and files in Linux". Next, please follow the editor to study!

Find

Find path-command parameter [output form]

Parameter description:

Path: tell find where to find what you want

Command parameters: there are many parameters, which will be discussed below.

Output form: there are many output forms,-print,-printf,-print0,-exec,-ok,-ls anyway a lot of read the manual yourself.

Talk about exec.

The-exec find command executes other linux commands given by this parameter on matching files. The corresponding command is in the form of 'command-and' {}\;, notice the space between {} and\;.

-ok and-exec have the same function, but interact with the meeting person. OK will confirm to you whether you want to execute or not before execution.

The main parameters of the find command:

-name looks for files by file name.

-perm looks for files according to file permissions.

-prune uses this option to prevent the find command from looking in the currently specified directory, and if the-depth option is also used, the-prune option will be ignored by the find command.

-user looks for files according to their owners.

-group looks for files by the group to which they belong.

-mtime-n + n finds the file according to the change time of the file,-n indicates that the file change time is within n days from now, and + n indicates that the file change time was n days ago. The find command also has-atime and-ctime options, but they all have the-mtime option

Similar, so we will only introduce the-mtime option here.

-nogroup looks for a file that does not have a valid group, that is, the group to which the file belongs does not exist in / etc/groups.

-nouser looks for a file without a valid owner, that is, the owner of the file does not exist in / etc/passwd.

-newer file1! File2 looks for files whose change time is newer than the file file1 but older than the file file2.

-type looks for a certain type of file, such as:

B-block device file.

D-directory.

C-character device file.

P-pipe file.

L-symbolic link file.

F-ordinary file.

S-socket file

-size n [c] looks for files with n blocks of file length, with c indicating that the file length is in bytes.

When depth looks for files, it first looks for the files in the current directory, and then looks in its subdirectories.

-maxdepth finds the maximum number of directory layers such as 1, that is, only one level of directory is found.

-fstype looks for files in a certain type of file system, which can usually be found in the configuration file

Found in / etc/fstab, the configuration file contains information about the file system in this system.

-mount does not cross the file system mount point when looking for files.

-follow if the find command encounters a symbolic link file, it tracks to the file that the link points to.

-cpio uses the cpio command on matching files to back up the files to the tape device.

Example:

A, look for it by name

The code is as follows:

[zhangy@BlackGhost ~] $find ~-name memcached.pid-print # find the file named memcache.pid in the home directory

/ home/zhangy/memcached/memcached.pid

[zhangy@BlackGhost] $find. -name "* .pid"-print #. Represents the current directory and finds all files ending in pid

. / memcached/memcached.pid

. / .tencent / qq/95219454.pid

[zhangy@BlackGhost] $find ~-name "[0-9] * .pid"-print # finds all pid files that start with a number. Here, [0-9] * does not match 23. It is quite different from the general language class. The * in shell can represent all characters (single, multiple). If you want to match 2345, you can only write [0-9] [0-9] [0-9] [0-9].

/ home/zhangy/.tencent/qq/95219454.pid

[zhangy@BlackGhost ~] $find / home/zhangy/.tencent/-name "[0-9] * .pid"-print # look for the pid file under the .tencent folder

/ home/zhangy/.tencent/qq/95219454.pid

B, find through file permissions

The code is as follows:

[zhangy@BlackGhost css] $find ~-perm 755-print | more # ~ represents the $home directory to find files with permissions of 755

/ home/zhangy/www/css2/c_textshadow.html

/ home/zhangy/www/css2/c_textautospace.html

[zhangy@BlackGhost css] $find. -perm 700-name "uplink *"-print | more # finds all files that begin with production u _ and have permissions of 700

. / css2/u_length_cm.html

. / css2/u_length_px.html

Ignore the catalog to find it.

The code is as follows:

[zhangy@BlackGhost download] $find. -name "* .gz"-prune-o\ (\!-name aaa\)-print # look for files that are in the previous directory, not in the directory ending in aaa, and do not end in gz

.

. / eaccelerator-0.9.5.3.tar

. / fix-crash-in-excerpts.patch

. / AddFeed_Widget_WordPress_Plugin.zip

. / jQuery china-addthis plugin 1.07.rar

D, find the file according to the file type

The code is as follows:

[zhangy@BlackGhost download] $find. -type d-print # find the directory under the current directory

.

. / ddd

[zhangy@BlackGhost download] $find. !-type d-print # find the non-directory files under the current directory

. / eaccelerator-0.9.5.3.tar

. / haproxy-1.3.15.7.tar.gz

. / fix-crash-in-excerpts.patch

E, find the file according to the user and user group to which the file belongs

The code is as follows:

[zhangy@BlackGhost download] $find. -nouser-print # finds files in the current directory that do not belong

[zhangy@BlackGhost download] $find / home/zhangy/download-user zhangy-group users-print # find files whose user group is users and user is zhangy

/ home/zhangy/download

/ home/zhangy/download/eaccelerator-0.9.5.3.tar

/ home/zhangy/download/haproxy-1.3.15.7.tar.gz

F, root file size to find

The code is as follows:

[zhangy@BlackGhost download] $find / home/zhangy/download-size + 1000000c-print # look for files with a file size larger than 1000000 characters. Note that the + sign indicates that it is greater than

/ home/zhangy/download/eaccelerator-0.9.5.3.tar

/ home/zhangy/download/mmseg-0.7.3.tar.gz

[zhangy@BlackGhost download] $find / home/zhangy/download-size-10-print # looks for files whose file size is less than 10 blocks. Note that the-sign indicates less than, and one piece equals 512b.

/ home/zhangy/download

/ home/zhangy/download/fix-crash-in-excerpts.patch

/ home/zhangy/download/test.sql.zip

G, find the modification time of the root file

The code is as follows:

[zhangy@BlackGhost download] $find / home/zhangy-mtime-5-print # files modified within 5 days.

/ home/zhangy/www/css2/c_textshadow.html

/ home/zhangy/www/css2/c_textautospace.html

[zhangy@BlackGhost download] $find / home/zhangy-mtime + 5-print # find the file that was modified 5 days ago, and + indicates the previous

/ home/zhangy/www/test.php

[root@vmx14420 www] # find. /-mmin-5-print # find files that have been modified within 5 minutes

. / cache/index.html

HJE exec explanation

The code is as follows:

[zhangy@BlackGhost download] $find. -type f-size + 1000000c-exec ls-al {}\; # shows all files greater than 1000000 under the current directory, followed by a command, {}, which represents the file name

-rw-r--r-- 1 zhangy users 3624960 2009-03-08. / eaccelerator-0.9.5.3.tar

-rw-r--r-- 1 zhangy users 3091711 12-18 13:48. / mmseg-0.7.3.tar.gz

-rw-r--r-- 1 zhangy users 1191330 2009-06-26. / pcre-7.9.tar.gz

I, match

The code is as follows:

[tank@localhost workspace] $find. / database/-name'* .sql'- print # find files ending in sql

. / database/28toplearning.sql

[tank@localhost workspace] $find. / database/-name'* .sql *'- print # find the file name, including the sql file

. / database/28toplearning.sql

[tank@localhost workspace] $find. / database/-name'28 records'- print # find files that start with 28

. / database/28toplearning.sql

Locate

Usage: locate [OPTION]. [PATTERN]...

Search for entries in the mlocate database.

-b,-- basename matches the base file name of a unique path name

-c,-- count displays only the number where the entry was found

-d,-- database DBPATH replaces the default database with DBPATH (/ var/lib/mlocate/mlocate.db)

-e,-- existing displays only existing file entries

-L,-- follow follows the spread of symbolic links when the file exists (default)

-h,-- help displays this help

-I,-- ignore-case ignores case differences when matching patterns

-l,-- limit,-n LIMIT is limited to the output (or count) of the LIMIT project

-m,-- mmap ignores backward compatibility

-P,-- nofollow,-H do not follow the spread of symbolic links when checking files

-0,-- items are separated by NUL when null is exported

-S,-- statistics does not search for items and displays statistics about each used database

-Q,-- quiet does not report error messages about reading the database

-r,-- regexp REGEXP searches the basic regular expression REGEXP instead of the pattern

The regex pattern is an extended regular expression

-s,-- stdio ignores backward compatibility

-V,-- version displays version information

-w,-- wholename matches the full pathname (default)

Example:

The code is as follows:

[root@localhost ~] # locate ~ / install # find files in the current directory that begin with install

/ root/install.log

/ root/install.log.syslog

[root@localhost ~] # locate-I ~ / install # find files in the current directory that begin with install, and are not case sensitive

/ root/install.log

/ root/install.log.syslog

[root@localhost ~] # locate-I / etc/fuc # find files that begin with fuc in the / etc directory, not case-sensitive

At this point, the study on the "command introduction to find directories and files in Linux" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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