これにより、先頭に inodenumber を付けた名前に名前を変更することで、重複した名前の衝突が処理されます。関数は厳密には必要ありません。
この方法は、ファイル名にスペースやタブが含まれているか、さらに悪いことに失敗する可能性があります。(ただし、スペースやタブを名前に入れる人は負けるに値します...)
#!/bin/sh
DIRS="dir1 dir2 dir3 dir4"
TARGET=$HOME/txt
move_one()
{
p=$1 # Full pathname
t=$2 # Target directory
i=$3 # Inode number from sourcefile
d=`dirname $p`
b=`basename $p`
#echo "p=$p"
#echo "d=$d" # the path-prefix from the source file
#echo "b=$b" # The name part of the source file
#echo "t=$t"
#echo "i=$i"
# After checking, remove the echo below for more fun
echo mv -i $p $t/$i$b
}
# inodenumber+name
find $DIRS -name \*txt -type f -ls | awk '{print $1," ",$11}' | (
while read i p; do
# echo "i=$i"
# echo "p=$p"
move_one $p $TARGET $i
done
)
#eof