bashunzip

unzip: Argument list too long


everyone. I have CSV file with ~1.5mil strings with column "ArchiveName", and i'm trying to unzip every archive with script:

#!/bin/bash
csvPath=/any/folder/ArchiveName.csv
archiveFolderPath=/any/archives
cd $archiveFolderPath
while IFS=";" read -r ArchiveName
do 
    unzipPath=/any/unzip
    sudo unzip "${ArchiveName}" -d "${unzipPath}"
done < <(tail -n +1 $csvPath)

When i'm start script - get error "Argument list too long"

i'm tried to cut csv file to 5k string, but it still doesn't work.. How i can change script or csv to make it alive?


Solution

  • "Argument list is too long" means: one of the strings you pass to sudo unzip "${ArchiveName}" -d "${unzipPath}" is too long (not: there are too many lines in your csv).

    One way to debug that issue is to check what values are passed to your command:

    echo "unzipping ${ArchiveName} ..."
    sudo unzip "${ArchiveName}" -d "${unzipPath}"
    

    If you have long csv lines where ArchiveName is the first field: you got tricked by the behavior of read.

    read will place all the remaining fields in the last variable you specify. Here is an example:

    $ IFS=";" read one <<<"foo;bar;baz"
    $ echo $one
    foo;bar;baz
    $ IFS=";" read one two <<<"foo;bar;baz"
    $ echo $one
    foo
    $ echo $two
    bar;baz
    

    In your code: ArchiveName contains the complete csv line (not: just the first field)

    Just add a second variable to your call to read :

    while IFS=";" read -r ArchiveName _discard
    do ...