我需要獲取作為 git 存盤庫的目錄的內容數量。
我必須得到以下數量:
1) Other directories inside the directory I am currently iterating (and the other sub-directories inside them if they exist)
2) .txt files inside the directory and its sub-directories
3) All the non-txt files inside the directory and its sub-directories
在上述所有情況下,我必須忽略.git目錄以及其中的所有檔案和目錄。
此外,我必須專門使用 bash 腳本。我不能使用另一種編程語言。
現在我正在使用以下命令來實作這一點:
為了讓所有的
.txt我用的檔案:find . -type f \( -name "*.txt" \)。.txt里面沒有檔案,.git所以這是有效的。要獲取
non-txt我使用的所有檔案:find . -type f \( ! -name "*.txt" \). 問題是我也從中獲取了所有檔案.git,但我不知道如何忽略它們。要獲得所有
directories和sub-directories我使用:find . -type d。我不知道如何忽略.git目錄及其子目錄
uj5u.com熱心網友回復:
簡單的方法是添加這些額外的測驗:
find . ! -path './.git/*' ! -path ./.git -type f -name '*.txt'
與此有關的問題./.git仍然是不必要的遍歷,這需要時間。
相反,-prune可以使用。-prune不是測驗(如-path, 或-type)。這是一個動作。操作是“不要下降當前路徑,如果它是一個目錄”。它必須與列印操作分開使用。
# task 1
find . -path './.git' -prune -o -type f -name '*.txt' -print
# task 2
find . -path './.git' -prune -o -type f ! -name '*.txt' -print
# task 3
find . -path './.git' -prune -o -type d -print
- 如果
-print未指定,./.git也列印為默認操作。 - 我用過
-path ./.git,因為你說的是??“.git目錄”。如果由于某種原因.git樹中還有其他目錄,它們將被遍歷和列印。要忽略名為 的樹中的所有目錄.git,請替換-path ./.git為-name .git。
uj5u.com熱心網友回復:
有時撰寫 bash 回圈比單行代碼更清晰
for f in $(find .); do
if [[ -d $f && "$f" == "./.git" ]]; then
echo "skipping dir $f";
else
echo "do something with $f";
fi;
done
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/368831.html
下一篇:如何在Git中更改分支庫?
