You are not logged in.

#1 2010-09-22 18:59:12

Pank
Member
From: IT
Registered: 2009-06-13
Posts: 371

[SOLVED] Move using wild cards in bash

Hi
I have tried to write a script for transmission to move completed ... podcasts to their designated folder.

/And I am no programmer!/

It works by finding a hopefully unique part of the filename and match it with the list of folders in my TV Shows folder. Unfortunately there are lots of white spaces and capital letters, as the XBMC Scrapper needs to find metadata based on the file and folder names. This makes the process a lot harder.

Thus, my question is, how do I combine white space and wild card expansion in a bash script?

Basically, the line that I am struggling with is:

    mv "$LOCATION/$FILENAME" "$DESTDIR/"*"$NICENAME"* >> $TORRENTDIR/$LOGNAME

The destination folder partly contains the $NICENAME, but not quite. Thus the * are needed.

Note: $NICENAME and $DESTDIR contains spaces and there is not really anything to do about it do about it due to the XBMC compatibility.

I would really appreciate a hint.

Thanks,
Rasmus

The full script [modified to work]:

#!/bin/bash                                                                    

# *************                                                                
REMOTE=/usr/bin/transmission-remote
DESTDIR="/mnt/data/video/TV Shows"
TORRENTDIR=/mnt/data/download/complete
LOGNAME=log
# *************                                                                

# Add new log entry                                                            
echo `date`>> $TORRENTDIR/$LOGNAME
echo "" >> $TORRENTDIR/$LOGNAME

# Move Finished                                                                
FINISHED="$($REMOTE -l   | tail --lines=+2 | grep 100% |  awk '{ print $1;     
}')"
for i in $FINISHED; do
echo Torrent No. $i is finished.
LOCATION=`$REMOTE   -t $i -i | awk '/Location/'|sed -e 's/  Location: //'`
FILENAME=`$REMOTE -t $i -i | grep Name | cut -c9- `
NICENAME=`echo $FILENAME | tr "[A-Z]" "[a-z]" | sed -e 's/^the[\.,\ ]//g' -e   
's/[._]/\ /g' | cut -d' ' -f1-2`
#NICENAMEFILE=`echo $FILENAME | tr "[A-Z]" "[a-z]" | sed -e 's/^the[\.,\       
]//g' -e 's/[._]/\\ /g' | cut -d' ' -f1-2`                                     

#disable casematch                                                             
shopt -s nocasematch
# test if any TV Show folder contains the NICENAME                             
if [ "`ls "$DESTDIR" | grep -i "$NICENAME"`" != ""  ]; then
    echo "Moving file $i"
    destination="$(find "$DESTDIR" -type d -iname '*'"$NICENAME"'*' -print -quit)"
    mv "$LOCATION/$FILENAME" "$destination" >> $TORRENTDIR/$LOGNAME
    $REMOTE -t $i -r                                                          
    else
    echo "No match. You will have to look into it yourself"
    echo "No Match: $FILENAME" >> $TORRENTDIR/$LOGNAME
    fi
done
shopt -u nocasematch

echo "---------------------------">> $TORRENTDIR/$LOGNAME

echo Done

exit

Last edited by Pank (2010-09-22 19:43:41)


Arch x64 on Thinkpad X200s/W530

Offline

#2 2010-09-22 19:09:24

brisbin33
Member
From: boston, ma
Registered: 2008-07-24
Posts: 1,799
Website

Re: [SOLVED] Move using wild cards in bash

I know it doesn't answer your /specific/ question but...

# find the first destination folder that matches '*nicename*'
destination="$(find "$DESTDIR" -type d -name '*'"$NICENAME"'*' -print -quit)"

# move it there
mv "$LOCATION/$FILENAME" "$destination"

sidenote: i use xbmc and he still parses ok with underscores in place of spaces.

Offline

#3 2010-09-22 19:23:00

Pank
Member
From: IT
Registered: 2009-06-13
Posts: 371

Re: [SOLVED] Move using wild cards in bash

I will definitely try out your suggestion brisbin. As long as it works I am happy smile

On xbmc: I use the version for the old Xbox. I remember when I first set it up it was really picky. It might have changed though. . . Anyhow, all folders are with white spaces now, so not having to change that is somewhat appealing in itself.


Arch x64 on Thinkpad X200s/W530

Offline

#4 2010-09-22 19:30:38

livibetter
Member
From: Taipei
Registered: 2008-05-14
Posts: 95
Website

Re: [SOLVED] Move using wild cards in bash

Pank wrote:

Hi

Basically, the line that I am struggling with is:

    mv "$LOCATION/$FILENAME" "$DESTDIR/"*"$NICENAME"* >> $TORRENTDIR/$LOGNAME

The destination folder partly contains the $NICENAME, but not quite. Thus the * are needed.

I tried this

DESTDIR="dest dir"
NICENAME="nice name"
cd /tmp
mkdir -p "$DESTDIR"/"something $NICENAME blah"
touch "$DESTDIR"/"something $NICENAME blah"/thefile
ls -1 "$DESTDIR"
ls -d "$DESTDIR"/*"$NICENAME"*
ls -1 "$DESTDIR"/*"$NICENAME"*
touch newfile
mv newfile "$DESTDIR"/*"$NICENAME"*
ls -1 "$DESTDIR"/*"$NICENAME"*

And I got:

something nice name blah
dest dir/something nice name blah
thefile
#### after touch newfile
newfile
thefile

It seems to be right result. I don't put '/' with `$DESTDIR`.

Last edited by livibetter (2010-09-22 19:34:00)

Offline

#5 2010-09-22 19:42:02

Pank
Member
From: IT
Registered: 2009-06-13
Posts: 371

Re: [SOLVED] Move using wild cards in bash

brisbin33,
using iname I got it working. Thanks for pointing out the obvious (Find; Dohh!).

livibetter.
it actually workes the other way around. It transforms something without spaces to something with spaces to be able to match it with a folder with spaces and move it there.

I'd guess the script is quite messy but now it works.


Arch x64 on Thinkpad X200s/W530

Offline

Board footer

Powered by FluxBB