特定のファイルが特定のサイズの場合にのみコマンドを実行する方法

特定のファイルが特定のサイズの場合にのみコマンドを実行する方法

特定のファイルが定義されたサイズを超える場合にのみコマンドを実行するにはどうすればよいですか?どちらも最終的にcrontabで1行のコードで実行する必要があります。

擬似コード:

* * * * * find /cache/myfile.csv -size +5G && echo "file is > 5GB"

ベストアンサー1

GNUがある場合は、そのオプションを使用してサイズを取得statできます。--printf

例えば

size=$(stat --printf '%s' /cache/myfile.csv)
if [ "$size" -gt 5368709120 ] ; then  # 5 GiB = 5 * 1024 * 1024 * 1024
  echo "file is > 5GB"
fi

man stat詳細より。


BSD stat(FreeBSDやMacなど)にも同様の書式設定オプションがあります-f

size=$(stat -f '%z' /cache/myfile.csv)

あるいは、Perlの組み込みstat関数または-sファイルテスト演算子を使用することもできます(bashのfile testに似ています-sが、ファイルが存在し空でない場合はtrueを返すのではなくファイルサイズを返します)。 Perlのstat関数は、次のデータ(からコピー)を含むファイルのメタデータの13要素リスト(配列)を返しますperldoc -f stat

[...] Not all fields are supported on all filesystem types. Here are
the meanings of the fields: 

  0 dev      device number of filesystem
  1 ino      inode number
  2 mode     file mode  (type and permissions)
  3 nlink    number of (hard) links to the file 
  4 uid      numeric user ID of file's owner
  5 gid      numeric group ID of file's owner
  6 rdev     the device identifier (special files only) 
  7 size     total size of file, in bytes
  8 atime    last access time in seconds since the epoch
  9 mtime    last modify time in seconds since the epoch
 10 ctime    inode change time in seconds since the epoch (*)
 11 blksize  preferred I/O size in bytes for interacting with the
             file (may vary from file to file)
 12 blocks   actual number of system-specific blocks allocated
             on disk (often, but not always, 512 bytes each) 

(The epoch was at 00:00 January 1, 1970 GMT.)

フィールド7は私たちに必要なフィールドです。

ファイルサイズを返すには(後でシェルコマンドまたはスクリプトで使用するために)、次のようにしますstat

# stat
perl -e 'print scalar((stat(shift))[7])' /cache/myfile.csv

# -s
perl -e 'print -s shift' /cache/myfile.csv

または、Perlを使用してすべての操作を実行できます。

# stat
perl -e 'print "File is > 5 GiB\n" if (stat(shift))[7] > 5*1024*1024*1024' /cache/myfile.csv

# -s
perl -e 'print "File is > 5 GiB\n" if -s shift > 5*1024*1024*1024' /cache/myfile.csv

perldoc -f statand perldoc -f -X(そしてhelp testbashで)を参照してください。

ただし、Perlのshift関数は、配列の最初の要素(デフォルトで@ARGV指定されていない場合はコマンドライン引数の配列)を削除し、その値を返します。配列のすべての要素を処理するためにループで頻繁に使用されますが、ここでは最初のパラメータ(ファイル名)にのみ興味があります。perldoc -f shift語彙の範囲やサブルーチンでの使用に関する注意事項を含む詳細については、参考資料を参照してください。

おすすめ記事