我有一个目录里面有多个目录,其中包含许多类型的文件。
我想找到*。jpg文件,然后得到计数和总大小的所有个人一个。
我知道我必须使用find wc -l和du -ch,但我不知道如何在单个脚本或单个命令中组合它们。
找到。-type f name "*。jpg" -exec -不确定如何连接所有这三个
###假设你的起始文件夹是.
this will give you all files and the to这会给你所有的文件和总大小:
find . -type f -name '*.jpg' -exec du -ch {} +
末尾的+在所有文件上一次执行du -ch,而不是每个文件,让您得到frand总数。
如果你只想知道总数| tail -n 1
at the end.
公平警告:这其实是执行的
du -ch file1 file2 file3 ...
这可能会破坏很多文件。
要检查有多少:
$ getconf ARG_MAX
2097152
这是在我的系统上配置的。
但这并没有给出文件的数量。您需要捕获find的输出并使用它两次。
最后一行是total,所以我们将使用除了最后一行以外的所有文件来获得文件的数量,最后一行是total:
OUT=$(find . -type f -name '*.jpg' -exec du -ch {} +)
N=$(echo "$OUT" | head -n -1 | wc -l)
SIZE=$(echo "$OUT" | tail -n 1)
echo "Number of files: $N"
echo $SIZE
这给了我:
Number of files: 143
584K total
###如果你想要得到线的数量和每个个体的大小.jpg
file in a directory you can try this script.
#!/usr/bin/env bash
#Create variable to store the totals of .jpg file found and their line count.
Files=$(wc -l *.jpg | tail -1 | awk '{print "Total Files: "$1}')
sizes=$(du -ch *.jpg | tail -1 | awk '{print "Total Size: " $1}')
#Create temporary directories
size="tempsizefile.txt"
line="templinefile.txt"
#Begin a loop to display each file seperately showing line count and size
for j in *.jpg; do
du -ch $j |awk 'NR==1{print $1}' > "$size" #Size used
wc -l $j > "$line" #Total lines in each file and filename
paste "$size" "$line"
rm -f "$size" "$line" #Paste then remove the temp files
done
echo "Size Lines Filename"
echo
#Optional if the individual display is not needed.
echo "$Files"
echo "$sizes"
输出
4.0K 1 a90.jpg
4.0K 1 a91.jpg
4.0K 1 a92.jpg
4.0K 1 a93.jpg
4.0K 1 a94.jpg
4.0K 1 a95.jpg
4.0K 1 a96.jpg
4.0K 1 a97.jpg
4.0K 2 a98.jpg
4.0K 1 a99.jpg
4.0K 1 a9.jpg
Size Lines Filename
Total Files: 100
Total Size: 400K