0

I want to compare the size and timestamp of the following jar files in separate directories.

First data is fetched by:

grep -Eo "[[:digit:]]+[[:space:]]+[[:digit:]]+.[[:digit:]]+.[[:digit:]]+[[:space:]]+[[:digit:]]+.[[:digit:]]+.[[:digit:]]+[[:space:]]+.*?test1.jar" Sample.dat

Output 1 is something like this:

29003 2015-04-24 15:56:16 XYZ_jar/java7/test1.jar

Second data is fetched by:

cd /dir1/foo/xyz/java7
stat test1.jar

Output 2 is something like this:

  File: `test1.jar'
  Size: 29003       Blocks: 64         IO Block: 1234   regular file
Device: ab12c/34567d    Inode: 1234567     Links: 1
Access: (0123/-rwxr-xr--)  Uid: (123456/foo)   Gid: ( 1234/  fooooo)
Access: 2015-06-01 04:00:03.000000000 -0500
Modify: 2015-04-24 15:56:16.000000000 -0500
Change: 2015-06-01 00:13:01.000000000 -0500

I need to compare the size and timestamp of Output 1 which is 29003 and 2015-04-24 15:56:16 respectively, to the size and Modify date of Output 2 , which is 29003 and Modify: 2015-04-24 15:56:16.000000000 -0500 respectively.

Current code/script based from answer:

cd /dir1/foo
output1=$( grep -Eo "[[:digit:]]+[[:space:]]+[[:digit:]]+.[[:digit:]]+.[[:digit:]]+[[:space:]]+[[:digit:]]+.[[:digit:]]+.[[:digit:]]+[[:space:]]+.*?test1.jar" Sample.dat )

cd /dir1/foo/xyz/java7
size=$( stat -c "%s" $test1.jar )


refdate=$( awk '{print $4}' <<< "$output1" )
modt=$( stat -c "%y" yourfile2 | awk '{print $1}' )

print $refdate
print $size
print $modt


if [[ "$modt" == "$refdate" ]]
then echo equal date
else echo different date
fi
0

You can use format specifiers in stat to get the specific information:

stat -c "%s %Y" yourfile

To assign it individually for later comparison:

size=$( stat -c "%s" yourfile )
modt=$( stat -c "%Y" yourfile )

To compare an attribute of two files you may use, e.g.:

size1=$( stat -c "%s" yourfile1 )
size2=$( stat -c "%s" yourfile2 )

if [[ $size1 == $size2 ]]
then echo equal size
else echo different size
fi

This can also be done inline; using the arithmetic command it's:

if (( $( stat -c "%s" yourfile1 ) == $( stat -c "%s" yourfile2 ) ))
then echo equal size
else echo different size
fi

To compare against a string containing an ISO date in field 4 you need stat -c "%y", e.g.:

refdate=$( awk '{print $4}' <<< "${output1}" )
modt=$( stat -c "%y" yourfile2 | awk '{print $1}' )

if [[ "$modt" == "$refdate" ]]
then echo equal date
else echo different date
fi
  • how do I compare them with output 1? I think I made the wrong approach with the perl command – Philip Morris Jun 1 '15 at 15:56
  • @Philip Morris; I added the test to my answer. – Janis Jun 1 '15 at 16:04
  • but my first output is not a file. it is just a string for sample.dat . Also modt=$( stat -c "%Y" yourfile ) returned something like 1429908976 and not something like 2015-04-24 15:56:16 – Philip Morris Jun 1 '15 at 16:05
  • Can you help me reconstruct my 1st command for output 1 so it can be used as a variable? – Philip Morris Jun 1 '15 at 16:06
  • @Philip Morris; I extended my answer. (Note the number you see with %Y are the seconds since Epoch; if you compare dates this form is usually more flexible. In your case you want to compare an ISO date format though, so you have to use %y instead (see my updated nswer).) – Janis Jun 1 '15 at 16:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.