現在のレベル (サブフォルダーを含まない) のフォルダーのリストを取得し、フォルダー名とフォルダー内のファイル数を単純に出力したい (できれば *.jpg にフィルター処理することをお勧めします)。
これは標準のbashシェルで可能ですか? ls -l
ファイル数以外のすべてについて出力します:)
私はこれを思いついた:
find -maxdepth 1 -type d | while read dir; do
count=$(find "$dir" -maxdepth 1 -iname \*.jpg | wc -l)
echo "$dir ; $count"
done
-maxdepth 1
ディレクトリ内の jpg ファイルの検索をサブディレクトリを考慮して再帰的に行う必要がある場合は、2 番目を削除します。ファイルの名前のみが考慮されることに注意してください。jpg 画像であることを隠して、ファイルの名前を変更できます。代わりに、コマンドを使用file
してコンテンツを推測できます (現在、再帰的に検索することもできます)。
find -mindepth 1 -maxdepth 1 -type d | while read dir; do
count=$(find "$dir" -type f | xargs file -b --mime-type |
grep 'image/jpeg' | wc -l)
echo "$dir ; $count"
done
ただし、ファイルの一部を読み取って最終的に内容を解釈する必要があるため、これは非常に遅くなります (運が良ければ、ファイルの先頭にマジック ID が見つかります)。は、検索する別のディレクトリとして (現在のディレクトリ) を-mindepth 1
印刷することを防ぎます。.
この質問は、自分の同様のスクリプトをすでに見つけた後で見つけました。それはあなたの条件に合っているようで、非常に柔軟なので、答えとして追加すると思いました.
利点:
.
、第 1 レベルのサブディレクトリの場合は 1 など)。find
コマンドは 1 つだけなので、大規模なディレクトリでは少し高速です生コード:
find -P . -type f | rev | cut -d/ -f2- | rev | \
cut -d/ -f1-2 | cut -d/ -f2- | sort | uniq -c
関数にラップして説明しました:
fc() {
# Usage: fc [depth >= 0, default 1]
# 1. List all files, not following symlinks.
# (Add filters like -maxdepth 1 or -iname='*.jpg' here.)
# 2. Cut off filenames in bulk. Reverse and chop to the
# first / (remove filename). Reverse back.
# 3. Cut everything after the specified depth, so that each line
# contains only the relevant directory path
# 4. Cut off the preceeding '.' unless that's all there is.
# 5. Sort and group to unique lines with count.
find -P . -type f \
| rev | cut -d/ -f2- | rev \
| cut -d/ -f1-$((${1:-1}+1)) \
| cut -d/ -f2- \
| sort | uniq -c
}
次のような出力が生成されます。
$ fc 0
1668 .
$ fc # depth of 1 is default
6 .
3 .ssh
11 Desktop
44 Downloads
1054 Music
550 Pictures
もちろん、番号を最初に指定すると、次のようにパイプできますsort
。
$ fc | sort
3 .ssh
6 .
11 Desktop
44 Downloads
550 Pictures
1054 Music
私の場合は、コマンド ラインから入力する方が高速です。:)
他の提案は、以下よりも実際の利点を提供しますか?
find -name '*.jpg' | wc -l # recursive
find -maxdepth 1 -name '*.jpg' | wc -l # current directory only
#!/bin/bash
for dir in `find . -type d | grep -v "\.$"`; do
echo $dir
ls $dir/*.jpg | wc -l
done;
外部コマンドなしでそれを行うことができます:
for d in */; do
set -- "$d"*.jpg
printf "%s: %d\n" "${d%/}" "$#"
done
または、 awk ( Solarisでは nawkまたは/usr/xpg4/bin/awk )を使用できます。
printf "%s\n" */*jpg |
awk -F\/ 'END {
for (d in _)
print d ":",_[d]
}
{ _[$1]++ }'