Search code examples
bashshellzipunzipfile-length

How to check the length of file which is inside the zip file


I want to check files inside the zip are not empty. I know the unzip -l command but it gives lot of information.

[abc@localhost test]$ unzip -l empty_file_test.zip
Archive:  empty_file_test.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
    0      07-05-2017 06:43   empty_first_20170505.csv
    0      07-05-2017 06:43   empty_second_20170505.csv
---------                     -------
    0                         2 files

I extracted the file names from the zip file by command

file_names="$(unzip -Z1 empty_file_test.zip)
file_name_array=($file_names)
file1=${file_name_array[0]}
file2=${file_name_array[1]}

I tried using -s option but not useful

if [ -s $file1 ]; then
   echo "file is non zero"
else
   echo "file is empty"    
fi

It always prints file is empty even though file is not empty.


Solution

  • unzip -l empty_file_test.zip | awk 'NR>=4{if($1==0){print $4}}'
    

    may work for you, which could also be written as

    unzip -l empty_file_test.zip | awk 'NR >= 4 && $1==0{print $4}'