LoginSignup
2
0

More than 1 year has passed since last update.

#shell で「ファイルの中身が存在する」「空ファイル or ファイルが存在しない」の違いを判定する ( -s filepath )

Last updated at Posted at 2019-05-25
  • if [ -s <filepath> ] でファイルが存在し、なおかつ中身がある場合に True 判定が返理想
  • ファイルの中身が空 or ファイルが存在しない場合に False 判定が返りそう

example

$ echo "a" > /tmp/a
$ touch /tmp/b
$ if [ -s /tmp/a ]; then echo not empty; else echo empty; fi
not empty
$ if [ -s /tmp/b ]; then echo not empty; else echo empty; fi
empty
$ if [ -s /tmp/c ]; then echo not empty; else echo empty; fi
empty

ref

You can use the find command and other options as follows. The -s option to the test builtin check to see if FILE exists and has a size greater than zero. It returns true and false values to indicate that file is empty or has some data. This page shows how to check if a file is empty in Bash shell running on a Linux or Unix-like operating systems.

Linux / UNIX: Check If File Is Empty Or Not Using Shell Script - nixCraft

Original by Github issue

チャットメンバー募集

何か質問、悩み事、相談などあればLINEオープンチャットもご利用ください。

Twitter

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0