Moving files and remaning them if that name is allready in use

Hi everyone.

I would like to how can i move a file to a directory (mv) but if in that directory there is a file with the same name it automatacly renames the file including .1 in the moving file name. For example:

I want to move file Test to the directory MyDirectory. But in MyDirectory there is allready a file with the name Test. After moving the Test file, the directory MyDirectory would have 2 files: Test and Test.1. If i would move another file with the name Test to MyDirectory it would end up like this: Test, Test.1 and Test.2.

I can't find a option on mv that would help me with this.
Thanks in advance.

I dont think a simple "mv" can do this.

You should write some script to achieve this.

Hi.

There is no such option, but using basic shell stuff it should be easy enough.

[highlight=bash]
#!/usr/bin/ksh

[ $# -eq 0 ] && echo "Provide a filename" && exit 1

C=1 # new default extention
TARGET=test # destination folder
FILE=$1
NEWFILE=$FILE

while true; do
if [ -f $TARGET/$NEWFILE ]; then
NEWFILE=$FILE.$C
C=$((C + 1))
else
mv $FILE $TARGET/$NEWFILE
break
fi
done

[/highlight]

The GNU version of mv has a -b option that does what you want.

Wow thanks for your promp answers guys. I'll try this. Thank you all :wink: