At an early stage of my script, it needs to unzip "x" amount of files provided by the user. Across the zipped file set, there are name-duplicates (but their contents differ from each other); i.e. both zip1.zip and zip2.zip contain the identically-named file "d09483272".

One could just unzip multiple files by doing unzip \*zip -d $M_DIR but this is inefficient -for my case- as newly decompressed files overwrite those already in the $M_DIR location.

Thought about decompressing them with a timestamp - some weird nested loop like the following (which clearly doesn't work)

    for zip in *zip; do
            unzip -l *zip | while read file
                    unzip -p *.zip ${file} > $(date "+%H:%M:%S:%s%N").pdf

If this is not an appropriate approach, probably there is a simpler way of doing this? Thoughts?

  • 1
    Why not unzip each zip file to a different folder?
    – finswimmer
    Aug 20, 2019 at 19:13
  • What do you want to end up with, for the duplicates?
    – Jeff Schaller
    Aug 20, 2019 at 19:30
  • The contents of all files -including the "name-duplicates" will be processed and pieced together at a later by the script - so no file should be left behind sort to speak.
    – madArch
    Aug 20, 2019 at 19:34

2 Answers 2


Have a look at the unzip-manpage:

 -n     never overwrite existing files.  If a file already exists, skip the extraction
        of that file without prompting.
  • Thanks, I did check but unfortunately the -n option doesn't work in this case because I would need the contents of both files - even though these may seem as duplicates, their contents differ from each other.
    – madArch
    Aug 20, 2019 at 18:57

I rushed into a non-elegant quick fix, due to time constraint.

    for zip in $T_DIR*zip; do
            unzip $zip -d $M_DIR;
            rename "s/.pdf/$(date "+%s%N").pdf/" ${M_DIR}*;

Each file is individually unzipped onto the $T_DIR folder, followed by adding a nano-second timestamp to their names. When the next wave of unzipped files come into the $T_DIR folder, there will no longer be any "name-duplicate". Thoughts on a faster/ improved solution?

This would only work with the caveat that the "name-duplicates" files exist in separate zip files.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .