First for loop - checks if it can access zip parameters in a directory
If unzip fails for any of the file in that directory, then it goes inside the second for loop - to list which file is that
My doubt is..Is there any way I can tell status for every 5000 files in the first for loop. like processed 5000 files...10000 files etc...for monitoring
4.I can very well keep a counter and do this logic in second for loop, since it loops every file.but this will execute only when there are corrupted files
for j in `find $i -name "abc*" -type d`; do
unzip -qT $j/\* 2>>/dev/null;
if [ $? -ne 0 ]; then
for k in `find $j -type f`; do
unzip -qT $k 2>>/dev/null;
done
fi
done
for j in `find $i -name "abc*" -type d`; do
// Here $i is directory.
((i++)); (($i%5000)) || echo "$i files processed"
// Here it will display the count of sub-directories. I need to display list of
files in sub-directories.
Apologize : Replace i with another unused letter !
I think you could try to avoid the first check (unzip -qT $j/\*), believe that it should'nt slow-down the script to much and you don't need to look twice if there is an error with unzip.
I suggest (some modif. to make it more readable)
for FILE in $(ls abc*/*) # This should work
do
((n++)); (($n%5000)) || echo "$n files processed"
unzip -qT $FILE 2>>/dev/null || echo "Error in file $FILE"
done
for FILE in $(ls $j) # This should work
// $j - last level subdirectory which has list of zipfiles without .zip extension
do
((n++)); (($n%5000)) || echo "$n files processed"
unzip -qT $FILE 2>>/dev/null
if [ $? -ne 0 ]; then
echo $FILE is BAD
fi
done
Instead if I use as I mentioned in the below code it gives the correct output.
for k in `find $j -type f`; do
performance :
if I run the code with the 2 loops - 1. scan all the files in the directory with one unzip system call
2. If any of the dir has corrupted files, loop the second directory and list the files
It takes 0m5.186s this time to execute 1.3 lakhs files
If i remove the outer loop, it takes 1m46.280s to execute 1.3 lakh files
Our server will hold data close to 1.4 TB. There is a performance hit in these 2 ways of implementation. Thats the reason, I wanted to count the files in the outer for loop (while testing the unzip for all the files in one sub-directory)
You can only count yhe number of files processed in each subdir.
for j in $(find $i -name "abc*" -type d)
do
(( n+=$(find $j -type f | wc -l) ))
if ! unzip -qT $j/\* 2>>/dev/null
then
for k in $(find $j -type f)
do
unzip -qT $k 2>>/dev/null;
done
fi
echo "$n files processed"
done
#!/bin/bash
bfile=/tmp/BAD_BITFILES_`hostname`;
for i in /store/archive/TP_* ; do
echo "Processing main directory : $i"
for j in $i/L*/L*/L2*; do
echo $j
# n=$(( n + $(find $j -type f | wc -l) )) - statement 1
set -- *
n=$(( $n + $# )) - statement 2
unzip -qT $j/\* 2>>/dev/null;
if [ $? -ne 0 ]; then
for k in $j; do
unzip -qT $k 2>>/dev/null;
if [ $? -ne 0 ]; then
echo $k is BAD;
echo $k >> $bfile;
fi
done
fi
done
echo $n
done
If i replace stmnt 1 with stmnt 2 (highlighted in brown color in the above code)as suggested by johnson, the output varies
If I use stmnt 1 the output is and this is the right count of files