LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Create folder from .rar or .r00 file with filename excluding the extension (usinBash) (http://www.linuxquestions.org/questions/linux-newbie-8/create-folder-from-rar-or-r00-file-with-filename-excluding-the-extension-usinbash-693391/)

Techno Guy 12-28-2008 02:42 AM

Create folder from .rar or .r00 file with filename excluding the extension (usinBash)
 
Hi, I have been reading posts in this forum for a few days now, and they have been very helpful, but today I got stuck and after a few hours trying to figure out how to get this done I got a way of doing it, but it has still has errors and im sure it's not the proper way.

So I would be greatfull if someone could help me out here.

My question is as follows:
Im trying to find all files that are ether .r00 or .part1.rar in the current directory, which I have done. Then I want to create a folder from each of them and take out the extension in the folder name which it does too but obviously not the correct way, as it still comes up with errors (such as "file exists") but since I do it twice it gets my folders created, but is there a better/correct way of doing this?

Is there a way that I can do something like this: DIRNAME=${i%.r0*0 OR i%.part1.rar} .....

This is what I am doing at the moment:
Code:

#!/bin/bash
for i in `ls *.r0*0 *.part1.rar`; do
DIRNAME=${i%.r0*0}
mkdir $DIRNAME
DIRNAME=${i%.part1.rar}
mkdir $DIRNAME
done

Your help would be greatly appreciated.

Disillusionist 12-28-2008 05:21 AM

Welcome to LQ.

Try the following:

Code:

#!/bin/bash
for i in `ls *.r0*0 *.part1.rar`
do
  DIRNAME=${i%.r0*0}
  if [ "$DIRNAME" == "$i" ]
  then
      DIRNAME=${i%.part1.rar}
  fi
  if [ ! -f $DIRNAME ]
  then
      mkdir $DIRNAME
  fi
  mv $i $DIRNAME/.
done


Techno Guy 12-28-2008 02:28 PM

Fantastic, thanks Disillusionist! it worked perfectly.

I found out this morning that I needed a 3rd find/replace extension and I managed to cobble together some code from yours and also added a unrar function so it dumps the rar contents into the new folder of the rar files.....

Here is the code im using now (i'v given it here incase some one else one day might need it....)

Code:

#!/bin/bash
for i in `ls *.r0*0 *.part1.rar *.part0*1.rar`
do
  DIRNAME=${i%.r0*0}
  if [ "$DIRNAME" == "$i" ]
  then
      DIRNAME=${i%.part1.rar}
  fi

  if [ "$DIRNAME" == "$i" ]
  then
      DIRNAME=${i%.part0*1.rar}
  fi

  if [ ! -f $DIRNAME ]
  then
      mkdir $DIRNAME
        cd $DIRNAME
        rar x ../$i
        cd ..
  fi
  # mv $i $DIRNAME/.
done


jschiwal 12-28-2008 03:30 PM

Code:

for i in `ls *.r0*0 *.part1.rar *.part0*1.rar`
The ls command isn't needed. Why do people use it? The expansion occurs before the ls command is run.

This will suffice:
Code:

for i in *.r0*0 *.part1.rar *.part0*1.rar

Disillusionist 12-28-2008 04:05 PM

EDIT: my original code was flawed and would attempt to create a directory each time.

So that you only create the directory once, use:

Code:

  if [ ! -f $DIRNAME ]
  then
      ###
      ### $DIRNAME is not a "regular file"
      ###
      if [ ! -d $DIRNAME ]
      then
        ###
        ### $DIRNAME is not a directory either (so create it)
        ###
        mkdir $DIRNAME
      fi
      cd $DIRNAME
      rar x ../$i
      cd ..
  else
      echo "$DIRNAME is a file!  not processing $i"
  fi


Techno Guy 01-03-2009 02:09 AM

Thanks jschiwal, now that I look at it, yea I can do with out the "ls", i'v taken it out of my other code and it all seems to still be working :)

Thanks again to Disillusionist, the new code looks much more efficient then the old one, it seems to be working fine too, but I wont know for sure till tomorrow when I get to do a proper test, will report back if there are any issues.

Disillusionist 01-03-2009 04:35 AM

Quote:

Originally Posted by Techno Guy (Post 3395377)
Thanks again to Disillusionist, the new code looks much more efficient then the old one, it seems to be working fine too, but I wont know for sure till tomorrow when I get to do a proper test, will report back if there are any issues.

That was just a snippet, the complete code (with Jschiwal's ammendment) is:
Code:

#!/bin/bash
for FILE in *.r0*0 *.part1.rar *.part0*1.rar
do
  DIRNAME=${FILE%.r0*0}
  DIRNAME=${DIRNAME%.part1.rar}
  DIRNAME=${DIRNAME%.part0*1.rar}

  if [ ! -f $DIRNAME ]
  then
      ###
      ### $DIRNAME is not a "regular file"
      ###
      if [ ! -d $DIRNAME ]
      then
        ###
        ### $DIRNAME is not a directory either (so create it)
        ###
        mkdir $DIRNAME
      fi
      cd $DIRNAME
      rar x ../$FILE
      cd ..
  else
      echo "$DIRNAME is a file!  not processing $FILE"
  fi
done


Techno Guy 01-04-2009 12:13 AM

Oh, ok. The incomplete one still kinda worked, until this morning when I tried it with a bunch of files, was wondering if I was missing something but since it worked as was I didn't think any further :)

But yea im using the new code in my script now and all seems fine now too :)

Thanks again so much for all your time and help! (maybe one day I will help you :) )


All times are GMT -5. The time now is 10:28 AM.