LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-04-2013, 03:29 PM   #1
Tchiarot
Member
 
Registered: Feb 2013
Location: Lima, Peru
Distribution: Debian
Posts: 39

Rep: Reputation: Disabled
Interrupt the script, if the compress file size more than 20M


Hi friends,

I'm trying to make a program for compress some files of a particular directory, but I want to Interrupt the process if the generated file size more than 20M.

I was Thinking in use while, but It doesn't work..
Some Idea?
Code:
Code:
#!/bin/bash
#######################
cd /home/user/
while (( $(ls -s $1$(date +%y%d%m).tgz | awk '{print $1}') < 20000 ))
 do
  tar cvzpf $1.tgz $1/ --exclude=file1 --exclude=file2 --exclude=file3
done
####################
Greetings,

Teddy Chiarot

Last edited by Tchiarot; 10-11-2013 at 11:39 AM.
 
Old 10-04-2013, 07:58 PM   #2
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
I think you could pipe it through head which will close when/if it gets too many bytes:

Code:
M=$((1024 * 1024))
tar cvp "$1/" --exclude=file1 --exclude=file2 --exclude=file3 | gzip | head -c $((20*M)) > "$1.tgz"

# see if it was interrupted
if gzip -t $1.tgz ; then
     echo "$1.tgz fits in less than 20M"
else
     echo "$1.tgz is too big"
     rm $1.tgz
done

Last edited by ntubski; 10-11-2013 at 10:41 AM. Reason: have my "when" and "if" it too.
 
2 members found this post helpful.
Old 10-11-2013, 11:38 AM   #3
Tchiarot
Member
 
Registered: Feb 2013
Location: Lima, Peru
Distribution: Debian
Posts: 39

Original Poster
Rep: Reputation: Disabled
Smile

Thanks ntubski,

It helps me a lot I could finish my script I share it with you maybe Someone found something helpful:

Code:
Code:
#!/bin/bash
MINE_CLIENT=$1
if [ "{$MINE_CLIENT:-NULL}" = "NULL" ] ; then
   echo "No se Ingreso nombre de Cliente, Escriba TarClient.sh xyz(Iniciales de Cliente)"
else
#Creando el tgz de los ddb's.
  cd /dsp/client/opns/
  mkdir ftddbs
  ls *.ddb | grep -v 0 | grep -v 1 | grep -v 2 > t
  for ddb in ` cat t ` ; do
       cp -p $ddb ftddbs
  done
  tar cvzpf ftddbs$(date +%y%m%d%H%M).tgz ftddbs
  if gzip -t ftddbs$(date +%y%m%d%H%M).tgz ; then
       echo "Se creo Satisfactoriamente el ftddbs$(date +%y%m%d%H%M).tgz"
       rm -rf ftddbs/ t
       mkdir /dsp/cli/vl$(date +%y%m%d)
       mv ftddbs$(date +%y%m%d%H%M).tgz /dsp/cli/vl$(date +%y%m%d)
  else
       echo "Ocurrio un error al crear el archivo"
  fi

#Creando el tgz del cliente.
  cd /dsp/cli/
  M=$((1024 * 1024))
  tar cvzp "$1/" --exclude=opns --exclude=surface --exclude=qt --exclude=psw/gpslog --exclude=psw/log --exclude=psw/old \
  --exclude=backup --exclude=zbckp2/ | head -c $((20*M)) > "$1.tgz"
#Revisar si el archivo pesara mas de 20 Megas.
  if gzip -t $1.tgz ; then
       echo "$1.tgz se comprimio con menos de 20 Megas"
       mv $1.tgz /dsp/cli/vl$(date +%y%m%d)
       tar cvpf vl$(date +%y%m%d).tgz vl$(date +%y%m%d)/
       rm -rf vl$(date +%y%m%d)/
  else
       echo "$1.tgz es demasiado pesado, excluya mas carpetas"
       rm $1.tgz
  fi
fi
P.D.: Sorry for the late replay, but I was very busy these days, so Thanks again.

Grettings,

TChiarot.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to compress the size of ARM9 executable file srikanth.p Linux - Newbie 1 05-01-2011 09:51 AM
Basic bash script question re: file size or # of lines in a file the_fornicator Programming 6 09-03-2009 09:41 AM
compress file size bigger than uncompress in gzip format kumartnj Linux - General 1 04-10-2009 02:28 AM
Can you write a script to display file name, Inode, and size of any file? JaxsunApex Linux - Newbie 3 01-30-2007 08:39 AM
Create a script to display file name, Inode, and size of any file. Has to be a script JaxsunApex Linux - Newbie 7 01-29-2007 08:15 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:01 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration