osx - Mass unzipping of files containing signs to be escaped and renaming of created folders using regexp -
osx - Mass unzipping of files containing signs to be escaped and renaming of created folders using regexp -
i need create simple bash 1 line sript follow:
find zip files in folder unzip each file rename result folders.the first part quite easy:
find . -type f -name '*.zip'
the problem comes next, files looks way:
ab_firstname_somestring_a!1.zip ab_secondname_somestring_a!1.zip ab_thirddname_somestring_a1.zip
etc. problem comes when using xargs - when doesn't receive escaped filenames not extract them. lastly problematic step renaming - result folders have exact same name original zip file without .zip @ end, , rename during process part remains:
firstname secondname thirdname
the thing known - name between first _ , sec _.
to expand on comments above. think looking like
class="lang-sh prettyprint-override">find . -type f -name '*.zip' -exec bash -c \ ' tar -xzf "$1" && f=$1 && f=${f#*_} && mv -iv -- "${1%%.zip}" "${f%%_*}" ' \ bash {} \;
if want else other zip
archives, can instead
find . -type f -name '*.zip' -exec bash -c ' \ if [[ $1 =~ _(.*)_(.*)_ ]]; \ tar -xzf "$1" && mv -iv -- "${1%%.zip}" "${bash_rematch[1]}"; \ else \ : #do else other zip archives, don't have 3 underscores fi ' \ bash {} \;
e.g.
class="lang-sh prettyprint-override">> tree . ├── ab_firstname_somestring_a!1.zip ├── ab_secondname_somestring_a!1.zip └── ab_thirddname_somestring_a1.zip 0 directories, 3 files > find . -type f -name '*.zip' -exec bash -c \ > ' tar -xzf "$1" && f=$1 && f=${f#*_} && mv -iv -- "${1%%.zip}" "${f%%_*}" ' \ > bash {} \; ‘./ab_firstname_somestring_a!1’ -> ‘firstname’ ‘./ab_secondname_somestring_a!1’ -> ‘secondname’ ‘./ab_thirddname_somestring_a1’ -> ‘thirddname’
osx bash shell
Comments
Post a Comment