find命令的结果顺序是什么

 更新时间:2023年05月22日 10:55:36   作者:baiyu33  
这篇文章主要介绍了find命令的结果顺序是什么,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

1. 目的

在研读 doxygen 源码时, 在不同电脑、不同操作系统上使用了 find 命令, 发现对于相同的 doxygen 源码目录、相同的 find 命令参数, 得到了不同顺序的结果。尝试了解下 find 命令的参数, 希望得到统一一致的结果, 对不同结果的原因稍作分析。

简单的结论: find . -maxdepth 1 -type d 这样的命令后, 接入管道和 sort -V 得到自然排序结果:

find . -maxdepth 1 -type d | sort -V

2. 准备: 克隆 doxygen 源码

git clone https://gitee.com/mirrors/doxygen
git checkout 79a9efb
* 79a9efb 2023-05-18 | Merge pull request #10052 from albert-github/feature/bug_regr_ca65fd0bbb717a3f65e64bfcebe36a5debba66fa (grafted, HEAD -> master, origin/master, origin/HEAD) [Dimitri van Heesch]

我们希望用 find 命令, 打印出 doxygen 目录下的第一级子目录。

3. ubuntu22.04 结果

(base) zz@localhost-43% cat /etc/issue
Ubuntu 22.04.1 LTS \n \l
(base) zz@localhost% find --version
find (GNU findutils) 4.8.0
Copyright (C) 2021 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Written by Eric B. Decker, James Youngman, and Kevin Dalley.
开启的特性: D_TYPE O_NOFOLLOW(enabled) LEAF_OPTIMISATION FTS(FTS_CWDFD) CBO(level=2)
(base) zz@localhost% find . -maxdepth 1 -type d
.
./.git
./templates
./libxml
./deps
./libversion
./.github
./examples
./testing
./doc_internal
./addon
./winbuild
./doc
./src
./cmake
./vhdlparser

4. ubuntu16.04 结果

(base) zz@localhost-04% cat /etc/issue
Ubuntu 16.04.1 LTS \n \l
(base) zz@localhost-04% find --version
find (GNU findutils) 4.7.0-git
Copyright (C) 2016 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Written by Eric B. Decker, James Youngman, and Kevin Dalley.
Features enabled: D_TYPE O_NOFOLLOW(enabled) LEAF_OPTIMISATION FTS(FTS_CWDFD) CBO(level=2)
(base) zz@localhost-04% find . -maxdepth 1 -type d
.
./.github
./src
./doc
./addon
./doc_internal
./libxml
./testing
./deps
./.git
./examples
./libversion
./templates
./cmake
./winbuild
./vhdlparser

5. git bash 结果

$ find --version
find (GNU findutils) 4.9.0
Copyright (C) 2022 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Written by Eric B. Decker, James Youngman, and Kevin Dalley.
Features enabled: D_TYPE O_NOFOLLOW(enabled) LEAF_OPTIMISATION FTS(FTS_CWDFD) CBO(level=2)
$ find . -maxdepth 1 -type d
.
./.git
./.github
./addon
./cmake
./deps
./doc
./doc_internal
./examples
./libversion
./libxml
./src
./templates
./testing
./vhdlparser
./winbuild

6. 三路比较

7. 保持一样的结果: 用自然排序

传入 | sort -V 参数即可:

find . -maxdepth 1 -type d | sort -V
(base) zz@localhost% find . -maxdepth 1 -type d | sort -V
.
./.git
./.github
./addon
./cmake
./deps
./doc
./doc_internal
./examples
./libversion
./libxml
./src
./templates
./testing
./vhdlparser
./winbuild

8. References

https://www.baeldung.com/linux/find-default-sorting-order

关于find命令查找的排序规则探索以及排序方法

1、linux中find命令的排序规则

find命令的搜寻条件直接关系到输出结果,默认应该是按从左到右的的顺序判断,如果有逻辑运算(-not、-and、-or)还得再判断条件组合。
find查询的结果是找到一个匹配的项就立即输出结果,一边查找一边输出,查找到的内容不是一次性输出的,所以可能没有统一排序。
但是具体排序规则需要分析find命令的底层实现了。

2、linux shell编程中文件查找并排序的方法

方法一:

find  dir  -name "*.txt"  | sort

方法二:

ls  $(find dir -name "*.txt")

到此这篇关于find命令的结果顺序是什么的文章就介绍到这了,更多相关find命令的结果顺序内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Linux中rz命令和sz命令使用详解大全

    Linux中rz命令和sz命令使用详解大全

    在linux中rz 和 sz 命令允许开发板与主机通过串口进行传递文件了,下面我们就来简单的介绍一下rz 和 sz 命令实例
    2015-10-10
  • 关于vi和vim的区别及命令详解

    关于vi和vim的区别及命令详解

    下面小编就就为大家带来一篇关于vi和vim的区别及命令详解。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-01-01
  • Linux中修改文件权限chmod命令详解

    Linux中修改文件权限chmod命令详解

    在Linux系统中,chmod命令用于更改文件或目录的权限,它可以授予或撤销对文件的读取、写入和执行权限,本文给大家详细的介绍了Linux修改文件权限chmod命令用法,需要的朋友可以参考下
    2023-08-08
  • Bash中数组的操作教程

    Bash中数组的操作教程

    这篇文章主要介绍了Bash数组操,包括定义数组、读取数组、修改数组以及数组循环,文中通过示例代码给出了详细的介绍,有需要的朋友可以参考下,下面来一起看看吧。
    2016-12-12
  • shell脚本中 /dev/null 的用法小结

    shell脚本中 /dev/null 的用法小结

    /dev/null 通常被用于丢弃不需要的输出流,或作为用于输入流的空文件,这些操作通常由重定向完成,任何你想丢弃的数据都可以写入其中,本文重点给大家介绍shell脚本中 /dev/null 的用法小结,感兴趣的朋友一起看看吧
    2021-09-09
  • Linux用脚本实现“时分秒“倒计时功能

    Linux用脚本实现“时分秒“倒计时功能

    这篇文章主要介绍了Linux用脚本实现“时分秒“倒计时功能,本文图文实例相结合给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
    2019-12-12
  • git 使用及常用命令

    git 使用及常用命令

    本文是关于git 的使用和一些git使用小技巧,以及git的常用命令,进行的整理,希望能帮助有需要的小伙伴
    2016-07-07
  • 监控网站是否可以正常打开的Shell脚本分享

    监控网站是否可以正常打开的Shell脚本分享

    这篇文章主要介绍了监控网站是否可以正常打开的Shell脚本分享,可以指定N个网址,放在crontab中执行,需要的朋友可以参考下
    2014-09-09
  • 详解Linux  Shell 实现一个获取任意位数的随机密码的脚本

    详解Linux Shell 实现一个获取任意位数的随机密码的脚本

    这篇文章主要介绍了详解Linux Shell 实现一个获取任意位数的随机密码的脚本的相关资料,本文提供实现方法及实现代码,需要的朋友可以参考下
    2017-08-08
  • cpu时钟预取实例代码分享

    cpu时钟预取实例代码分享

    cpu时钟预取实例代码分享,大家参考使用吧
    2013-12-12

最新评论