LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 12-07-2007, 08:53 PM   #1
Aleksandersen
Member
 
Registered: Feb 2007
Location: Lillehammer, Norway
Distribution: Ubuntu
Posts: 36

Rep: Reputation: 15
Recompressing if not pressent or older than original


Hi,

I want a shell script that will find all files in a directory (and it's subdirectories) with the file extensions .js, .css, and .png. It should compress files using gzip, leaving the original file and a new with a .gz extension. However if a .gz file is pressent and has an older time stamp than the original file, than the compressed file should be replaced by a newly compressed file. If the time stamp is the same as for the original file than nothing should happen.

How to do this?

(I know what I want, just not how to do it.)
 
Old 12-07-2007, 10:17 PM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Quote:
Originally Posted by Aleksandersen View Post
Hi,

I want a shell script that will find all files in a directory (and it's subdirectories) with the file extensions .js, .css, and .png. It should compress files using gzip, leaving the original file and a new with a .gz extension. However if a .gz file is pressent and has an older time stamp than the original file, than the compressed file should be replaced by a newly compressed file. If the time stamp is the same as for the original file than nothing should happen.

How to do this?

(I know what I want, just not how to do it.)
The time-stamp of the original will necessarily always be
older than the gz because your script won't be automatically
running every time you save a new version of one of your files....



Cheers,
Tink
 
Old 12-08-2007, 03:17 AM   #3
Aleksandersen
Member
 
Registered: Feb 2007
Location: Lillehammer, Norway
Distribution: Ubuntu
Posts: 36

Original Poster
Rep: Reputation: 15
Exclamation

Quote:
Originally Posted by Tinkster View Post
The time-stamp of the original will necessarily always be
older than the gz because your script won't be automatically
running every time you save a new version of one of your files....
One of the features of gzip is that it keeps the time stamps of the uncompressed file.
 
Old 12-08-2007, 03:47 AM   #4
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Not if you try to keep the original which usually done by
using gzip -c (re-direct to stdout). How *are* you going
about the archival?


Cheers,
Tink
 
Old 12-08-2007, 04:02 AM   #5
Aleksandersen
Member
 
Registered: Feb 2007
Location: Lillehammer, Norway
Distribution: Ubuntu
Posts: 36

Original Poster
Rep: Reputation: 15
Let us go about this in another way.

I have a file, original.js. I want a compressed version of that file in the same directory as the file. So I would have original.js and original.js.gz. Now, if I alter original.js then I would want to update the compressed version so that they match. Taking it one step further, say that I have a whole bunch of files in one directory and more files in subdirectories of that directory. Recompressing everything would be time consuming. So I want to recompress files that have been altered only. Using a shell script.

How would I achieve this?

Last edited by Aleksandersen; 12-08-2007 at 04:12 AM.
 
Old 12-08-2007, 01:15 PM   #6
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Ok, I've come up with a two-script solution. I couldn't quite figure
out why gzip executed from find's exec gave "strange" results.

zippo - save in your path
Code:
#!/bin/bash
cp "${1}" "${1}.bak"
gzip -fq "${1}"
mv "${1}.bak" "${1}"
The finder
Code:
#!/bin/bash
for i in `find -type f -name \*.gz`
do
  path=`dirname $i`
  file=`basename ${i} .gz`
  ori="${path}/${file}"
  
  #echo $i : $ori
  find -newer "${i}" -wholename "${ori}" -exec /path/to/zippo {} \;
done
Hope this does it for you :}


Cheers,
Tink

Last edited by Tinkster; 12-08-2007 at 01:16 PM.
 
  


Reply

Tags
compress, compression, gzip, script, shell


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Original /etc/ permissions help please bluesman2333 Linux - Newbie 2 12-06-2011 12:53 AM
what to tell the original poster to do next. titanium_geek LQ Suggestions & Feedback 7 06-12-2006 08:32 PM
LQ Would Like Original Articles XavierP Linux - News 4 12-03-2005 01:12 PM
/etc/ original permissions bluesman2333 Linux - Newbie 2 01-28-2004 08:30 AM
Original Project slinkyredfoot Linux - General 2 09-27-2003 07:30 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 05:59 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