0

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

Dean
  • 755
  • 3
  • 15
  • 31
  • A Kind of duplicate: http://stackoverflow.com/questions/1313590/bash-copy-all-files-except-one – Wok Oct 08 '10 at 12:59

2 Answers2

3
find . -name "*show1*" -exec cp {} /mnt/main/data/tv/Show1 \;

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

hellvinz
  • 3,460
  • 20
  • 16
  • Thanks, however it returns: find /my/dir -name *show1* -exec cp {} "/mnt/main/data/tv/Show 1" \; find: show1.pdtv.avi: unknown option – Dean Oct 08 '10 at 13:03
  • 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 Oct 08 '10 at 13:27
  • When I quote it, it doesnt find anything (yes a file is there). find: No match – Dean Oct 08 '10 at 13:28
  • 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 Oct 08 '10 at 20:22
0

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
  • 327,991
  • 56
  • 259
  • 343