Find files of type within folder copy multiple results to different folders

Ok question number two: I'd like to search a directory for multiple file types (rar, txt, deb) and depending on what's found, copy those files to folders named Rar, TextFiles, and Debs. I'm looking for speed here so the faster the script the better. I want it to be a function that I pass 1 argument to which is the location to search. Running this function will search that directory for files and copy them to their correct folders. Here's my unfinished function:

function makeBackup ()
{
loc="$1"
if [ "$#" -eq 1 ]
then
case ${1} in
    '*.deb') test -e "$loc"; find "$loc" -type f -iname "*.deb";;
    '*.txt') test -e "$loc"; find "$loc" -type f -iname "*.txt";;
    '*.rar') test -e "$loc"; find "$loc" -type f -iname "*.rar";;
    esac
elif  [ "$#" -lt 1 -o "$#" -gt 1 ]
then
echo "Please enter one location to backup"
fi
}
I know I'm probably going to need a for loop but I'm no good with those anymore. I don't know how to combine case and for in this situation. Or, if you have a better idea that's speedy that'd be awesome. Thanks guys

How about this - it does do three searches thru the location (for each of the filetypes) but I can't think of a way to find all three filetypes and then have them moved to the right folers.

function makeBackup ()
{
    loc="$1"
    [ $# -ne 1 ] && echo "Please enter one location to backup" && return 1
    [ -d "$loc" ] || echo "Location must be a directory" || return 2
    mkdir -p $loc/Debs $loc/TextFiles $loc/Rar
    find $loc -name "*.deb" -print | xargs -r cp -n -p -t $loc/Debs
    find $loc -name "*.txt" -print | xargs -r cp -n -p -t $loc/TextFiles
    find $loc -name "*.rar" -print | xargs -r cp -n -p -t $loc/Rar
}
1 Like

I think I may need to use a loop to do it. Like, "if this directory exists, loop through while x=1 and test for filetype a, if that exists then copy it to blah, increment x to 2, if x=2 then test for filetype b, etc, etc, etc, last test end this loop. I just don't know if that's feasible and how to implement that. I'm thinking a for loop... for [x=1, something here, x++] but I'm no good with combining my tests and looping.

function makeBackup ()
{
    loc="$1"
    [ $# -ne 1 ] && echo "Please enter one location to backup" && return 1
    [ -d "$loc" ] || echo "Location must be a directory" || return 2
    mkdir -p $loc/Debs $loc/TextFiles $loc/Rar

    find $loc -type f \( -name "*.deb" -o -name "*.txt" -o -name "*.rar" \) |while read file
    do
         case ${file##*.} in
             'deb') subfolder="Debs" ;;
             'txt') subfolder="TextFiles" ;;
             'rar') subfolder="Rar" ;;
         esac
         cp $file $loc/$subfolder
    done
}
1 Like

I'm totally floored by the rapid response and the attention to detail you guys display. Holy expletive here. Thank you so much

---------- Post updated 01-28-11 at 12:09 AM ---------- Previous update was 01-27-11 at 10:41 PM ----------

So i love it, works great. One last question: can I export "$file" so that I may use the var in another script that's a bit different? I've export(ed) "$loc" but since "$file" is being created with a while loop, is there a way?

---------- Post updated at 01:58 AM ---------- Previous update was at 12:09 AM ----------

Ok last thing guys, I used rdcwayx's solution and in his function he returns a 1 or 2 depending on error status. I need to know do I use the trap command to exit my script for that? Should, instead of returning 1 or 2 should I exit 1 if i want it to stop everything at that point?

forgot to add, I'm trying to incorporate grep -n "$loc" into the "valid directory test".

[ -d $loc ]

I can't seem to get echo to allow me to use grep. I tried backticks, concatenation, and holding my breath til it worked. You shocked too? None worked. How would I say "$loc is not a valid directory, see line (grep -n $loc /this/script.sh)