简体   繁体   English

在查找和移动Bash脚本中包含相同名称的不同文件

[英]Including different files with the same name in a Find and Move Bash Script

Im using a bash script to search for and archive files in different sub directories. 我正在使用bash脚本在不同的子目录中搜索和存档文件。 There are some files that have the same name; 有些文件具有相同的名称。

(eg ABC_000.gif) (例如ABC_000.gif)

however they are in fact different images. 但是它们实际上是不同的图像。 Is there a simple way to move and rename these files by adding a string to the end of the filename such as ABC_000.gif for the original file and ABC_000.gif.gif for the duplicated file 是否有一种简单的方法来移动和重命名这些文件,方法是在文件名的末尾添加一个字符串,例如原始文件为ABC_000.gif,复制文件为ABC_000.gif.gif

$SOURCE is the source file, $DEST is the destination file; $SOURCE是源文件, $DEST是目标文件; this appends the suffix while $DEST is already there, then moves it. $DEST已经存在时,它会附加后缀,然后将其移动。

while [ -e $DEST ]
do
    DEST+=.`<<<$DEST sed 's/.*\.//'`
done
mv $SOURCE $DEST

Try this: 尝试这个:

#!/bin/bash
i = 0
FILES=`find -name ABC_000.gif | xargs -r`
for FILE in $FILES; do
    mv $FILE ./$FILE_$i
    let "i += 1"
done

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM