0

Am creating a zip file in the script, when I run the script and go to the target folder and unzip the zip file I created in script, it's creating a directory and unzipping the file in that directory.

Below is the code in the script.

for fname in `cat $Filenm`
 do
  echo $fname
  fnme=$(echo ${fname}|awk -F\/ '{print $8}')
  echo $fnme>>$scriptLog
  tofilename="${date_new}_${fnme}"
   zip -r $tofilename.zip $fileDir/$fnme
  rm $fileDir/$fnme
  mv $tofilename.zip $todir

When I go to $todir after the script execution and unzip $tofilename.zip, it's creating the $fileDir in the $todir and unzipping the file there, so in $todir I have to go into like 5 sub directories to see my text file.

I want the text file in the zip file to be unzipped into the $todir, but not the way its happening.

Thanks in advance.

2

You can use the -j option to zip (-j for "junk paths") which will discard the name of the directory and keep only the file name.

Also, you don't need the -r option (-r for "recurse into directories") since you're passing it a single file name (the -r option is useful when you want to store a whole directory tree into the zipfile.)

So this line should be enough to store the file in the zipfile without the directory name:

zip -j $tofilename.zip $fileDir/$fnme

Another option is to keep storing the full directory path in the zipfile and simply discarding it when you unpack it, by passing the unzip command the -j option, which works similarly to how this option works when zipping.

  • 1
    Thanks a lot Filipe Brandenburger , i changed it to zip -j $tofilename.zip $fileDir/$fnme , it worked. – New_user Jul 28 '18 at 5:22

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.