我想计算所有子目录中所有 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
如何组合这两个命令,或者是否有有效的替代方案可以解决这个问题?
答案1
您缺少\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 行。
列出档案中的文件:
$ 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
-O
使用/选项将文件提取到 stdout--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
没有换行)计算行数
wc
:$ find . -name "*.tar" -print0 | xargs -0I{} tar xOf {} | wc -l 10