SH脚本从一个目录移动到另一个文件根据文件名 [英] SH script to move files from one dir to another depending on the filename
问题描述
我试图写一个sh脚本时,我下载的一个完成将运行。结果
这应该寻找在〜/下载特定文件名,并根据不同的文件名移动到不同的目录。结果
即。我已经下载欢乐合唱团的最后一集,文件名是:
glee_some_trash_files_always_have.mkv
它应该被移到
〜/ TVshows /吉利/
这是我能够做到:
#!/斌/庆典如果在〜/ Downoads文件名;然后
结果=
如果filename = * *高兴;然后
结果= MV $文件名〜/ TVshows /吉利/
其他
如果filename = *口袋妖怪*;然后
结果= MV $文件名〜/ TVshows /口袋妖怪/
万一
DONE
是我的做法正确吗?请注意,我是很新的SH。
先谢谢了。
###################################### #########################################
编辑:这是我的剧本,我希望有人还能找到有用的:
#!/斌/庆典CD$ HOME /下载
#为文件名中的*;做
找 。型F |而IFS =读取文件名;做#查找所有〜/下载子迪尔斯文件
案$ {文件名,, *}在此#语法发出小写的值:$ {VAR ,, *}(bash的第4版)
*。第三部分):;;被移动#*排除文件。第二部分
move.sh):;;
#*测试*)MV$文件名,$ HOME / TVshows /吉利/;; #使用移动就没有必要{&功放;&安培; RM$文件名}
*测试*)SCP$文件名,imac@imac.local:/用户/ iMac电脑/台式机/&放大器;&安培; RM$文件名;;
*美国* *的爸爸)SCP$文件名,imac@imac.local:/用户/ iMac电脑/电影/系列/美国\\爸爸/&放大器;&安培; RM$文件名;;
*)回声不知道往哪里放$文件名;;
ESAC
DONE
这就是shell的情况
语句就派上用场了:
#!/斌/庆典
CD$ HOME /下载
对于文件名在*;做
#这个语法发出小写的值:$ {VAR ,, *}(bash的第4版)
案$ {文件名,, *}中
欢乐*)MV$文件名,$ HOME / TVshows /吉利/;;
口袋妖怪*)MV$文件名,$ HOME / TVshows /宠物小精灵/;;
*)回声不知道往哪里放$文件名;;
ESAC
DONE
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 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
这篇关于SH脚本从一个目录移动到另一个文件根据文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!