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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值