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

时间:2021-01-13 19:25:22

Im using a bash script to search for and archive files in different sub directories. There are some files that have the same name;

我使用bash脚本搜索和存档不同子目录中的文件。有些文件名称相同;

(e.g. 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

2 个解决方案

#1


1  

$SOURCE is the source file, $DEST is the destination file; this appends the suffix while $DEST is already there, then moves it.

$ SOURCE是源文件,$ DEST是目标文件;这会附加后缀,而$ DEST已经存在,然后移动它。

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

#2


0  

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

#1


1  

$SOURCE is the source file, $DEST is the destination file; this appends the suffix while $DEST is already there, then moves it.

$ SOURCE是源文件,$ DEST是目标文件;这会附加后缀,而$ DEST已经存在,然后移动它。

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

#2


0  

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