Results 1 to 7 of 7

Thread: [SOLVED] bash script to move files

  1. #1
    Join Date
    Apr 2006
    Beans
    71
    Distro
    Ubuntu 7.04 Feisty Fawn

    [SOLVED] bash script to move files

    This is probably not as difficult as I'm making it, but I was wondering if anyone else might have some suggestions.

    Here's what I want to do:

    I'm trying to write a script to check a directory for a specific filetype (.torrent in this case) and then prompt for a directory to move each file to, one by one.

    so if the script finds:
    Code:
    foo.torrent
    foobar.torrent
    foobarmoo.torrent
    it will then ask which directory I want to move foo.torrent too, do that then move on to the next one.

    so far I have:
    Code:
    #!/bin/bash
    
    SAVE_DIR ~/Desktop
    cd $SAVE_DIR
    ls *.torrent >> ~/.torrent_list
    some parsing command here that outputs 1st .torrent as FILE
    
    echo "select an option"
    echo "[1] music"
    echo "[2] movies"
    echo "[3] tv"
    echo "[4] software"
    echo "[5] misc"
    
    read CHOICE
    
    case "$CHOICE" in
    
    	"1" )
    	mv $FILE ~/.rtorrent/music/
    	;;
    	
    	"2" )
    	mv $FILE ~/.rtorrent/movies/
    	;;
    
    	"3" )
    	mv $FILE ~/.rtorrent/tv/
    	;;
    
    	"4" )
    	mv $FILE ~/.rtorrent/software/
    	;;
    	
    	"5" )
    	mv $FILE ~/.rtorrent/misc/
    	;;
    
    esac
    
    rm ~/.torrent_list
    
    exit
    basically I need something that can parse the created file and stop after finding the first .torrent file. from there I think the rest should work. I tested what I could and it moves the files fine.

    Any suggestions would be appreciated. This is definitely not anything important, just a time saver (well, once I get it working anyway )

  2. #2
    Join Date
    Sep 2006
    Beans
    2,914

    Re: bash script to move files

    Quote Originally Posted by murlosad View Post
    Any suggestions would be appreciated. This is definitely not anything important, just a time saver (well, once I get it working anyway )
    instead of asking for input, why not do everything in a config file
    eg of config file

    pattern="*.torrent"
    destination="/somewhere"


    then in the script, parse the config file and move the files.

  3. #3
    Join Date
    Apr 2006
    Beans
    71
    Distro
    Ubuntu 7.04 Feisty Fawn

    Re: bash script to move files

    I thought about that, but the problem is that I want to move the torrents around depending on what they are, e.g. a movie goes into the movies directory, a linux distro into software, etc. and since torrents are not really named in any particular standard I don't think it a config file would be able to handle it.

    Thanks for replying though.

    I think I can do it with sed, but I've never used sed before so I'm still reading up on it.

  4. #4
    Join Date
    Oct 2007
    Location
    Fort Collins, CO, USA
    Beans
    481
    Distro
    Ubuntu 9.04 Jaunty Jackalope

    Re: bash script to move files

    You don't need to run ls into a file and parse its output.
    You can just have bash match the *.torrent pattern and work through the file names.
    Code:
    #!/bin/bash
    
    SAVE_DIR ~/Desktop
    cd $SAVE_DIR
    for FILE in *.torrent
    do
    
    echo moving $FILE
    echo "select an option"
    echo "[1] music"
    echo "[2] movies"
    echo "[3] tv"
    echo "[4] software"
    echo "[5] misc"
    
    read CHOICE
    
    case "$CHOICE" in
    
            "1" )
            mv $FILE ~/.rtorrent/music/
            ;;
    
            "2" )
            mv $FILE ~/.rtorrent/movies/
            ;;
    
            "3" )
            mv $FILE ~/.rtorrent/tv/
            ;;
    
            "4" )
            mv $FILE ~/.rtorrent/software/
            ;;
    
            "5" )
            mv $FILE ~/.rtorrent/misc/
            ;;
    
    esac
    done

  5. #5
    Join Date
    Apr 2006
    Beans
    71
    Distro
    Ubuntu 7.04 Feisty Fawn

    Re: bash script to move files

    Brilliant! I knew it had to be simpler than I was making it. Thank you very much sir, that does exactly what I want. I am a happy man tonight.

    now it's time for sleep.

  6. #6
    Join Date
    Sep 2006
    Beans
    2,914

    Re: [SOLVED] bash script to move files

    @OP, i hope you do not have that many torrent files, if not, you will be entering options like mad.

    Code:
    awk 'BEGIN{q="\047"}
    {dir=""}
    /avi|mpg|mpeg/{
      dir="~/.rtorrent/movies/"  
    }
    /mp3|wma/{
      dir="~/.rtorrent/music/"
    }
    {
     if (dir) {
      cmd = "mv "q FILENAME q" "dir
      print cmd
      system(cmd)
      nextfile
     }
    }' /tmp/*.torrent
    Last edited by ghostdog74; April 14th, 2008 at 08:54 AM.

  7. #7
    Join Date
    Apr 2006
    Beans
    71
    Distro
    Ubuntu 7.04 Feisty Fawn

    Re: [SOLVED] bash script to move files

    Quote Originally Posted by ghostdog74 View Post
    @OP, i hope you do not have that many torrent files, if not, you will be entering options like mad.
    lol, no not too many, I just tend to find two or three at a time, and usually I'm remoting into my desktop when I dl them, so it's easier to have a script do most of the work for me.

    Thanks everybody for the help.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •