Solved

Shell script to extract rar files in severa directories

Posted on 2016-11-15
7
48 Views
Last Modified: 2016-11-16
I'm working on a bash script to unrar 10 to 20 directories containing rare files, I however want all extracted files to remain in the directory where the rar files reside.
I have the below working ok if the directory name is continuos, however if the folder name is for example /rar files directorie1 where there are spaces in the dir name it fails.

Any help would be appreciated!

#!/bin/bash

while
mesg="\n==============================================\n
  1.. Unrar all files.\n
  2.. Delete rar and sfv files.\n
  3.. Exit
  \n==============================================\n
Select: \c"
do
  echo -e $mesg
  read selection
  case $selection in
  1)
    for f in `find $1 -wholename *.rar`
    do
      echo "Unpacking in directory: "`dirname $f`
      unrar e -inul $f `dirname $f`
    done ;;
  2)
    for g in `find $1 -wholename *.rar`
    do
      cd `dirname $g`
      echo "Deleting in directory: "`dirname $g`
      rm *.r?? *.url *.sfv *.nfo imdb.nfo
      rm -r Sample/
    done ;;
  3)
    exit;;
  esac
done
0
Comment
Question by:atorex
  • 3
  • 3
7 Comments
 
LVL 76

Expert Comment

by:arnold
ID: 41889154
Assign dirname to a variable, and then enclose the variable in double quotes to preserve the full name,
Alternatively, surround your `dirname ` with double-quotes or try "$(dirname $g)" as a replacement in your script.
0
 
LVL 76

Expert Comment

by:arnold
ID: 41889159
Try the following,
#!/bin/bash

while 
mesg="\n==============================================\n
  1.. Unrar all files.\n
  2.. Delete rar and sfv files.\n
  3.. Exit
  \n==============================================\n
Select: \c"
do
  echo -e $mesg
  read selection
  case $selection in
  1)
    for f in `find $1 -wholename *.rar`
    do
      echo "Unpacking in directory: "`dirname $f`
      unrar e -inul $f "`dirname $f`"
    done ;;
  2)
    for g in `find $1 -wholename *.rar`
    do
      cd "`dirname $g`"
      echo "Deleting in directory: "`dirname $g`
      rm *.r?? *.url *.sfv *.nfo imdb.nfo
      rm -r Sample/
    done ;;
  3)
    exit;;
  esac
done

Open in new window

0
 

Author Comment

by:atorex
ID: 41889786
I was having issues making it work with your proposal, my scripting skills are limited, however I came up with a hack way of doing it, I'm renaming all directories replacing the spaces with an underscore at the start of my script so now all directories are accessed with no issues.

Thanks for the input.

Regards,
Atorex
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 19

Expert Comment

by:simon3270
ID: 41889855
One other thing might fix is the "remove" section where you cd into each directory to remove the old rar files etc.

Your original code will only work if you have the full path (starting with a "/") for each directory.  If not, when you cd into the first directory, the second one won't be starting from the right place so will fail.

Fix with either this, to put the "cd" command and "rm" into a subshell (so that the "cd" is forgotten when you exit the subshell):
for g in `find $1 -wholename *.rar`
    do
      (cd `dirname $g`
      echo "Deleting in directory: "`dirname $g`
      rm *.r?? *.url *.sfv *.nfo imdb.nfo
      rm -r Sample/ )
    done ;;

Open in new window

or remember where you started and go back to it after each cd:
OLDPWD=`pwd`
    for g in `find $1 -wholename *.rar`
    do
      cd "`dirname $g`"
      echo "Deleting in directory: `dirname $g`"
      rm *.r?? *.url *.sfv *.nfo imdb.nfo
      rm -r Sample/
      cd "${OLDPWD}"
    done ;;

Open in new window

0
 

Assisted Solution

by:atorex
atorex earned 0 total points
ID: 41889921
I actually have removed this portion of the script, here is what I'm trying to do.
I have a process that is downloading some files each in its own directory, this script as you see will extract the files resulting in an .mkv file, I have another process that moved just the .mkv to my NAS storage leaving the download directory full of downloaded folders with the .rar files.
So instead of having this portion of the script I wanted to add a delete statement at the start of the script to remove the directories that have already been processed, my issue is if I do an rm -rf to all directories in this folder I may remove new downloads, I was thinking adding a flag file after the car files are extracted marking that it was completed so if the mkv is removed I would still know it was processed.
how could I
I guess I would have to figure out how to write an if statement that will do an listing and in each result if the file is a directory then validate that it has the flag file if yes delete the folder else continue?

easier said then done for me, how would you accomplish this part?
0
 
LVL 76

Accepted Solution

by:
arnold earned 500 total points
ID: 41890015
If test -d "$file"
This checks that what is referenced by $file is a directory

If test -f "$file"
Tests if the file exists and is a regular file.
0
 

Author Closing Comment

by:atorex
ID: 41890518
Thanks for the help
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Using 'screen' for session sharing, The Simple Edition Step 1: user starts session with command: screen Step 2: other user (logged in with same user account) connects with command: screen -x Done. Both users are connected to the same CLI sessio…
SSH (Secure Shell) - Tips and Tricks As you all know SSH(Secure Shell) is a network protocol, which we use to access/transfer files securely between two networked devices. SSH was actually designed as a replacement for insecure protocols that sen…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now