Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question

closed as too broad by Braiam, Alvin Row, Wilf, bain, Luis Alvarado Jul 29 '14 at 1:45

There are either too many possible answers, or good answers would be too long for this format. Please add details to narrow the answer set or to isolate an issue that can be answered in a few paragraphs.If this question can be reworded to fit the rules in the help center, please edit the question.

What about using a version control system instead? – choroba Jul 28 '14 at 12:55
@choroba what do you mean with control system? I have no info about it. – kenn Jul 28 '14 at 12:57
Check Revision control. Check SVN, Git, and so on. – choroba Jul 28 '14 at 12:59
@choroba yes, the terminology fits in my case. I believe there are powerful tools to manage it. I ll take a look at it in detail. – kenn Jul 28 '14 at 13:08
Is this a log file? Did you look into logrotate? – Digital Chris Jul 28 '14 at 15:23
up vote 4 down vote accepted

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/

The cron command would be 0 17 * * * /home/USER/path/to/ 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!

share|improve this answer
To execute it automatically, you can set up a cronjob. You can f.e. use this generator – jnuk Jul 28 '14 at 13:04
Thank you Tim. Your script is what was in my mind – kenn Jul 28 '14 at 13:05
@Tim For sure :) You deserved that – kenn Jul 28 '14 at 13:11
@Tim don't parse the output of cat command. Just wc -l < file would be enough. – Avinash Raj Jul 28 '14 at 13:20
@Avinash Thanks, edited! – Tim Jul 28 '14 at 13:21

Here is a sample script that does what you describe


LINES=$(cat "$FILENAME" |wc  | awk '{print $1}')
DATE=$(date +'%Y-%m-%d')

        tar xvf ""backup_$DATE.tar"" "$BACKUP_PATH"

if [ "$LINES" -gt "$MAXLINE" ];

First it declares som variables,

FILENAME is the file you want to check the length of.

LINES iis the amount of lines we find i the file we check

MAXLINES is the max amount of lines we allow before making a backup.

DATE is todays date in Year-Month-Day format

BACKUP_PATH is the directory you are going to backup.

do_backup is the function that contains backup command

save the script into a file in terminal:

chmod +x  script_name

to make it executable

./script_name /path/to/file_to_check_rows_of

If you want to understand more of how the script works try running it with:

sh -x script_name

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.