linux for in file,How to find files in Linux?

这篇博客主要介绍了Linux中用于查找文件的`find`命令,强调了它与`locate`命令的区别。`find`命令实时递归搜索,虽然较慢但能提供最新信息并具有丰富的选项,如根据名称、类型、大小等进行搜索。而`locate`则依赖于预先构建的数据库,搜索速度快,但可能不包含最新变动的文件。博客还详细列举了`find`命令的各种选项,如`-name`、`-type`、`-size`等,并给出了使用示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Actually, there are two commands,findand locate, which help us find files that meet to a specific pattern. But,it’s important for us to keep in mind that there are several differences between findand locatewhich make them have different strengths in different aspects.

The findsearches recursively in real time, is slower but always up-to-date and provides more options (-name, -type, -size, …) than locate.

The locatesearches by using a previously built database which is automatically updated on a nightly basis. To update the database manually, change to a root user andtype the command updatedb, which would take a great amount of time according to the size of the system. Thus, it locates files much faster than find, but using an ‘older’ database may cause user failure to locate files which could be removed or not updated into the database.# locate passwd

Since locatedoesn’t search in real time and is rarely used in reality, this essay would mainly focus on the findcommand and only the options frequently used will be covered.

The format of the findcommand is:# find [path] [pattern] [action]

Path: Specify the working directory whose default working directory is the current one the findcommand starts in.

Pattern: Specify the search criterion. Without specification, it will recursively return all files under the working directory by default.

Action: Specify the action that will perform on the files found.

1. Standard Pattern

-name ‘FILENAME’: Match the files whose name is exactly the same as the FILENAME which can include the metacharacters (‘*’, ‘?’, ‘[]’) which make it more flexible.# find /etc -name 'passwd*'

-iname ‘FILENAME’: Like –name, but the match is case insensitive.

-regex PATTERN: File name matches regular expression pattern.

-user USERNAME: Search by user uname.# find /tmp -user user1

-group GROUPNAME: Search by group gname.

-uid UID: Search by numeric user ID.# find /tmp -uid 2003

-gid GID: Search by numeric group ID.

-nouser: Files that no user corresponds to will be returned.

-nogroup: Files that no group corresponds to will be returned.

-type [f|d|b|c|l|p|s]: Search by the type of files.

f    regular file

d    directory

b    block special

c    character special

l    symbolic link

p    named pipe

s    socket# find /tmp -type f

-size [+|-]#[kMG]: Search by the size of files. The defaultsize unit is byte.

+    greater than the specified criteria of size.

-    less than the specifiedcriteria of size.

k    for kilobytes

M    for megabytes

G    for gigabytes# find /etc -size -1M

-[mtime|ctime|atime][+|-]#: The default time unit is day.

mtime    file’s data was last modified #*24 hours ago.

ctime    file’s status was last changed #*24hours ago.

atime    file was last accessed #*24 hours ago.# find /tmp -atime +30

-[mmin|cmin|amin][+|-]#: The default time unit is minute.

mmin    file’s data was last modified #minutes ago.

cmin    file’s status was last changed # minutes ago.

amin    file was last accessed # minutes ago.# find /tmp -amin -5

-perm MODE: File’s permission bits are exactly the same as MODE (octal or symbolic).# find . -perm 644

-MODE: All of the permission bits MODE are set for the file.# find . -perm -664

/MODE: Any of the permission bits MODE are set for the file.# find . -perm /222

2. Logical operation

-a: and# find /tmp -nouser -a -type d -ls

-o: or# find ./ -not \(-user user1 -o -user user2 \)

-not: not# find /tmp -not -type d

3. Action

-print: print the full file name on the standard output.

-ls: list current file in ls –dils format on standard output.

-exec COMMAND {} \;: execute COMMAND. {} represents the current file name.# find ./ -perm -006 -exec chmod 0-w {} \;

-ok COMMAND {} \;: like –exec but ask the user first.# find ./ -type d -ok chmod +x {} \;

Reference:[1] Man7.org, (2015). find(1) - Linux manual page. [online] Available at: http://man7.org/linux/man-pages/man1/find.1.html [Accessed 14 Oct. 2015].

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值