这通过重命名为带有 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