0

I need to rename any file with the name already exists in the folder. After it is renamed that I can move it.

 if [ -e $BACKUP$PARAM1 ]; then
   echo -e "In this directory there is already a file with the same name. \n"
   read -p "Please, write a new filename: " newname
   mv "$PARAM1" "$newname"
   mv $PARAM1 $BACKUP
   fi

The first mv command doesn't work. Any idea to solve it?

I write a dirty simulation:

in $BACKUP$PARAM1 there is the full folder of a file moved to Backup directory. $BACKUP contains "Backup/", $PARAM1 contains "song.mp3", so $BACKUP$PARAM1 = Backup/song.mp3.

If in Backup folder already exists a file called "song.mp3", I'll rename my file before to move in Backup. So I write a new filename: "music.mp3", stored in $newname then proceed to the change of name using the mv command: mv "$PARAM1" "$newname"---> "song.mp3" to "music.mp3".

At the and, I move the renamed file to the "Backup" folder: mv $PARAM1 $BACKUP to obtain: "Backup/music.mp3".

jww
  • 97,681
  • 90
  • 411
  • 885

1 Answers1

2
if [ -e "$BACKUP$PARAM1" ]; then
   echo -e "In this directory there is already a file with the same name. \n"
   read -p "Please, write a new filename: " newname
   mv "$BACKUP$PARAM1" "$BACKUP$newname"
   fi

You can rename your file and at the same time move it to backup as above. Also use absolute pathnames for example instead of "$PARAM1" use "$BACKUP$PARAM1".

Also check to see if your path name for your backdirectory is correct for example instead of "Backup/" it could be "/Backup/" if the backup directory is in another directory

repzero
  • 8,254
  • 2
  • 18
  • 40