Bash Check If File Exists and Not Empty

Language:
Shell
8 views
0 favorites
1 hours ago

Code Implementation

Shell
#!/bin/bash

FILE="data.txt"

if [ -s "$FILE" ]; then
  echo "File exists and is not empty."
else
  echo "File does not exist or is empty."
fi

In Bash scripts, it is often necessary to check if a file exists and is not empty.

#bash#scripting#file-check

Snippet Description

  • The -s parameter indicates that the file exists and its size is greater than 0
  • If the file does not exist or is empty, it enters the else branch
  • Can be used in scenarios such as scheduled tasks and log monitoring

Comments

Loading...