skip to Main Content

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?

2

Answers


  1. 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
    
    Login or Signup to reply.
  2. 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
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search