码迷,mamicode.com
首页 > 系统相关 > 详细

Linux-find

时间:2020-06-06 22:01:36      阅读:100      评论:0      收藏:0      [点我收藏+]

标签:cat   简单   ESS   lis   man   statistic   not   tlb   cto   

 

 
1. fd
2. mlocate
 2.1 安装mlocate
 2.2 手动更新库
 2.3 查看配置文件
 2.4 使用
 2.5 帮助-常用选项
3. grep 
 3.1 常用选项:
 3.2 示例
4. find


桌面搜索:
https://wiki.archlinux.org/index.php/List_of_applications/Utilities#File_searching
https://en.wikipedia.org/wiki/List_of_search_engines#Desktop_search_engines

命令行搜索:
https://wiki.archlinux.org/index.php/Core_utilities#find_alternatives

1. fd

Simple, fast and user-friendly alternative to find. Ignores hidden and .gitignore‘d files by default. 简单,快速和用户友好的替代方法。 默认情况下,忽略隐藏和.gitignore文件。
https://github.com/sharkdp/fd
注意:fd -h 打印简短的概述; fd --help 提供所有细节.
常用选项:

FLAGS:    
-H, --hidden Search hidden files and directories 搜索隐藏的文件和目录
-I, --no-ignore Do not respect .(git|fd)ignore files 不尊重。(git | fd)忽略文件
-s, --case-sensitive Case-sensitive search (default: smart case) 区分大小写的搜索(默认:智能大小写)
-i, --ignore-case Case-insensitive search (default: smart case) 不区分大小写的搜索(默认:智能大小写)
-g, --glob Glob-based search (default: regular expression) 基于glob的搜索(默认值:正则表达式)
-a, --absolute-path Show absolute instead of relative paths 显示绝对路径而不是相对路径
-l, --list-details Use a long listing format with file metadata 对文件元数据使用长列表格式
-L, --follow Follow symbolic links 跟随符号链接
-p, --full-path Search full path (default: file-/dirname only) 搜索完整路径(默认值:仅文件名/目录名)
-0, --print0 Separate results by the null character 用空字符分隔结果
-h, --help Prints help information 打印帮助信息
-V, --version Prints version information 打印版本信息
OPTIONS:    
-d, --max-depth <depth> Set maximum search depth (default: none) 设置最大搜索深度(默认值:无)
-t, --type <filetype>... Filter by type: file (f), directory (d), symlink (l), executable (x), empty (e), socket (s), pipe (p) 类型过滤: file (f), directory (d), symlink (l), executable (x), empty (e), socket (s), pipe (p)
-e, --extension <ext>... Filter by file extension 按文件扩展名过滤
-x, --exec <cmd> Execute a command for each search result 对每个搜索结果执行命令
-X, --exec-batch <cmd> Execute a command with all search results at once 一次执行所有搜索结果的命令
-E, --exclude <pattern>... Exclude entries that match the given glob pattern 排除与给定的glob模式匹配的条目
-c, --color <when> When to use colors: never, *auto*, always 何时使用颜色:从不,*自动*,始终
-S, --size <size>... Limit results based on the size of files. 根据文件大小限制结果。
--changed-within <date|dur> Filter by file modification time (newer than) 按文件修改时间(早于)过滤
--changed-before <date|dur> Filter by file modification time (older than) 按文件修改时间过滤(早于)
-o, --owner <user:group> Filter by owning user and/or group 通过拥有用户和/或组进行过滤
ARGS:    
<pattern> the search pattern - a regular expression unless ‘--glob‘ is used (optional) 搜索模式-除非使用‘--glob‘,否则为正则表达式(可选)
<path>... the root directory for the filesystem search (optional) 文件系统搜索的根目录(可选)

 

2. mlocate

Merging locate/updatedb implementation. 通过索引库查找
https://pagure.io/mlocate

2.1 安装mlocate

包里包括了一个 updatedb.timer 单元,用于定期更新数据库。
$ cat /usr/lib/systemd/system/updatedb.timer
[Unit]
Description=Daily locate database update

[Timer]
OnCalendar=daily
AccuracySec=12h
Persistent=true

$ systemctl is-enabled updatedb.timer
static

2.2 手动更新库

$ sudo updatedb

2.3 查看配置文件

$ cat /etc/updatedb.conf 
PRUNE_BIND_MOUNTS = "yes"
PRUNEFS = "9p afs anon_inodefs auto autofs bdev binfmt_misc cgroup cifs coda configfs cpuset cramfs debugfs devpts devtmpfs ecryptfs exofs ftpfs fuse fuse.encfs fuse.sshfs fusectl gfs gfs2 hugetlbfs inotifyfs iso9660 jffs2 lustre mqueue ncpfs nfs nfs4 nfsd pipefs proc ramfs rootfs rpc_pipefs securityfs selinuxfs sfs shfs smbfs sockfs sshfs sysfs tmpfs ubifs udf usbfs vboxsf"
PRUNENAMES = ".git .hg .svn"
PRUNEPATHS = "/afs /media /mnt /net /sfs /tmp /udev /var/cache /var/lib/pacman/local /var/lock /var/run /var/spool /var/tmp"

最后2行表示, 默认会忽略的版本控制文件, 不关键的文件目录.

2.4 使用

locate命令的运行速度很快,但是没有find命令那么多选项。你可以用它来执行简单的文件搜索,基本的搜索方式与find命令相同。
$ locate -i "*.jpg" |wc -l
349

