Linux- ls If statement

linux shell date if-statement ls

397 просмотра

1 ответ

I'm looking for an if statement. So when I run my script, it brings me a list of the backups with the date. The if statement is for, if there is no backup of the last 2 days echo "No recent backup".

for when I ls -lrth:

-rw-r--r--. 1 nfsnobody nfsnobody 1.5G Aug 15 12:02

-rw-r--r--. 1 nfsnobody nfsnobody 1.5G Aug 16 01:30

If the date is older than 2 days echo " "

Автор: anonimo21 Источник Размещён: 08.11.2019 11:23

Ответы (1)


1 плюс

BACKUP_DIRECTORY="basename" #basename of the directory 

MATCHES=$(find "$BACKUP_DIRECTORY" -maxdepth 1 -type f -mtime -2 -ls | wc -l) 

if [ $MATCHES -eq 0 ]; then
    echo "CRITICAL: No files older than 2 days yet in directory $*"
    exit 2
else
    echo "OK: Found $MATCHES file(s) of the last 2 days in ${BACKUP_DIRECTORY}"
    exit 0
fi

to get a list of backups of the last 2 days use:

find "$BACKUP_DIRECTORY" -maxdepth 1 -type f -mtime -2 -ls

  • -maxdepth 1. Spezifies the depth directory travesal. We don't want to go more than 1 levels down in the sub directories.
  • -type f means only files will be found (no directories).
  • -mtime -2 means the age of the file is less than 2 days.

wc -l counts the number of the found files

Автор: Elisabeth S Размещён: 23.08.2016 12:47
Вопросы из категории :
32x32