Page 1 of 2 12 LastLast
Results 1 to 10 of 15

Thread: Simple shell script to automatically move folder

  1. #1
    Join Date
    Oct 2007
    Location
    Vanløse, Denmark
    Beans
    39

    Question Simple shell script to automatically move folder

    Hey everybody!

    I'm in desperate (not really) need of a simple shell script that move folders, to a specific location, according to the name of the folder.

    For example I want the folder "SeriesName - 3x11 - Name of episode" to be moved to "/SeriesName/Season 3/" automatically.

    Does anyone know a script that can do this?


    Thanks.

    /knutz

  2. #2
    Join Date
    Jun 2007
    Location
    Maryland, US
    Beans
    6,270
    Distro
    Kubuntu

    Re: Simple shell script to automatically move folder

    Here's a simple script:
    Code:
    #!/bin/bash
    
    series_name=`echo $1 | awk -F" " '{ print $1 }'`
    season_num=`echo $1 | awk -F" " '{ print $3 }' | awk -F"x" '{ print $1 }'`
    
    mkdir -p /$series_name/Season\ $season_num
    mv "$1" /$series_name/Season\ $season_num
    It takes as an command-line argument the name of the directory that you want to move.

    P.S. Per your request, the new directory is created under the '/' directory. To do this, one requires root (sudo) privileges. Is this really where you want to place these new directories??

    P.S #2 I also assumed that the season number is yielded from the first number of "3x11". If I am wrong, then where is the season number coming from?

  3. #3
    Join Date
    Oct 2007
    Location
    Vanløse, Denmark
    Beans
    39

    Re: Simple shell script to automatically move folder

    Quote Originally Posted by dwhitney67 View Post
    Here's a simple script:
    Code:
    #!/bin/bash
    
    series_name=`echo $1 | awk -F" " '{ print $1 }'`
    season_num=`echo $1 | awk -F" " '{ print $3 }' | awk -F"x" '{ print $1 }'`
    
    mkdir -p /$series_name/Season\ $season_num
    mv "$1" /$series_name/Season\ $season_num
    It takes as an command-line argument the name of the directory that you want to move.

    P.S. Per your request, the new directory is created under the '/' directory. To do this, one requires root (sudo) privileges. Is this really where you want to place these new directories??

    P.S #2 I also assumed that the season number is yielded from the first number of "3x11". If I am wrong, then where is the season number coming from?

    Thank you very much for this.
    P.S. #1 > That was a mistake. I have a folder for it.
    P.S #2 > Yes you are correct.

    What if the SeriesName contains a " " (space). Would this script work then? (Don't know if I am understanding it correctly)
    And what if the SeriesName contains the letter x?
    Last edited by knutz; December 9th, 2009 at 11:37 AM.

  4. #4
    Join Date
    Jun 2007
    Location
    Maryland, US
    Beans
    6,270
    Distro
    Kubuntu

    Re: Simple shell script to automatically move folder

    Quote Originally Posted by knutz View Post
    What if the SeriesName contains a " " (space). Would this script work then? (Don't know if I am understanding it correctly)
    No; someone else with better bash scripting experience would have to help you with this issue.

    And what if the SeriesName contains the letter x?
    Yes, as long as the Series Name does not contain a space. For example, "x-files - 3x11" would work; but directories with names like "x files - 3x11" or "fu manchu - 2x5", which contain a space in the series name, would not work.

  5. #5
    Join Date
    Aug 2008
    Location
    Israel
    Beans
    151
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Simple shell script to automatically move folder

    Just enclose arguments with double quotes.
    Last edited by Rany Albeg; December 9th, 2009 at 12:18 PM.

  6. #6
    Join Date
    Jun 2007
    Location
    Maryland, US
    Beans
    6,270
    Distro
    Kubuntu

    Re: Simple shell script to automatically move folder

    Quote Originally Posted by Rany Albeg View Post
    Just enclose arguments with double quotes.
    It's not that simple.

    Below is a hokey solution that probably could stand to be refined:
    Code:
    #!/bin/bash
    
    series_name=""
    
    tmp=$1
    while [ true ]
    do
            tmp2=`echo $tmp | awk -F" " '{ print $1 }'`
    
            if [ "$tmp2" == "-" ]
            then
                    break
            fi
    
            tmp=`echo $tmp | cut -d" " -f2-`
    
            if [ "$series_name" == "" ]
            then
                    series_name="$tmp2"
            else
                    series_name="$series_name $tmp2"
            fi
    done
    
    season_num=`echo $1 | awk -F"-" '{ print $2 }' | awk -F"x" '{ print $1 }' | awk -F" " '{ print $1 }'`
    
    #echo "$series_name"
    #echo "$season_num"
    
    mkdir -p "$series_name"/Season\ $season_num
    mv "$1" "$series_name"/Season\ $season_num

  7. #7
    Join Date
    Oct 2007
    Location
    Vanløse, Denmark
    Beans
    39

    Re: Simple shell script to automatically move folder

    Quote Originally Posted by dwhitney67 View Post
    It's not that simple.

    Below is a hokey solution that probably could stand to be refined:
    Code:
    #!/bin/bash
    
    series_name=""
    
    tmp=$1
    while [ true ]
    do
            tmp2=`echo $tmp | awk -F" " '{ print $1 }'`
    
            if [ "$tmp2" == "-" ]
            then
                    break
            fi
    
            tmp=`echo $tmp | cut -d" " -f2-`
    
            if [ "$series_name" == "" ]
            then
                    series_name="$tmp2"
            else
                    series_name="$series_name $tmp2"
            fi
    done
    
    season_num=`echo $1 | awk -F"-" '{ print $2 }' | awk -F"x" '{ print $1 }' | awk -F" " '{ print $1 }'`
    
    #echo "$series_name"
    #echo "$season_num"
    
    mkdir -p "$series_name"/Season\ $season_num
    mv "$1" "$series_name"/Season\ $season_num
    This works!

    Thank you very much!!
    I sure hope this can be helpful to others as well.


    THANKS!

    /knutz

  8. #8
    Join Date
    Oct 2007
    Location
    Vanløse, Denmark
    Beans
    39

    Re: Simple shell script to automatically move folder

    How do i run the script as root?
    When i try to run it, it goes bananas..
    Last edited by knutz; December 9th, 2009 at 06:04 PM.

  9. #9

    Re: Simple shell script to automatically move folder

    Quote Originally Posted by dwhitney67 View Post
    Here's a simple script:
    Code:
    #!/bin/bash
    
    series_name=`echo $1 | awk -F" " '{ print $1 }'`
    season_num=`echo $1 | awk -F" " '{ print $3 }' | awk -F"x" '{ print $1 }'`
    
    mkdir -p /$series_name/Season\ $season_num
    mv "$1" /$series_name/Season\ $season_num
    It takes as an command-line argument the name of the directory that you want to move.

    P.S. Per your request, the new directory is created under the '/' directory. To do this, one requires root (sudo) privileges. Is this really where you want to place these new directories??

    P.S #2 I also assumed that the season number is yielded from the first number of "3x11". If I am wrong, then where is the season number coming from?
    if the filename has a space, just use "-" as the delimiter

    Code:
    series_name=`echo $1 | awk -F"-" '{ print $1 }'`

  10. #10
    Join Date
    Jun 2007
    Location
    Maryland, US
    Beans
    6,270
    Distro
    Kubuntu

    Re: Simple shell script to automatically move folder

    Quote Originally Posted by mo.reina View Post
    if the filename has a space, just use "-" as the delimiter

    Code:
    series_name=`echo $1 | awk -F"-" '{ print $1 }'`
    How does one then remove the space at the end of the resulting 'series_name'?

    If the file has the format of "foo foo - 3x11", and I use the statement you have shown above, the resulting 'series_name' is "foo foo " (note the trailing white-space).

    P.S. I ask the simple question above because, believe it or not, earlier today I thought of the suggestion you offered, and naturally I encountered a problem with it.

Page 1 of 2 12 LastLast

Tags for this Thread

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
  •