Unpack all RAR archive files in a directory tree

I use the following shell script to unpack all RAR files in a directory and all subdirectories. The script supports both the old and new RAR naming conventions (both .rar, .r00, .r01, … AND .partXX.rar styles).

#!/bin/bash
 
set -e
 
archives="rar partrar"
 
unpack_rar() {
  name=`echo "$1" | egrep '^.*[.]rar$' | egrep -v "[.]part[0-9]+[.]"`
  if [ ! -z "$name" ]; then
    basename=`echo "$name" | sed 's/\.rar$//g'`
    basetrunc=`basename "$basename"`
    dirname=`dirname "$name"`
    rar x -vy "$name" "$dirname" && find "$dirname" -regextype posix-egrep -regex ".*$basetrunc.*.r(ar|[0-9]+)\$" -print0 | xargs -0 -n1 rm -f
  else
    return 1
  fi
  return 0
}
 
unpack_partrar() {
  name=`echo "$1" | egrep '^.*[.]part0+1[.]rar$'`
  if [ ! -z "$name" ]; then
    basename=`echo "$name" | sed 's/\.part0\+1\.rar$//g'`
    basetrunc=`basename "$basename"`
    dirname=`dirname "$name"`
    rar x -vy "$name" "$dirname" && find "$dirname" -regextype posix-egrep -regex ".*$basetrunc.*.rar\$" -print0 | xargs -0 -n1 rm -f
  else
    return 1
  fi
  return 0
}
 
find . | while read file; do
    for arc in $archives; do
        unpack="unpack_$arc"
        $unpack "$file" && break
    done
done

Linkbacks

Use the following URL for manually sending trackbacks: http://rigo.info/lib/plugins/linkback/exe/trackback.php/en:blog:unpack_all_rar_archive_files_in_a_directory_tree
en/blog/unpack_all_rar_archive_files_in_a_directory_tree.txt · Utolsó módosítás: 2009-04-14 00:00 (külső szerkesztés)
CC Attribution-Noncommercial-Share Alike 4.0 International
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0