|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
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
|
|||
|
|||
|
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? |
|
#2
|
|||
|
|||
|
echo $1 | grep 'gz$' > /dev/null
if [ $? -eq 0 ] then do something fi ls -l <filename> will show the date of the last edit |
|
#3
|
|||
|
|||
|
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. |
![]() |
| Viewing: Dev Shed Forums > Operating Systems > UNIX Help > shell scripting |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|