Bash – How to delete files with spaces in them in bash script

bashscripting

this is a three part topic to a two part process that still is not working for me:

Objective: to manipulate a filename then delete it
the filename has spaces between the words then the dot .ext

Solution:
1) remove all the white space between the words making on long string of chars with no white space between then then delete that file.

or
2) place quotes on both sides of the filename with spaces then delete that file.

what I did to get my solution. using three different way to reach my solution for the first part of my objective, still not reaching my solution for the second part of my objective.

first how I got my filename out of a directory into a variable.

            find $PWD -name "*.mp3" | while [ $xf  -le $MAXNUM  ] ; 
                       do read FILENAME;

the variable $FILENAME returned the complete path/filename

'/home/userx/ffmpegdir/sub/file with — lots of spaces and __ —– _ &&???*# $$ LSD —-in the name _(3).mp3'

I used basename to strip the filename off that string to give me just the filename using just the pref and ext variables.

#strip the old file name off the path from FILENAME

                 c=$FILENAME
                 xpath=${c%/*} 
                 xbase=${c##*/}
                 xfext=${xbase##*.}
                 xpref=${xbase%.*}
                 path=${xpath}
                 pref=${xpref}
                 ext=${xfext}   

then I assign it to another variable.

                #puts quote marks on both sides of the file 
                #with spaces adding the dot (.)
                # extention "mp3" giving me a new file name
                #with the same ext. 

                differentquotes="${pref}.${ext}"

it returns this, the new file with ext. still having spaces and such between the words

  • differentquotes='file with — lots of spaces and __ —– _ &&???*# $$ LSD —-in the name _(3).mp3'

when I call rm to remove the file I add quotes around that file name like this

                      rm "\"${differentquotes}\""

that calls to the rm like this with the quotes on both end of the file

  • rm '"file with — lots of spaces and __ —– _ &&???*# $$ LSD —-in the name _(3).mp3"'

then all I get is a return error like this

rm: cannot remove `"file with — lots of spaces and __ —– _ &&???*# $$ LSD —-in the name _(3).mp3"': No such file or directory

so I decided to try just removing all the spaces between the file name then tried to delete it like this

                        oldname="${pref// }.${ext}"
                        echo "${oldname}" "the hole name now is that "
                        echo
                        echo "calling rm" "${oldname}\"\""
                        rm "${oldname}"

and I got this result, it removed all the spaces between the file name but I still received an error return from rm.

  • oldname='filewith–lotsofspacesand_—–&&???*#$$LSD—-inthename_(3).mp3'

  • rm 'filewith–lotsofspacesand_—–&&???*#$$LSD—-inthename_(3).mp3'
    rm: cannot remove `filewith–lotsofspacesand_—–&&???*#$$LSD—-inthename_(3).mp3': No such file or directory

so I then tried it like this. I removed all the space within just the pref var first while assigning it to another var name then put the ext on to it then tried to remove it and got this with the same error

                anothernewname="${pref// }"
                echo ${anothernewname}
                putthemtogether="${anothernewname}.${ext}"
                echo ${putthemtogether}
                rm "${putthemtogether}"

I removed all the spaces between the words in just the pref :
+ anothernewname='filewith–lotsofspacesand_—–&&???*#$$LSD—-inthename_(3)'

then I put the ext back onto the different variable name here:

  • putthemtogether='filewith–lotsofspacesand_—–&&???*#$$LSD—-inthename_(3).mp3'

then I called rm to remove it and still got the same error :

  • rm 'filewith–lotsofspacesand_—–&&???*#$$LSD—-inthename_(3).mp3'

rm: cannot remove `filewith–lotsofspacesand_—–&&???*#$$LSD—-inthename_(3).mp3': No such file or directory
+ echo

all three times I get the same error- No such file or directory

what is the answer to “what I am doing wrong so that I no longer do it?”

added this:
I just tried this: adding the path vatiable to tell it where it is path/filename

                  rm "${path}"/"\"${differentquotes}\""

giving me still an error:

    rm: cannot remove `/home/userx/ffmpegdir/sub/"file  with -- lots of spaces and ____ ----- ___ &&???*# $$ LSD ----in the name _(3).mp3"': No such file or directory

if I put that into the terminal just like that it would work: rm path/"file name with spaces.ext" just as it reads above — is that not a yes, too?

Best Answer

Always quote your substitutions. Do not add spurious quotes to your strings.

foo="${file#...}"
bar="${file%...}"
baz="$file"
Related Question