Linux

將所有子目錄中所有 tar 文件的程式碼總行數相加

  • March 9, 2020

我想計算所有子目錄中所有 tar 文件的總行數。

考慮以下範例目錄se-example

se-example
├── directory1
│   ├── archive1.tar
│   └── archive2.tar
└── directory2
   ├── README.txt
   ├── archive3.tar
   └── directory3
       └── archive4.tar

要重新創建此範例,請考慮以下內容:

echo "create and enter example"
mkdir se-example
cd se-example

echo "create example directory 1 (w/ two tar files)"
mkdir directory1
cd directory1

echo "create example tar 1 [+7 lines]"
mkdir archive1
printf "Line 1 \n Line 2" >> archive1/README.txt
printf "Line 1 \n Line 2" >> archive1/code1.py
printf "Line 1 \n Line 2 \n Line 3" >> archive1/code2.py
tar -cf archive1.tar archive1
rm -rf archive1 

echo "create example tar 2 [+5 lines]"
mkdir archive2 
printf "Line 1 \n Line 2 \n Line 3" >> archive2/code1.py
printf "Line 1 \n Line 2" >> archive2/code2.py
tar -cf archive2.tar archive2
rm -rf archive2

cd ..

echo "create example directory 1 (w/ subdirectory, readme, and two tar files)"
mkdir directory2
cd directory2

echo "create example readme [+0 lines]"
printf "Line 1 \n Line 2" >> README.txt

echo "create example tar 3 [+1 line]"
mkdir archive3 
printf "Line 1" >> archive3/code1.py
tar -cf archive3.tar archive3
rm -rf archive3

echo "create example subdirectory (w/ one tar file)"
mkdir directory3
cd directory3

echo "create example tar 4 [+5 lines]"
mkdir archive4
printf "Line 1 \n Line 2 \n Line 3" >> archive4/code1.py
printf "Line 1 \n Line 2" >> archive4/code2.py
tar -cf archive4.tar archive4
rm -rf archive4

cd ..
cd ..
cd ..

echo "done creating example"

在這個例子中,有四個 tar 文件,總共 18 行(tar 1 中 7 行,tar 2 中 5 行,tar 3 中 1 行,tar 4 中 5 行)。正確答案是 18。

我知道如何計算 tar 文件的數量(這裡建議):

find . -maxdepth 1 -mindepth 1 -type d | while read dir; do
 printf "%-25.25s : " "$dir"
 find "$dir" -name "*.tar" | wc -l
done

我知道如何計算單個 tar 文件中的程式碼行數(此處建議):

tar -tf se-example/directory1/archive1.tar | wc -l

我如何組合這兩個命令,或者是否有一個有效的替代方案可以解決這個問題?

\n在每個測試文件的最後一行都缺少換行符,因此wc實際上的計數更少:

$ find . \( -name "*.py" -o -name "README*" \) -print0 | xargs -0 wc -l
 1 ./directory1/archive1/README.txt
 1 ./directory1/archive1/code1.py
 2 ./directory1/archive1/code2.py
 2 ./directory1/archive2/code1.py
 1 ./directory1/archive2/code2.py
 1 ./directory2/README.txt
 0 ./directory2/archive3/code1.py
 2 ./directory2/directory3/archive4/code1.py
 1 ./directory2/directory3/archive4/code2.py
11 total

由於/directory2/README.txt不在存檔中,我們總共有 10 行。

  1. 列出檔案中的文件:
$ find . -name "*.tar" -print0 | xargs -0I{} tar tf {}
archive1/
archive1/README.txt
archive1/code1.py
archive1/code2.py
archive2/
archive2/code1.py
archive2/code2.py
archive3/
archive3/code1.py
archive4/
archive4/code1.py
archive4/code2.py
  1. -O使用/--to-stdout選項將文件提取到標準輸出:
$ find . -name "*.tar" -print0 | xargs -0I{} tar xOf {}
Line 1
Line 2Line 1
Line 2Line 1
Line 2
Line 3Line 1
Line 2
Line 3Line 1
Line 2Line 1Line 1
Line 2
Line 3Line 1
Line 2

(最後一行Line 2沒有換行符) 3. 用 計算行數wc

$ find . -name "*.tar" -print0 | xargs -0I{} tar xOf {} | wc -l
10

引用自:https://unix.stackexchange.com/questions/571600