Monday, April 29, 2024
 Popular · Latest · Hot · Upcoming
4
rated 0 times [  4] [ 0]  / answers: 1 / hits: 1588  / 3 Years ago, sun, august 29, 2021, 9:09:19

I have a few files to be backed up. I need a script to check line number of a file and if it exceeds, say 8000 lines, it will simply zip it and name it with date prefix format.
I need your help.


More From » bash

 Answers
6

This script will get the line numbers, then if it is more than 7999 (greater than or equal to 8000) it will make a .tar.gz of the file.



#! /bin/bash

lif=$(wc -l < /home/USER/path/to/file.txt)

if [ "$lif" -gt 7999 ]; then
tar /home/USER/path/to/where/you/want/the/backup/to/be/filename-$(date +%Y-%m-%d-%H:%M).tar.gz /home/USER/path/to/file.txt


Don't for get to chmod the script to get it to run - chmod +x /home/USER/path/to/script.sh



The cron command would be 0 17 * * * /home/USER/path/to/script.sh to run every dday at 5pm. Use this generator to help get the cron command if you want it to be different.



Thanks @Jnuk for the generator!


[#23944] Monday, August 30, 2021, 3 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
tonhorn

Total Points: 196
Total Questions: 118
Total Answers: 95

Location: Vanuatu
Member since Fri, May 13, 2022
2 Years ago
;