find命令的結(jié)果順序是什么
1. 目的
在研讀 doxygen 源碼時, 在不同電腦、不同操作系統(tǒng)上使用了 find 命令, 發(fā)現(xiàn)對于相同的 doxygen 源碼目錄、相同的 find 命令參數(shù), 得到了不同順序的結(jié)果。嘗試了解下 find 命令的參數(shù), 希望得到統(tǒng)一一致的結(jié)果, 對不同結(jié)果的原因稍作分析。
簡單的結(jié)論: find . -maxdepth 1 -type d 這樣的命令后, 接入管道和 sort -V 得到自然排序結(jié)果:
find . -maxdepth 1 -type d | sort -V
2. 準(zhǔn)備: 克隆 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 結(jié)果
(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 結(jié)果
(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 結(jié)果
$ 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. 保持一樣的結(jié)果: 用自然排序
傳入 | sort -V 參數(shù)即可:
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
關(guān)于find命令查找的排序規(guī)則探索以及排序方法
1、linux中find命令的排序規(guī)則
find命令的搜尋條件直接關(guān)系到輸出結(jié)果,默認(rèn)應(yīng)該是按從左到右的的順序判斷,如果有邏輯運(yùn)算(-not、-and、-or)還得再判斷條件組合。
find查詢的結(jié)果是找到一個匹配的項就立即輸出結(jié)果,一邊查找一邊輸出,查找到的內(nèi)容不是一次性輸出的,所以可能沒有統(tǒng)一排序。
但是具體排序規(guī)則需要分析find命令的底層實現(xiàn)了。
2、linux shell編程中文件查找并排序的方法
方法一:
find dir -name "*.txt" | sort
方法二:
ls $(find dir -name "*.txt")
到此這篇關(guān)于find命令的結(jié)果順序是什么的文章就介紹到這了,更多相關(guān)find命令的結(jié)果順序內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Shell腳本實現(xiàn)檢測進(jìn)程是否正在運(yùn)行
這篇文章主要介紹了Shell腳本實現(xiàn)檢測進(jìn)程是否正在運(yùn)行,本文給出的代碼簡潔清晰,并給出了使用方法,需要的朋友可以參考下2015-01-01
npm script命令同時進(jìn)行多個監(jiān)聽服務(wù)的方法
這篇文章主要介紹了npm script命令同時進(jìn)行多個監(jiān)聽服務(wù)的方法,非常不錯,具有一定的參考借鑒價值,需要的朋友參考下吧2018-08-08
Linux使用curl訪問網(wǎng)頁和wget下載文件
這篇文章介紹了Linux使用curl命令訪問網(wǎng)頁和wget下載文件的方法,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-06-06
linux shell在while中用read從鍵盤輸入的實現(xiàn)
下面小編就為大家?guī)硪黄猯inux shell在while中用read從鍵盤輸入的實現(xiàn)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-01-01
獲取同一網(wǎng)段下所有機(jī)器MAC地址的shell腳本
有時候需要在當(dāng)前同一網(wǎng)段下所有機(jī)器MAC地址,需要的朋友可以參考下2013-01-01

