同じ名前のファイルをディレクトリに移動し、ディレクトリ名にカウントされた番号を書き込みます。

同じ名前のファイルをディレクトリに移動し、ディレクトリ名にカウントされた番号を書き込みます。
find /volume1/file/* -type f \( -name "*DF*" -a -name "*LIVE*" \) -print0 | while IFS= read -d '' file
do
    # extract the name of the directory to create
    dirName="${file%.E*}"
    count=$(find "$file" -type f | wc -l;)
    # create the directory if it doesn't exist
    [[ ! -d "$dirName$count" ]] && mkdir "$dirName$count"

    mv "$file" "$dirName$count"
done

こんにちは、

他のエンコーダのシェルスクリプトを使用していますが、さらに多くの機能を追加したかったのですが、うまくいかなかったので修正しました。

部品修正count=$(find "$file" -type f | wc -l;)

たとえば、私のLinuxハードドライブには約10,000個のファイルがあり、この10,000個のファイルの中から「.E」の前にあるファイルヘッダーを読み取り、そのヘッダーにディレクトリを作成してファイルを移動します。ここでは、そのディレクトリのファイル数をディレクトリ名に入力したいと思います。

EX)
artist.E01.DF.LIVE
artist.E02.DF.LIVE
artist.E03.DF.LIVE
artist.E04.DD.LIVE
directory name = "artist3"

これをしたいのですが、変更されたスクリプトを使用すると1つだけ計算されます。

ベストアンサー1

(1)どこで、何を検索したいですか?find "$file" -type f名前付きパスの下のすべてのファイルを見つけます$file。しかし、これは1つのファイルなので、一度だけ見つけることができるので、取得する数は常に1です。

$file(2) 下の名前のファイルを検索しようとするかもしれませんが、/volume1/file/ファイルが 4 つなら、最初のファイルを移動した後は 3 つだけ残るので、ディレクトリ名が変わります。

(テストされていない)次のようなことをしたいかもしれません。

find /volume1/file/* -type f \( -name "*DF*" -a -name "*LIVE*" \) -print0 | while IFS= read -d '' file
do
    # extract the name of the directory to create
    dirName="${file%.E*}"
    if test -d "$dirName"*; then
      dirName="$dirName"*
    else
      count=$(find /volume1/file/* -name "$dirName*" -type f | wc -l)
      dirName="$dirName$count"
      mkdir "$dirName"
    fi
    mv "$file" "$dirName"
done

おすすめ記事