[SOLVED] help clean up file movement script

Hello Group,

Once again another script hacked together from a few sources to try and suit my needs. This is to go through a /temp directory and for each ls entry ask which Dir of three I want it sorted.

The script works but there are a few behaviors that are odd so I figured I'd ask for help cleaning this up. The first issue is it continues to ask to move the file even if you have answered yes prior, which should cause an error but instead it just happily copies it to both, or all three which is really weird. I'd like it to stop after one affirmative answer or better yet if its easier & someone can show me how it could be one read with a (M)ovie (T)elevision or (K)ids prompt & and three $replys looking for first corresponding letters . I just wasn't able to figure that out for the life of me.

Here it is: TIA to all, this is my goto group for this stuff, since I'm obviously not a coder.

#/bash
for files in `cd /mnt/storage/media/temp/; ls`; do

echo "Found: /mnt/storage/media/temp/"$files;

read -p "Move to movies (y/n)?"
[ "$REPLY" == "n" ] || mv /mnt/storage/media/temp/$files /mnt/storage/media/Video/movies/$files

read -p "Move to Mike (y/n)?"
[ "$REPLY" == "n" ] || mv /mnt/storage/media/temp/$files /mnt/storage/media/Video/mike/$files 

read -p "Move to TV (y/n)?"
[ "$REPLY" == "n" ] || mv /mnt/storage/media/temp/$files /mnt/storage/media/Video/television/$files

done

#/bash is wrong. It needs to be #!/bin/bash.

That's a useless use of ls and dangerous use of backticks.

You can use the built-in select menu to make things simpler.

#!/bin/bash


for FILE in /mnt/storage/media/temp/*
do
        [ -f "$FILE" ] || continue # Ignore non-files

        # Special variable which controls select menu prompt
        PS3="Move $FILE to /mnt/storage/media/Video/?"

        # 'select' is a loop like 'while', but prints and does a menu inside
        select DIR in movies mike television quit skip
        do
                [ -z "$DIR" ] && continue # User picked a bad option
                [ "$DIR" = "quit" ] && exit # User wants to quit
                [ "$DIR" = "skip" ] && break # User wants to skip this file

                echo mv "$FILE" "/mnt/storage/media/Video/$DIR/"
                break
        done
done

Thanks that worked perfect.

1 Like