Skip to content
Advertisement

Move a file and rename it if the destination folder contains also a file with the same name

Sorry for my bad english. I want to move a file from the Desktop to user’s input destination. If destination has also a file with the same name, how can i ask user to rename the file so that he can have them both in destination folder? I have this right now:

#!bin/bash
echo "path"
read path
echo "Do you want to move code files somewhere else?"
read -t 5 y
if [[ $y = 'yes' ]] ; then
echo "Tell me where:"
read mydest
find $path -type f ( -name "*.c" -or -name "*.cxx" -or -name "*.cpp" -or -name "*.cc" ) );do
mv ???
fi 

Advertisement

Answer

Demonstrating using mv and switches -i (interactive) and --backup:

$ echo foo > foo              # making test files
$ echo bar > bar
$ mv -i --backup foo bar      # mving foo
mv: overwrite 'bar'? y        # -i caused this
$ cat bar                     
foo                           # mv caused this
$ cat bar~                    # --backup caused this
bar
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement