Find all zips, and unzip in place - Unix

前端 未结 3 1934
走了就别回头了
走了就别回头了 2021-02-05 15:41

I have been trying for some time and believe I am fairly close to this, but I am fairly new to Unix so have been finding this difficult.

I have a folder, containing many

3条回答
  •  闹比i
    闹比i (楼主)
    2021-02-05 16:15

    find . -name '*.zip' -exec sh -c 'unzip -d `dirname {}` {}' ';'
    

    This command looks in current directory and in its subdirectories recursively for files with names matching *.zip pattern. For file found it executes command sh with two parameters:

    -c
    

    and

    unzip -d `dirname ` 
    

    Where is name of file that was found. Command sh is Unix shell interpreter. Option -c tells shell that next argument should be interpreted as shell script. So shell interprets the following script:

    unzip -d `dirname ` 
    

    Before running unzip shell expands the command, by doing various substitutions. In this particular example it substitutes

    `dirname `
    

    with output of command dirname which actually outputs directory name where file is placed. So, if file name is ./a/b/c/d.zip, shell will run unzip like this:

    unzip -d ./a/b/c ./a/b/c/d.zip
    

    In case you ZIP file names or directory names have spaces, use this:

    find . -name '*.zip' -exec sh -c 'unzip -d "`dirname \"{}\"`" "{}"' ';'
    

提交回复
热议问题