[英]BASH Script to move files is not moving files correctly
I've written a script to move files from the _Incoming directory to ../[AZ] directories based on the first letter of the file name. 我已经编写了一个脚本,用于根据文件名的第一个字母将文件从_Incoming目录移动到../[AZ]目录。
It doesn't appear to be working correctly as all of the remaining files in this directory are being moved into the default '../@' directory. 由于该目录中的所有剩余文件都被移到默认的“ ../@”目录中,因此似乎无法正常工作。
Here is the script I'm using: 这是我正在使用的脚本:
#!/bin/bash
echo "+-------------------------------------------------------------------+"
echo "| Removing _ and replacing with space |"
echo "+-------------------------------------------------------------------+"
find /run/media/chad/New\ Volume/Chad/_Incoming/ -depth -name '*_*' |
while IFS= read -r f ; do
mv -iv "$f" "$(dirname "$f")/$(basename "$f"|tr '_' ' ')" ;
done
### Would be nice if this didn't rename the Incoming directory.
### But, since it does...
echo "+-------------------------------------------------------------------+"
echo "| Trying to rename ' Incoming' back to '_Incoming' -- Failure=OKAY |"
echo "+-------------------------------------------------------------------+"
mv -v /run/media/chad/New\ Volume/Chad/\ Incoming \
/run/media/chad/New\ Volume/Chad/_Incoming
echo "+-------------------------------------------------------------------+"
echo "| Sorting and Moving Files based on Alpha |"
echo "+-------------------------------------------------------------------+"
for i in /run/media/chad/New\ Volume/Chad/_Incoming/\*; do
echo "i:$i"
FILE=$(basename "$i")
echo "FILE:$FILE"
LTR=$(echo "${FILE:0:1}" | tr [:lower:] [:upper:] )
echo "LTR:$LTR"
NEWFILE="/run/media/chad/New Volume/Chad/$LTR/$FILE"
echo "NEWFILE:$NEWFILE"
mv -v "$i" "${NEWFILE//_/ }"
done
echo "+-------------------------------------------------------------------+"
echo "| Moving the left-overs to the '@' directory |"
echo "+-------------------------------------------------------------------+"
mv -v /run/media/chad/New\ Volume/Chad/_Incoming/* /run/media/chad/New\ Volume/Chad/@
Here is the output when I have a couple of files to move: 这是我有几个文件要移动时的输出:
+-------------------------------------------------------------------+
| Removing _ and replacing with space |
+-------------------------------------------------------------------+
renamed '/run/media/chad/New Volume/Chad/_Incoming/' -> '/run/media/chad/New Volume/Chad/ Incoming'
+-------------------------------------------------------------------+
| Trying to rename ' Incoming' back to '_Incoming' -- Failure=OKAY |
+-------------------------------------------------------------------+
renamed '/run/media/chad/New Volume/Chad/ Incoming' -> '/run/media/chad/New Volume/Chad/_Incoming'
+-------------------------------------------------------------------+
| Sorting and Moving Files based on Alpha |
+-------------------------------------------------------------------+
i:/run/media/chad/New Volume/Chad/_Incoming/*
FILE:*
LTR:*
NEWFILE:/run/media/chad/New Volume/Chad/*/*
+-------------------------------------------------------------------+
| Moving the left-overs to the '@' directory |
+-------------------------------------------------------------------+
renamed '/run/media/chad/New Volume/Chad/_Incoming/Deep-fried Sea Bream - Mukbang eating show.mp4' -> '/run/media/chad/New Volume/Chad/@/Deep-fried Sea Bream - Mukbang eating show.mp4'
renamed '/run/media/chad/New Volume/Chad/_Incoming/HOW TO BUILD A PLANT SUPPORT.mp4' -> '/run/media/chad/New Volume/Chad/@/HOW TO BUILD A PLANT SUPPORT.mp4'
When you backslash-escape a *
, it is no longer a wildcard. 当您对
*
反斜杠转义时,它不再是通配符。 So 所以
for i in /run/media/chad/New\ Volume/Chad/_Incoming/\*; do
does not do what you expect, and what it does is evident from the debugging output (ie it sets $i
to the non-existing directory /run/media/chad/New\\ Volume/Chad/_Incoming/*
. 不会达到您的期望,并且从调试输出中可以明显看出(即,它将
$i
设置$i
不存在的目录/run/media/chad/New\\ Volume/Chad/_Incoming/*
。
When you fix that, you'll find that 解决该问题后,您会发现
mv -v /run/media/chad/New\ Volume/Chad/_Incoming/* /run/media/chad/New\ Volume/Chad/@
doesn't really do what you want, because all of the newly created directories in /run/media/chad/New\\ Volume/Chad/_Incoming/
will match the glob and get moved into @
. 并没有真正做到您想要的,因为
/run/media/chad/New\\ Volume/Chad/_Incoming/
所有新创建的目录都将与glob匹配并移至@
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.