LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
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 03-26-2013, 09:43 AM   #1
Ivano
LQ Newbie
 
Registered: Mar 2013
Posts: 7

Rep: Reputation: Disabled
[BASH] How to create *.tgz file?


Hello!

Script to do:
For every first-level, older than 3 months directories check if it has more than 1000 files in whole structure:
* yes - for every second-level directory do *.tgz file, check if archive is buillt correctly and remove directories that are archived,
* no - do nothing with it.

Here is what I have so far. My main problems:
1. How to create archive for second-level directories?
2. How to fix problem with empty spaces in directory names?
3. How to correctly write part with checking *.tgz files and removing archived directories (now I'm getting errors: ls: you don't have permission to /root/dir/dir: There is no such file or directory)?

Code:
for DIR in `find /root/* -maxdepth 1 -type d -mtime +90` ; do
  if [[ ! -f "$DIR.tgz" ]] ; then
    if [[ `ls -1R $DIR | wc -l` -gt 1000 ]] ; then
      tar czf $DIR.tgz $DIR
    fi
  fi
	
  if [[ -f "$DIR.tgz" ]] ; then
    rm -rf $DIR
  fi
done
 
Old 03-26-2013, 10:12 AM   #2
mina86
Member
 
Registered: Aug 2008
Distribution: Slackware
Posts: 374

Rep: Reputation: 150Reputation: 150
Unfortunately, because file names can have new line characters in them, find is highly limited command, which is hard to use right. To improve things over what you are doing now you cod do:
Code:
find /root/* -maxdepth 1 -type d -mtime +90 | while read DIR; do
    …
done
but the problem with new line characters remains.

Second of all, always quote your variables. It's not always required, but if you are unsure, quote it.

Furthermore, $(…) is preferred over backtics. And lastly, you should clean up .tgz file if tar command fails. Lastly, tar's return code can be used to determine whether you need to remove the directory.

An as for counting number of files inside of a directory, find works better than ls -lR.

Code:
if ! [ -f "$DIR.tgz" ] &&
   [ "$(find "$DIR" -type f -printf \\n | wc -l)" -gt 1000 ]; then
    if tar czf "$DIR.tgz" "$DIR"; then
        rm -r "$DIR"
    else
        rm -f "$DIR.tgz"
    fi
fi
To solve problem with new lines in directory names pointed out at the beginning, the easiest way is to create another command for dealing with a directories, eg.:

blah.sh:
Code:
find /root/* -maxdepth 1 -type d -mtime +90 -exec sh blah-helper.sh {} +
blah-helper.sh:
Code:
for DIR; do
    …
done
If you don't have GNU find, replace “+” at the end of the find with “\;”.

Last edited by mina86; 03-26-2013 at 10:15 AM.
 
1 members found this post helpful.
Old 04-24-2013, 05:00 PM   #3
Ivano
LQ Newbie
 
Registered: Mar 2013
Posts: 7

Original Poster
Rep: Reputation: Disabled
I'm sorry for no response but I had problems with my health.

I did changes and code looks like this:

Code:
find /root/ -maxdepth 1 -type d -mtime +90 | while read DIR ; do

if ! [[ -f "$DIR.tgz" ]] &&
     [[ "$(find "$DIR" -type f -printf \\n | wc -l)" -gt 1000 ]] ; then
         if tar czf "$DIR.tgz" "$DIR" ; then
             rm -r "$DIR"
         else
             rm -f "$DIR.tgz"
         fi
fi

done
Everything works fine but I have one questions. We have structure of folders like this:
Folder 1
* Subfolder 1
* Subfolder 2
Folder 2
* Subfolder 1
* Subfolder 2
Script checks if there is 1000 files in Folder 1, Folder 2 and compress it. My question: can it do same thing but compress only Subfolder 1 and Subfolder 2 when there is 1000 files in whole Folder 1? I tried do to something but everything I do fails.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
[SOLVED] Need help create a bash script to edit CSV File imkornhulio Programming 13 02-05-2009 10:23 AM
Create a bash file trolojik1 Programming 6 05-26-2008 02:48 AM
Create a text file using Bash Boffy Programming 5 03-14-2005 09:06 PM
tgz file doesn't end in .tgz? detpenguin Slackware 4 05-15-2004 07:13 PM
howto create a file based on date in bash rohan208 Linux - Newbie 2 05-07-2004 03:54 PM


All times are GMT -5. The time now is 09:21 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration