I am using for loop for unzipping multiple files:

ls *.zip | for zipfile in *.zip;
    unzip "$zipfile"

If while unzipping two zip files, there is a file with same name in both zip then I want to concatenate those both files into single file with same name in same folder.

How to do it?


Assuming there are no directories in the archive:

mkdir tmpdir1 tmpdir2 targetdir
unzip -d tmpdir1 "$zipfile1"
unzip -d tmpdir2 "$zipfile2"
cd tmpdir1
for file in *; do
    if [ -f "../tmpdir2/${file}" ]; then
        cat "$file" "../tmpdir2/${file}" >"../targetdir/${file}"
        mv $file" "../targetdir/"
cd ../tmpdir2
mv * targetdir/
  • Is there any way to set a condition like if file with same name found then concatenate it or else leave it as is? As I have multiple zip files and this problem is only for some of them, the way you told is difficult to implement – varad Sep 5 '17 at 9:02

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.