Sign in to follow this  
shanenin

Mv Command In Bash Script

Recommended Posts

I am writing a script to rename avi files. The current files have spaces in them

from the current directory I have these two titles. for example

"sopranos-301 the car episode"

"sopranos-302 the murder episode"

I am running the script from the directory the files are in

for i in *; do
   echo "enter the new name you would like to change the file to"
   read INPUT
   # i have the mv command in quotes because the file has spaces in the name
   mv \"$i\" ${INPUT}
done

it is giving me an arror about mv can't move multiple files to a non directory. As far as I can see

 \"$i\"

is only specifing one file

Share this post


Link to post
Share on other sites

Escaping the quotes is disabling their quoting behavior; you end up prepending a quote to the first word of the file name and append a quote to the last. Replace the escaped quotes with unescaped double quotes and it'll work.

Share this post


Link to post
Share on other sites

Thanks, that worked. doing dumb stuff like that, I wonder how I ever manage to get anything to work. If you care to save a few keystrokes when remnaming stuff feel free to use this script :wacko:

#!/bin/sh
# for this script to work you must be in the directory that contains the files you
# want to rename

clear
echo "Does the directory listed contain the files you would like to rename?"
echo
echo "`pwd`"
echo
echo "enter return to continue, or type 'quit'(and return) to exit the script and change to the correct directory"

read F
if [ $F = "quit" ]
   then exit
fi

clear

for i in *; do
   clear
   echo "do you want to change the name of the file"
   echo
   echo $i ?
   echo
   echo "enter 'y' to rename it, or 's' to skip this file, or 'q' to exit this program"

   read INPUT
   while [ ${INPUT} != y ] && [ ${INPUT} != s ] && [ ${INPUT} != q ]; do
       echo "please choose from choices y, s, or q"
       read INPUT
   done

   case ${INPUT} in
       y)  echo "enter the new name you would like to change the file to"
           read FILE_NAME
           mv "$i" ${FILE_NAME}
           if [ "$?" = 0 ]; then
               echo "the file named $i was changed to ${FILE_NAME}"
               echo "press return to continue"
               read pause
           else
               echo "an error occurred when moving the file, exiting the script"
               exit 1
           fi;;

       s)  echo "the file $i was not renamed";;

       q)  exit;;
   esac

done

Edited by shanenin

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this