1

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.

Aniket Kulkarni
  • 12,825
  • 9
  • 67
  • 90

2 Answers2

5
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}'
sjsam
  • 21,411
  • 5
  • 55
  • 102
1

You could format the output fo unzip -l

unzip -l test.zip | awk '{print $1 "\t"  $4}' | tail -n+4 | head -n-2

Explanation:

unzip -l unzips the files and returns the deisred information

awk '{print $1 "\t" $4}' prints columns 1 and 4 (size and filename)

tail -n+4 strips the first few lines from the output (removing the header and unwanted information)

head -n-2 strips the last two lines from the output (removing the unwanted summary)

Edit:

To store empty files into an array you can map the output of the comand:

read -r -a array <<< `unzip -l test.zip | awk '{print $1 "\t"  $4}' | tail -n+4 | head -n-2 | awk '{if($1==0) print $2}'`

Explanation

unzip -l test.zip | awk '{print $1 "\t" $4}' | tail -n+4 | head -n-2 is explained above

awk '{if($1==0)}{print $2}' just gives you the filenames of the empty files

<<< inputs the output of the command in backticks `` into the read command

read -r -a array reads the input into the variable array

BUT

You could just use the shorter command of Sjsam and do the same:

read -r -a array <<< `unzip -l empty_file_test.zip | awk 'NR>=4{if($1==0){print $4}}'`

read -r -a array is explained above

<<< is explained above

awk 'NR>=4{if($1==0){print $4}}'

  • NR>=4 puts out every line > 4 (strip the header and unwanted output)
  • if($1==0){print $4}} if the size ($0) is 0 it executes {print $4}
  • {print $4} outputs the filename
Community
  • 1
  • 1