开发者

SH script to move files from one dir to another depending on the filename

I am trying to write a sh script that will run when one of my downloads is completed.

It should look for a specific filename on ~/Downloads and move it to a different dir depending on the filename.

I.e. I have downloaded the last episode of Glee, the filename is:

glee_some_trash_files_always_have.mkv

It should be moved to

~/TVshows/Glee/

This is what I was able to do:

#!/bin/bash

if filename in ~/Downoads; then
result=
if filename = *glee*; then
result= mv $filename ~/TVshows/Glee/
else
if filename = *pokemon*; then
result= mv $filename ~/TVshows/pokemon/
endif
done

Is my approach correct? Please note I am very new to sh.

Thanks in advance.

###############################################################################

Edit: Here is my script, I hope someone else could find it useful:

#!/bin/bash

cd "$HOME/Downloads"
# for filename in *; do
find . -type f | while IFS= read filename; do # Look for files in all ~/Download sub-dirs
  case "${filename,,*}" in  # this syntax emits the value in 开发者_StackOverflow社区lowercase: ${var,,*}  (bash version 4)
     *.part) : ;; # Excludes *.part files from being moved
     move.sh) : ;;
#    *test*)            mv "$filename" "$HOME/TVshows/Glee/" ;; # Using move there is no need to {&& rm "$filename"}
     *test*)           scp "$filename" "imac@imac.local:/users/imac/Desktop/" && rm "$filename" ;;
     *american*dad*)   scp "$filename" "imac@imac.local:/users/imac/Movies/Series/American\ Dad/" && rm "$filename" ;;
     *) echo "Don't know where to put $filename" ;;
  esac
done


This is where the shell's case statement comes in handy:

#!/bin/bash
cd "$HOME/Downloads"
for filename in *; do
  # this syntax emits the value in lowercase: ${var,,*}  (bash version 4)
  case "${filename,,*}" in
    glee*)    mv "$filename" "$HOME/TVshows/Glee/" ;;
    pokemon*) mv "$filename" "$HOME/TVshows/pokemon/" ;;
    *) echo "don't know where to put $filename";;
  esac
done


The mv command can move multiple files at a time. The last argument is treated as a directory name. The trailing / is important; if there's one matching file name, and the target directory doesn't exist (say, because you misspelled it), it will create it as a file.

mv ~/Downloads/*glee* ~/TVshows/Glee/
mv ~/Downloads/*pokemon* ~/TVshows/pokemon/


This is my script for serial sorting.

#!/bin/bash

PATH_FROM=/your/download/dir
PATH_TO=/path/serial/directory

cd $PATH_FROM
ls -1 *{mkv,avi,srt,mp4} | sed -e 's/\.[s|S][0-9].*$//g' | uniq | while read -r serial
do
    folder=$(echo $serial | tr A-Z a-z) folder=${folder/the./}
    folder=`echo ${folder//_/.}`
    folder=`echo ${folder//./ }`

    folder=( $folder )
    folder=`echo "${folder[@]^}"`

    ls -1 ${serial// /.}.* | sed -e 's/'$serial'\.[s|S]//g' | sed -e 's/\..*$//g' | uniq | while read -r s
    do
        season=s$(echo "$s" | sed -e 's/[e|E].*$//g' | sed -e 's/^0//g')
        mkdir -p "$PATH_TO/$folder/$season"
        mv -f $serial.?$s* "$PATH_TO/$folder/$season/"
        log=`date +"[%d/%m/%Y %X]"`
        echo $log" "$serial" success sync with "$PATH_TO"/"$folder"/"$season >> /path/to/logfiledir/log.txt
    done
done
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