ディレクトリ内のすべてのファイルに対して「du -sh」を実行するスクリプトをどのように作成しますか?

ディレクトリ内のすべてのファイルに対して「du -sh」を実行するスクリプトをどのように作成しますか?

私たちの組織には、ユーザーファイル専用のネットワーク共有があります。各ユーザーには独自のディレクトリがあります。共有スペースを最も使用している人が誰であるかを調べたいと思います。私の現在のコードは次のとおりです。

#!/bin/bash

#Author= x
#Date= 04/28/23
#Desc= x

# Defining the directory
directory="/path/to/directory"

# Creating array to hold the file names
files=()

# Loop through each file in the directory
for file in "$directory"/*; do
  # Add the file name to the array
  files+=("$file")
done

# Use du to get the size of each file and sort the results by size
du -sh "${files[@]}" | sort -h > output.txt

スクリプトを実行すると、「du: '$file'にアクセスできません。そのファイルまたはディレクトリがありません」というメッセージが表示されます。私はスクリプトに初めて触れましたが、この問題は変数の定義方法に関連していますが、何を変更するのかわかりません。 。

ベストアンサー1

あなたは仕事を過度に複雑にしています。 Gilesがコメントで指摘したように:

du -sh /path/to/directory/* | sort -h

おすすめ記事