Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Search Forums:



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Reply    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 02-08-2012
Registered User
 

Join Date: Mar 2011
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Bash Script to Compress All Subdirectories

I'd like to create simple bash script that, given a directory, compresses each directory by name, e.g.:

Contents of ~/Documents
Folder1
Folder2
Folder3

compress-subdirectoies.sh ~/Documents

Results:
Folder1.[some kind of compression like bzip2 or zip]
Folder2.[some kind of compression like bzip2 or zip]
Folder2.[some kind of compression like bzip2 or zip]

Any advice would be appreciated
Sponsored Links
    #2  
Old 02-08-2012
Moderator
 

Join Date: Aug 2005
Location: Saskatchewan
Posts: 12,191
Thanks: 232
Thanked 1,703 Times in 1,632 Posts
Since you have BASH, I presume you have linux.


Code:
find . -type d -mindepth 1 -maxdepth 1 | while read DIR
do
        echo tar -zcf "$DIR.tar.gz" "${DIR}"
done

Remove the 'echo' once you know it does what you want.
Sponsored Links
    #3  
Old 02-08-2012
ahamed101's Avatar
root is god!!!
 

Join Date: Sep 2008
Location: India
Posts: 1,478
Thanks: 33
Thanked 382 Times in 377 Posts
Try this...

Code:
find . -type d | xargs -I {} tar -cf {}.tar {}

--ahamed
    #4  
Old 02-08-2012
Moderator
 

Join Date: Aug 2005
Location: Saskatchewan
Posts: 12,191
Thanks: 232
Thanked 1,703 Times in 1,632 Posts
That make zips for folders inside folders, not just zips for the outside folders.
Sponsored Links
    #5  
Old 02-08-2012
ahamed101's Avatar
root is god!!!
 

Join Date: Sep 2008
Location: India
Posts: 1,478
Thanks: 33
Thanked 382 Times in 377 Posts
Quote:
Originally Posted by Corona688 View Post
That make zips for folders inside folders, not just zips for the outside folders.
mmm... maxdepth and mindepth is missing in my code...
I just created one level of folders and tested it... didn't realize that there can be subfolders too...


Code:
find . -mindepth 1 -maxdepth 1 -type d | xargs -I {} tar -cf {}.tar {}

--ahamed
Sponsored Links
    #6  
Old 02-09-2012
Registered User
 

Join Date: Mar 2011
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Wow, thank you both. Gosh, I love the "little pieces" way Unix works. Here's what I'd change:
1. move from plain text to XML as the transfer method (hidden from user) as the default communication method;
2. tell the Java people they've got the right idea but they didn't implement it right - building their apps is composed of lots of pieces (good) that are almost impossible to configure to work correctly directly (bad).
Sponsored Links
    #7  
Old 02-09-2012
Moderator
 

Join Date: Aug 2005
Location: Saskatchewan
Posts: 12,191
Thanks: 232
Thanked 1,703 Times in 1,632 Posts
Quote:
Originally Posted by furashgf View Post
Wow, thank you both. Gosh, I love the "little pieces" way Unix works. Here's what I'd change:
1. move from plain text to XML as the transfer method (hidden from user) as the default communication method;
We have a single column of data here. What value does XML add? Imagine a pipe chain failing because you got the name of a field slightly wrong in one of your commands. xml isn't appropriate for streaming, besides.

I can only see the value of XML for data which isn't easily stored in a simple, tabular format.

Last edited by Corona688; 02-09-2012 at 11:31 AM..
Sponsored Links
Reply

Tags
compress batchy

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Compress files as per timestamp in multiple subdirectories sreewin7 Shell Programming and Scripting 2 10-28-2010 12:28 PM
Bash: Gzip files in Directory and itīs Subdirectories JamesCarter Shell Programming and Scripting 2 02-10-2010 08:02 AM
Issue: Compress in unix server and FTP to windows and open the compress file using Winzip sakthifire UNIX for Dummies Questions & Answers 4 11-02-2009 08:38 AM
script needed for compress kingkhankk Shell Programming and Scripting 1 03-04-2008 07:01 AM
compress script cubicle^dweller UNIX for Dummies Questions & Answers 3 07-17-2003 09:01 AM



All times are GMT -4. The time now is 04:43 AM.