BASH脚本移动文件不能正确移动文件

时间:2019-05-20 16:50:31

标签: bash

我编写了一个脚本,用于根据文件名的第一个字母将文件从_Incoming目录移动到../[A-Z]目录。

由于该目录中的所有其余文件都已移到默认的“ ../@”目录中,因此似乎无法正常工作。

这是我正在使用的脚本:

#!/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/@

这是我要移动几个文件的输出:

+-------------------------------------------------------------------+
|               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'

1 个答案:

答案 0 :(得分:2)

反斜杠转义*时,它不再是通配符。所以

for i in /run/media/chad/New\ Volume/Chad/_Incoming/\*; do

并没有达到您的期望,并且从调试输出中可以明显看出(例如,它将$i设置为不存在的目录/run/media/chad/New\ Volume/Chad/_Incoming/*

修复该问题后,您会发现

mv -v /run/media/chad/New\ Volume/Chad/_Incoming/* /run/media/chad/New\ Volume/Chad/@

并没有真正做到您想要的,因为/run/media/chad/New\ Volume/Chad/_Incoming/中所有新创建的目录都将与该glob匹配并移至@中。