find命令的結(jié)果順序是什么
1. 目的
在研讀 doxygen 源碼時(shí), 在不同電腦、不同操作系統(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é)果是找到一個(gè)匹配的項(xiàng)就立即輸出結(jié)果,一邊查找一邊輸出,查找到的內(nèi)容不是一次性輸出的,所以可能沒有統(tǒng)一排序。
但是具體排序規(guī)則需要分析find命令的底層實(shí)現(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)文章
Linux用腳本實(shí)現(xiàn)“時(shí)分秒“倒計(jì)時(shí)功能
這篇文章主要介紹了Linux用腳本實(shí)現(xiàn)“時(shí)分秒“倒計(jì)時(shí)功能,本文圖文實(shí)例相結(jié)合給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-12-12監(jiān)控網(wǎng)站是否可以正常打開的Shell腳本分享
這篇文章主要介紹了監(jiān)控網(wǎng)站是否可以正常打開的Shell腳本分享,可以指定N個(gè)網(wǎng)址,放在crontab中執(zhí)行,需要的朋友可以參考下2014-09-09詳解Linux Shell 實(shí)現(xiàn)一個(gè)獲取任意位數(shù)的隨機(jī)密碼的腳本
這篇文章主要介紹了詳解Linux Shell 實(shí)現(xiàn)一個(gè)獲取任意位數(shù)的隨機(jī)密碼的腳本的相關(guān)資料,本文提供實(shí)現(xiàn)方法及實(shí)現(xiàn)代碼,需要的朋友可以參考下2017-08-08