find命令的结果顺序是什么

1. 目的

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

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

  1. find . maxdepth 1 type d | sort V

2. 准备: 克隆 doxygen 源码

  1. git clone https://gitee.com/mirrors/doxygen
  2. git checkout 79a9efb
  1. * 79a9efb 20230518 | 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 结果

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

4. ubuntu16.04 结果

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

5. git bash 结果

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

6. 三路比较

-1

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

传入 | sort -V 参数即可:

  1. find . maxdepth 1 type d | sort V
  1. (base) zz@localhost% find . maxdepth 1 type d | sort V
  2. .
  3. ./.git
  4. ./.github
  5. ./addon
  6. ./cmake
  7. ./deps
  8. ./doc
  9. ./doc_internal
  10. ./examples
  11. ./libversion
  12. ./libxml
  13. ./src
  14. ./templates
  15. ./testing
  16. ./vhdlparser
  17. ./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编程中文件查找并排序的方法

方法一:

  1. find dir name “*.txt” | sort

方法二:

  1. ls $(find dir name “*.txt”)

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

标签

发表评论