Unzip in bash script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Unzip in bash script
# 8  
Old 02-10-2010
I ll have minimum 15 main directories under each there are 10 -15 sub-directories

Each subdirectory will hold minimum 10000 files.

Thanks,
Vidhya
# 9  
Old 02-10-2010
You can only count yhe number of files processed in each subdir.
Code:
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

# 10  
Old 02-10-2010
Fran,

Yeah ...only a miminal information - total files in each sub-directory can be given.

Thanks for your inputs Smilie the above script solves my problem.
# 11  
Old 02-10-2010
Quote:
Originally Posted by frans
You can only count yhe number of files processed in each subdir.
Code:
for j in $(find $i -name "abc*" -type d)


Not only is find unnecessary, but it will break your script if any directory names contain whitespace or other pathological characters.

Use wildcard expansion directly:
Code:
for j in abc/*

Quote:
Code:
do
    (( n+=$(find $j -type f | wc -l) ))


That syntax is not portable; use the POSIX syntax:
Code:
n=$(( n + $(find $j -type f | wc -l) ))

However, you don't need find or wc; it can be done in the shell itself:
Code:
set -- *
n=$(( $n + $# ))

Quote:
Code:
    if ! unzip -qT $j/\* 2>>/dev/null
    then
        for k in $(find $j -type f)


Not only is find unnecessary, but it will break your script if any filenames contain whitespace or other pathological characters.
Quote:
Code:
        do
            unzip -qT $k 2>>/dev/null;
        done
    fi
    echo "$n files processed"
done

# 12  
Old 02-11-2010
Johnson,

Thanks for your corrections. Im a beginner and your inputs helped me to learn more stuff

I tried checking in few tutorials, but i didnt understand.Can you please clarify me on the behaviour of two lines of code

Code:
  set -- *
n=$(( $n + $# ))

Thanks,
Vidhya
# 13  
Old 02-11-2010
Code:
set -- *

sets the filelist (with wildcard * but could be i.e. *.txt) as the parameters $1, $2 ...
Code:
n=$(( $n + $# ))

$# returns the number of parameters --> the number of files in this case, and increment n by that number
# 14  
Old 02-11-2010
Thanks frans Smilie
Below is my final code draft
Code:
 
#!/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
Quote:
1.Processing directory : /store/archive/TP_0000000000000000478
7228 - value of n
2. Processing directory : /store/archive/TP_0000000000000000486
7228 - same count...since this directory doesnt have any files
3.Processing directory : /store/archive/TP_0000000000000000488
7942
If I use stmnt 2the output is wrong
Quote:
1. Processing directory : /store/archive/TP_0000000000000000478
949
This directory has following sub dir
/store/archive/TP_0000000000000000478/L000/L100/L200
..
/store/archive/TP_0000000000000000478/L000/L100/L272

Only the inner most directory contains the zip files

2.Processing directory : /store/archive/TP_0000000000000000486
962
Processing directory : /store/archive/TP_0000000000000000488
1066
Any help pls..

Thanks
Vidhya.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Unzip the .zip file without using unzip utility in UNIX

I have .zip file, i want to list all the files archived in the zip file. unzip utility is not working for me in unix. Please help me resolve this issue Thanks ganesh. (3 Replies)
Discussion started by: Ganesh L
3 Replies

2. Shell Programming and Scripting

Using Shell Script in place of Perl script to Unzip the zip files.

Hi Expert, We have some shell scripts which Internally uses Perl Script to Unzip the source zip files which comes to inbound directory. So now our requirement is to avoid the dependency on Perl Script and us Shell Script to unzip the files. I have the Perl script with me attached can some one... (3 Replies)
Discussion started by: naveen.dasu
3 Replies

3. Shell Programming and Scripting

How to Unzip a .ZIP file in Unix without using unzip cmd..?????

Hi All I have ftped a .ZIP file (zipped using WinZip in Windows) to my Unix server (HP-UX). I don't have unzip cmd available in my curent Unix version Please let me know any cmd in UNIX (other than unzip) using which I can unzip this .ZIP file . Please elaborate on the commands aval and... (5 Replies)
Discussion started by: sureshg_sampat
5 Replies

4. Shell Programming and Scripting

SFTP / UNZIP script issues

Hi everyone, i'm having a little trouble wih my first shell script ever. So the point of that script is to: -Archive Zip files in a directory -Remove txt files from that directory -connect through sftp and a rsa key to a remote server -download a couple of files -unzip downloaded files ... (0 Replies)
Discussion started by: Peanutz
0 Replies

5. Shell Programming and Scripting

unzip via bash startup script problems

i have two lines in my rc.local file that are wget -O/<path>/<file>.zip url://domain.com unzip -o /<path>/<file>.zip the wget works fine, but the unzip won't work. when i copy/pase the unzip line to the prompt it works fine. i thought that maybe the unzip was running before the wget... (0 Replies)
Discussion started by: easysnowboards
0 Replies

6. Shell Programming and Scripting

Script to unzip specific files

As part of an audit at work I need to review a lot of Windows workstations. One thing I need to do is review all of the .doc files. My problem is that I guessed a number of these files would be in zip archives. This is the case but they are mixed in with a lot of other file types that I am not... (1 Reply)
Discussion started by: stumpyuk
1 Replies

7. Shell Programming and Scripting

Shell script for unzip

All, I'm having 4 .zip files that are coming from FTP. I need to unzip those files and put that files into another folder. Can anyone help me how to write a shell script to check wether .zip files are located in FTP folder, if condition true then how to unzip and put it in another folder. Thanks in... (2 Replies)
Discussion started by: nvkuriseti
2 Replies

8. HP-UX

How to Unzip a .ZIP file in Unix without using unzip cmd..?????

Hi All I have ftped a .ZIP file (zipped using WinZip in Windows) to my Unix server (HP-UX). I don't have unzip cmd available in my curent Unix version Please let me know any cmd in UNIX (other than unzip) using which I can unzip this .ZIP file . Please elaborate on the commands aval and... (5 Replies)
Discussion started by: sureshg_sampat
5 Replies

9. Shell Programming and Scripting

Problems writing bash script to unzip files

I'm getting the following errors when I try to write a script to unzip some zip files. When I use the free trial copy of the commerical winzip program, however, they work fine. When I use -l or -t on unzip it indicates no errors. When I use the -o switch interactively from the bash command line it... (1 Reply)
Discussion started by: siegfried
1 Replies

10. Shell Programming and Scripting

unzip in script using rsh

Hi I have searched usr/bin and usr/sbin to try to find unzip but I cannot find it. My script is using the ksh #!/usr/bin/ksh but when I rsh to another box it cannot find unzip to unzip to a new archive, but I can unzip files when I am using a telnet session on the box. The error message... (4 Replies)
Discussion started by: speedieB
4 Replies
Login or Register to Ask a Question