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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Find files of type within folder copy multiple results to different folders
# 1  
Old 01-27-2011
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:
Code:
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

# 2  
Old 01-27-2011
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.

Code:
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
}

This User Gave Thanks to Chubler_XL For This Post:
# 3  
Old 01-27-2011
Thanks again for your fast response

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.
# 4  
Old 01-27-2011
Code:
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
}


Last edited by rdcwayx; 01-27-2011 at 11:32 PM..
This User Gave Thanks to rdcwayx For This Post:
# 5  
Old 01-28-2011
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".
Code:
[ -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)

Last edited by DC Slick; 01-28-2011 at 05:20 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Copy local files to single remote host but multiple folders using rsync

I'm trying to copy a file myfile.scr from my local Linux server to multiple folders on remote AiX server using single rsync command. Below command helps me copy the file "myfile.scr" from my localhost to a remote host folder "/app/deployment/tmpfiles" rsync --delay-updates -F --compress... (1 Reply)
Discussion started by: mohtashims
1 Replies

2. Shell Programming and Scripting

Execute multiple files in multiple folders and also output on same folder

How to execute multiple files in multiple folders and also output to be generated in the same folder? Hello Team, I have a path like Sanity_test/*/* and it has around 100+ folders inside with files. I would like to run/execute those files and output of execution to be placed on same /... (1 Reply)
Discussion started by: pushpabuzz
1 Replies

3. Shell Programming and Scripting

How do you compare two folders and copy the difference to a third folder in a remote server?

How do you compare one local folder and a remote folder and copy the difference to a third folder in a remote folder.e.g. Folder A -- Is in a remote server and it has the following files TEST1.OUT TEST2.OUT TEST3.OUT Folder B --Is in a local server and it has the following files ... (5 Replies)
Discussion started by: cumeh1624
5 Replies

4. Shell Programming and Scripting

Search for specific file type in subdirectory with multiple folders

I have a directory that is in the below order (the --- is not part of the directory tree, only there to help illustrate: DATE --- main level Folder1 --- level under DATE plugin_out --- level under Folder1 variantCaller_out.40 --- level under plugin_out 001,002,003 --- level under... (3 Replies)
Discussion started by: cmccabe
3 Replies

5. UNIX for Dummies Questions & Answers

Unzipping a file which has multiple folders and each folder has the files with same name in it

Hi, I have a zipped file a.zip. This has got multiple folders in it say x and y. x contains a.txt and y contains a.txt. Is it possible to unzip this file and have the 2 files extracted and rename them to unique names. Thanks in advance. (1 Reply)
Discussion started by: arunkesi
1 Replies

6. Shell Programming and Scripting

HELP! Need to compare 2 folders on 2 different systems, and copy unmatched filenames to other folder

This has been tearing my hair out. I need to: 1: compare server1:/data/archive/ to server2:/data/archive/ (through rsync, ssh, etc) 2: filenames that don't match, get copied (scp) to server2:/data/ server1 and server2 have ssh, scp, rsync access between eachother. Is there any option in... (3 Replies)
Discussion started by: damang111
3 Replies

7. UNIX for Dummies Questions & Answers

Copy multiple files with space to folder

Please help , I am in an urgent need, Please help nawk '{for(i=1;i<=NF;i++){printf("%s\n",$i)}}' filename | sed 's/.*com//' | nawk '/pdf/ {printf("F:%s\n",$0)}' | while read line; do mv $line /images/; done the above script works for without spaces but,My path is also having some space... (3 Replies)
Discussion started by: umapearl
3 Replies

8. Shell Programming and Scripting

copy some files from users home folders to my folder

i have users home directories in /home all the users have some files starting with character e and i want to copy all these files in a folder in my (root) home using a script i tried the script for i in m5 do cd m5 cp e1* /home/pc/exam cd .. done but get these... (3 Replies)
Discussion started by: pcrana
3 Replies

9. Shell Programming and Scripting

Find all text files in folder and then copy to a new folder

Hi all, *I use Uwin and Cygwin emulator. I´m trying to search for all text files in the current folder (C/Files) and its sub folders using find -depth -name "*.txt" The above command worked for me, but now I would like to copy all found text files to a new folder (C/Files/Text) with ... (4 Replies)
Discussion started by: cgkmal
4 Replies

10. Shell Programming and Scripting

Find and replace files in multiple folders

Hi there, I would like to write a script to automate the copy and renaming of files in multiple dir. I have a generic file named s253e.prb and would like to copy this to multiple dir and rename it. Example: Dir is AL-M1 and the prb file name is AL-M1.prb. I would like to be able to... (6 Replies)
Discussion started by: lodey
6 Replies
Login or Register to Ask a Question