Centos 7で特定のサイズ以上のファイルを削除するには、スクリプトが必要です。

Centos 7で特定のサイズ以上のファイルを削除するには、スクリプトが必要です。

ファイル名とサイズを考慮して、ファイルサイズが指定されたサイズより大きい場合は削除する必要があります。

ベストアンサー1

どうですか?

#!/bin/bash

# The first command line parameter is the size limit
LIMIT="$1"

shift 1

# Now loop over the rest of the command line parameters, which are the file names to check.
for file in "$@"; do 
  SIZE="$(stat --format="%s" "$file")"
  if [ "$SIZE" -gt "$LIMIT" ]; then
    echo "$file is $SIZE bytes. Deleting..."
    rm "$file"
  fi
done

サイズ制限は、すべてのファイル名が続く最初の引数として提供されます。

たとえば、サイズ制限は400バイトです。

script.sh 400 file1 file2 file3 ... fileN

ワイルドカードを使用することもできます。

script.sh 600 *.txt file1 bigfile2*.log dir1/*.txt dir2/*.old

おすすめ記事