2.5 帮助-常用选项

$ locate --help Usage: locate [OPTION]... [PATTERN]…  
  Search for entries in a mlocate database. 在mlocate数据库中搜索条目。
-A, --all only print entries that match all patterns 仅打印符合所有模式的条目
-b, --basename match only the base name of path names 仅匹配路径名的基本名称
-c, --count only print number of found entries 仅打印找到的条目数
-d, --database DBPATH use DBPATH instead of default database
(which is /var/lib/mlocate/mlocate.db)
使用DBPATH代替默认数据库
(/var/lib/mlocate/mlocate.db)
-e, --existing only print entries for currently existing files 仅打印当前现有文件的条目
-L, --follow follow trailing symbolic links when checking file existence (default) 检查文件存在时遵循尾随符号链接(默认)
-h, --help print this help 打印此帮助
-i, --ignore-case ignore case distinctions when matching patterns 匹配模式时忽略大小写区别
-l, --limit, -n LIMIT limit output (or counting) to LIMIT entries 将输出(或计数)限制为LIMIT个条目
-m, --mmap ignored, for backward compatibility 被忽略,为了向后兼容
-P, --nofollow, -H don‘t follow trailing symbolic links when checking file existence 检查文件存在时不要遵循尾随的符号链接
-0, --null separate entries with NUL on output 在输出上使用NUL的单独条目
-S, --statistics don‘t search for entries, print statistics about each used database 不搜索条目,打印有关每个使用的数据库的统计信息
-q, --quiet report no error messages about reading databases 报告有关读取数据库的任何错误消息
-r, --regexp REGEXP search for basic regexp REGEXP instead of patterns 搜索基本的正则表达式REGEXP而不是模式
--regex patterns are extended regexps 模式是扩展的正则表达式
-s, --stdio ignored, for backward compatibility 被忽略,为了向后兼容
-V, --version print version information 打印版本信息
-w, --wholename match whole path name (default) 匹配整个路径名(默认)

 

3. grep

3.1 常用选项:

  -s, --no-messagessuppress error messages 忽略一些报错信息
  -r, --recursivelike --directories=recurse 递归搜索
  -R, --dereference-recursivelikewise, but follow all symlinks 递归,查找链接文件目标
  -w, --word-regexpmatch only whole words 匹配特定单词
  -i, --ignore-caseignore case distinctions in patterns and data 忽略大小写
  -v, --invert-matchselect non-matching lines 不包含
  -l, --files-with-matchesprint only names of FILEs with selected lines 只显示文件名
  -n, --line-numberprint line number with output lines带输出行的打印行号

3.2 示例

查找并删除表格内高度参数
https://www.cnblogs.com/sztom/p/11297170.html#211
https://szosoft.blogspot.com/2019/08/grep.html#211

https://linuxconfig.org/how-to-find-all-files-with-a-specific-text-using-linux-shell
-- 原文包含更多示例及详解, 这里只摘录了少部分.
递归搜索etc目录及子目录下包含 club 字符的文件
$ sudo grep -sR club /etc/*

不打开文件, 只显示文件名
$ sudo grep -irl Club /etc/*

搜索时包含/排除指定文件
使用grep命令还可以仅包含特定文件作为搜索的一部分。例如,我们只想在扩展名为的配置文件中搜索特定的文本/字符串.conf。下一个示例将.conf在/etc目录中包含string的所有扩展名文件bash:
$ sudo grep -Ril bash /etc/*.conf
$ sudo grep -Ril --include=\*.conf bash /etc/*
同样,使用--exclude选项我们可以排除任何特定的文件名:
$ sudo grep -Ril --exclude=\*.conf bash /etc/*
从搜索中排除特定目录
$ sudo grep --exclude-dir=/etc/grub.d -Rwl stretch /etc/*

显示包含搜索字符串的行号
通过使用-n选项grep还将提供有关找到特定字符串的行号的信息:
$ sudo grep -Rni bash /etc/*.conf

4. find

https://www.cnblogs.com/duaimili/p/10192710.html
-- 原文包含更多示例及详解, 这里只摘录了少部分.
使用布尔操作符来整合搜索过滤。使用-and、-or和-not操作符来整合不同类型的搜索条件。
$ find /travelphotos -type f -size +200k -not -iname "*2015*"
这个命令会在“travelphotos”目录里查找大于200KB,文件名里不包含“2015”的所有文件。

根据用户或权限搜索文件。如果你想要查找特定用户的文件,或是带有特定权限的文件,可以根据此来缩窄搜索条件。
$ find / -user pat -iname "文件名" find / -group users -iname "文件名" find / -perm 777 -iname "文件名"
以上命令会搜索特定用户、群组或权限的文件。你可以忽略文件名,返回所有匹配条件的文件。如:find / -perm 777会返回所有带有777权限的文件(无限制条件)。[3]

将执行命令整合到搜索文件中。你可以将find命令和其它命令整合到一起,这样在返回搜索结果时,执行命令。用-exec来隔开find命令和第二个命令,然后结尾用{} \;字符。
$ find . -type f -perm 777 -exec chmod 755 {} \;
这样会在当前路径和所有子目录里搜索带有777权限的文件。找到文件后,chmod命令会将权限更改为755。

https://www.cnblogs.com/sztom/p/10209520.html

Linux-find

标签:cat   简单   ESS   lis   man   statistic   not   tlb   cto   

原文地址:https://www.cnblogs.com/sztom/p/13056459.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!