我有大量的工作文件,格式为a.mp4 b.txt c.avi d.doc等(扩展名与问题无关)。我也有一组文件,这些文件包含相同的命名文件,但带有特定的通用前缀“ broken_”,例如,broken_a.mp4,broken_b.txt
如果我有a.mp4和broken_a.mp4,我想将broken_a.mp4移动到保存目录。如果我有broken_d.mp4但没有匹配的d.mp4,请不要理会。
我有一些代码已成功用于识别和移动具有我要修改的扩展名的文件
这是相同的扩展名文件的工作示例代码的形式(对原始作者表示敬意),如果可能的话,我想对其进行修改
#!/bin/bash
# Name of source directory
SOURCE_DIR=.
# Name of destination directory
DEST_DIR=already_converted_m4v
# Create the destination directory for the moved files, if it doesn't already exist.
[ ! -d $DEST_DIR ] && mkdir -p $DEST_DIR
find $SOURCE_DIR -maxdepth 1 -type f -iname "*.avi" | while read fin
do
#echo "m4v doing avi"
fm4v=${fin/.avi/.m4v}
[ -f "$fm4v" ] && gmv -v --backup=numbered "$fin" $DEST_DIR/
done
我的垃圾第一次尝试显然无效,看起来很像:
#!/bin/bash
# Name of source directory
SOURCE_DIR=.
# Name of destination directory
DEST_DIR=Already_broken
# Create the destination directory for the moved files, if it doesn't already exist.
[ ! -d $DEST_DIR ] && mkdir -p $DEST_DIR
find $SOURCE_DIR -maxdepth 1 -type f -iname "*" | while read fin
do
#echo "working to find existing broken and unbroken files"
filetest_basename=$(basename "$fin" )
filetest_extension=$(extension "$fin" )
echo $filetest_basename
echo $filetest_extension
fileok=${filetest_basename/!broken_/broken_}
[ -f "$fileok" ] && gmv -v --backup=numbered "$fin" $DEST_DIR/
done
感谢您的帮助
find
在这里无关紧要,简单的shell循环就足够了:
SRCDIR='.'
DSTDIR='Already_broken'
[ -d "$DSTDIR" ] || mkdir -p -- "$DSTDIR"
for broken in "$SRCDIR"/broken_*; do
# can't use ${broken/broken_}, SRCDIR may contain broken_
if [ -f "${broken%"${broken##*/}"}${broken##*/broken_}" ]; then
echo gmv -v --backup=numbered "$broken" "$DSTDIR"
fi
done
如果输出看起来不错,请删除echo
。