tags:

views:

27

answers:

2

Hi

New to bash scripting, I'm writing a script to copy my TV shows accross from a download folder to an archive folder.

So far I have this:

find `*`show1`*`.avi |  cp \"" $0 "\" "/mnt/main/data/tv/Show1" 
find `*`show2`*`.avi |  cp \"" $0 "\" "/mnt/main/data/tv/Show2"

I understand this is not the best method, but my skills of bash are quite limited.

I need to know how I can copy that found file, or do nothing if it doesnt find anything matching (this will be a cron script). eg.

find `*`show1`*`.avi |  cp "show1.hello.world.xvid.avi" "/mnt/main/data/tv/Show1" 
find `*`show2`*`.avi |  cp "show2.foo.bar.xvid.avi" "/mnt/main/data/tv/Show2" 
find `*`show3`*`.avi |  cp "null (nothing found)" "/mnt/main/data/tv/Show3"

Thanks!

EDIT: Solved http://pastebin.com/aNLihR86

+1  A: 
find . -name "*show1*" -exec cp {} /mnt/main/data/tv/Show1 \;

(Replace the . by the directory you want to look files into)

hellvinz
Thanks, however it returns: find /my/dir -name *show1* -exec cp {} "/mnt/main/data/tv/Show 1" \;find: show1.pdtv.avi: unknown option
Dean
I think you have forgotten to quote the name param, it should be: find /my/dir -name "*show1*" -exec cp {} "/mnt/main/data/tv/Show 1" \;
hellvinz
When I quote it, it doesnt find anything (yes a file is there). find: No match
Dean
Ok got it working. http://pastebin.com/aNLihR86
Dean
If you cp has -t option (--target-directory) you can improve efficiency with find . -name "*show1*" -exec cp -t /mnt/main/data/tv/Show1 {} +
enzotib
A: 

Bash 4+

shopt -s globstar
shopt -s nullglob
for file in **/*show*
do
  case "$file" in
   *show1* ) dest="/mnt/main/data/tv/Show1" ;;
   *show2* ) dest="/mnt/main/data/tv/Show2" ;;
   *show3* ) dest="/mnt/main/data/tv/Show2";;
  esac
  cp "$file" "$dest"
done
ghostdog74
I get an error with it: http://pastebin.com/UDVCbrv0
Dean