bash SH 脚本根据文件名将文件从一个目录移动到另一个目录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7573815/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
SH script to move files from one dir to another depending on the filename
提问by fedxc
I am trying to write a sh script that will run when one of my downloads is completed.
我正在尝试编写一个 sh 脚本,该脚本将在我的一次下载完成后运行。
It should look for a specific filename on ~/Downloads and move it to a different dir depending on the filename.
它应该在 ~/Downloads 上查找特定的文件名,并根据文件名将其移动到不同的目录。
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.
我的方法正确吗?请注意,我对 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" "[email protected]:/users/imac/Desktop/" && rm "$filename" ;;
*american*dad*) scp "$filename" "[email protected]:/users/imac/Movies/Series/American\ Dad/" && rm "$filename" ;;
*) echo "Don't know where to put $filename" ;;
esac
done
回答by glenn Hymanman
This is where the shell's case
statement comes in handy:
这是 shellcase
语句派上用场的地方:
#!/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
回答by Ivan Karapuzoff
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
回答by Keith Thompson
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
命令可以一次移动多个文件。最后一个参数被视为目录名称。尾随/
很重要;如果有一个匹配的文件名,而目标目录不存在(比如,因为你拼错了它),它会将它创建为一个文件。
mv ~/Downloads/*glee* ~/TVshows/Glee/
mv ~/Downloads/*pokemon* ~/TVshows/pokemon/