mouseskowitz
mouseskowitz

Reputation: 45

How to write mv command that will work as cron job

I'm running Centos 7. I need to have a cron job that moves everything from /media/tmp to /media/tv except the .grab and Transcode folders. Yesterday I thought that the following worked, but today it moves the Transcode folder as well.

mv -n /media/tmp/*!(Transcode)!(.grab) /media/tv/

I've found that the above does not work as a cron job as the '(' causes and error. I learned that I needed to escape those, but now I get

mv: cannot stat ‘/media/tmp/!(Transcode)!(.grab)’: No such file or directory 

My current attempt at a bash script is

#!/bin/bash

mv -n '/media/tmp'/*!\(Transcode\)!\(.grab\) '/media/tv/'

My understanding is that the * is the problem, but using either ' or " on the file path doesn't seem to fix it like that post I found said it would.

Any ideas on how to get this to work correctly?

Upvotes: 1

Views: 683

Answers (2)

Ed Morton
Ed Morton

Reputation: 203985

I'd just do it as something simple like (untested):

mkdir -p /media/tv || exit 1
for i in /media/tmp/*; do
    case $(basename "$i") in
        Transcode|.grab ) ;;
        * ) mv -n -- "$i" /media/tv ;;
    esac
done

Upvotes: 1

vintnes
vintnes

Reputation: 2030

You're trying to use extglob, which may not be enabled for cron. I would avoid that option entirely, iterating over the glob with a negative ! regex match.

for file in /media/tmp/*; do
  [[ ! "$file" =~ Transcode|\.grab$ ]] && mv -n "$file" /media/tv/
done

Upvotes: 1

Related Questions