SunQuest
           UNIX Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsOperating SystemsUNIX Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Get inside! Sample the range of functionality easily built with JMSL Library for Time Series Data Analysis, Heat Maps, Portfolio Optimization, Monte Carlo Simulation, Stock Price Charting and more. Download Now!
  #1  
Old July 14th, 2003, 12:15 AM
granmastaraj granmastaraj is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 7 granmastaraj User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
shell scripting

Hello all,
I am trying to write a shell script. Does anyone know how to check in a shell script to see if a file is a gzip. For example, I want to write something similar to this pseudocode
if( $1 is gz)
then do something
else
do something esle

Also, does anyone know how to tell the last time a file was edited meaning how old the file is?

Reply With Quote
  #2  
Old July 14th, 2003, 12:22 PM
granmastaraj granmastaraj is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 7 granmastaraj User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
echo $1 | grep 'gz$' > /dev/null
if [ $? -eq 0 ]
then
do something
fi


ls -l <filename> will show the date of the last edit

Reply With Quote
  #3  
Old December 23rd, 2003, 07:02 AM
izn izn is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2003
Posts: 4 izn User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
two other ways where you do not rely on file extension:

1/ gzipped files always start with the same 2 bytes which are: "8b1f" hexadecimal. So let have a look at it:
hexdump some_gzip_file.gz | head -n 1 | cut -f2 -d' '
8b1f
hexdump does what it says. we take the first line of the output and then the second column which is the first 2 bytes of the file. We can store the value in a variable and then make some tests.
GZIP_WORDS=`hexdump some_gzip_file.gz | head -n 1 | cut -f2 -d' '`
test "$GZIP_WORDS" = "8b1f" && echo "gzip here"

2/ Why bother when we have nifty tools doing the job for us:
file -b -n some_gzip_file.gz gzip compressed data, deflated, last modified: Thu Jan 1 01:00:00 1970, os: Unix
Cutting for the first word will give the answer you need:
file -b -n smtp-vilter-1.0.5.tar.gz |cut -f 1 -d' '
gzip
man file for more information.

Reply With Quote
Reply

Viewing: Dev Shed ForumsOperating SystemsUNIX Help > shell scripting


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway