OS/Linux Server
[Shell Programming] 오래된 로그 삭제
by Haengsin
2022. 11. 13.
- 명령행 인자 2개 전달
- 1) Log 경로
- 2) 1,0 //1:삭제, 0:삭제 안함 출력
- 출력 ( Command : {스크립트 파일명}.sh {경로} {0 or 1} )
- hostname
- df -h // 파일시스템 용량.
- 오래된 파일 순으로 해당 디렉토리 파일들.
- 삭제 대상 리스트 출력 (1일 지난 파일들).
#!/bin/bash
count=$(find $1 -type f -mtime +1 | wc -l)
hostname -f
echo -e "\nExecute the command 'df -h' \n"
df -h
echo -e "\nSort oldest files first\n"
ls $1 --time-style="+%Y-%m-%d %H:%M:%S" -altr | grep ^-
echo -e "\nNeed to delete the below ${count} files\n"
find $1 -type f -mtime +30
echo -e "\nTotal : ${count}\n"
if [ $2 -eq 1 ]
then
echo -e "\nDelete old files\n"
sudo find $1 -mindepth 1 -maxdepth 1 -mtime +30 -name "*log*" -type f -exec rm -rf {} \;
else
echo -e "\nNo file deleted\n"
fi