2020-06-06

linux-find


 
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, --hiddenSearch hidden files and directories搜索隐藏的文件和目录
-I, --no-ignoreDo not respect .(git|fd)ignore files不尊重。(git | fd)忽略文件
-s, --case-sensitiveCase-sensitive search (default: smart case)区分大小写的搜索(默认:智能大小写)
-i, --ignore-caseCase-insensitive search (default: smart case)不区分大小写的搜索(默认:智能大小写)
-g, --globGlob-based search (default: regular expression)基于glob的搜索(默认值:正则表达式)
-a, --absolute-pathShow absolute instead of relative paths显示绝对路径而不是相对路径
-l, --list-detailsUse a long listing format with file metadata对文件元数据使用长列表格式
-L, --followFollow symbolic links跟随符号链接
-p, --full-pathSearch full path (default: file-/dirname only)搜索完整路径(默认值:仅文件名/目录名)
-0, --print0Separate results by the null character用空字符分隔结果
-h, --helpPrints help information打印帮助信息
-V, --versionPrints 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 --helpUsage: locate [OPTION]... [PATTERN]…

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

3. grep

3.1 常用选项:

  -s, --no-messages suppress error messages 忽略一些报错信息
  -r, --recursive like --directories=recurse 递归搜索
  -R, --dereference-recursive likewise, but follow all symlinks 递归,查找链接文件目标
  -w, --word-regexp match only whole words 匹配特定单词
  -i, --ignore-case ignore case distinctions in patterns and data 忽略大小写
  -v, --invert-match select non-matching lines 不包含
  -l, --files-with-matches print only names of FILEs with selected lines 只显示文件名
  -n, --line-number print 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


没有评论:

发表评论

Diode

导航 (返回顶部) 1. Diode 1.1 Diode 概述 1.2 肖克利二极管方程 1.3 缩写 Abbreviations 2. 主要功能 2.1 单向电流 (Unidirectional current flow) 2.2 阈值电压 (Threshold...