0

How do I best handle a mix of files and directories in an array. I need to create an array of files and folders and loop through the array and subject each item in the array to the rm command with the -rf options:

#Build array of files to be deleted

FILES=(
"~/Library/Preferences/Adobe InDesign"
"~/Library/Caches/Adobe InDesign"
"~/Library/Saved Application State/com.adobe.InDesign.savedstate"
)


#Loop through array deleting each file/directory with the recursive force options

    for i in "${FILES[@]}"

      do

        rm -rf "$i"

      done

    exit
7
  • 1
    using the shell "debugger" set -vx will show you how your command is being processed. I bet you don't want dbl-quotes around ${FILES[@]}. Good luck. Commented Sep 27, 2012 at 18:25
  • actually this code should work, any errors you get from it? Commented Sep 27, 2012 at 18:48
  • @mpapis it executes with no error. I used shellter's approach with debugging...it isn't doing anything funny with parsing the whitespace in the variable names...but it does not delete the files? I feel like i'm losing my mind. Commented Sep 27, 2012 at 18:56
  • verified permissions as well, even created a new folder called "test" and a new file "testfile" and it won't delete them either. Commented Sep 27, 2012 at 18:57
  • turn off debugging temporarily, add echo in front of rm and edit your post with the output? Loosing your mind, that's not good. Oh hey, I had a crazy spot yesterday, I had imported a shell script with \r\n (DOS) line endings and vim didn't show the ^M that I would expected AND the results were completely mystifying. cat -vet myScript to eliminate this possibility. Good luck. Commented Sep 27, 2012 at 19:29

2 Answers 2

2

The problem isn't with the array or spaces, it's with the ~ in the file paths. ~ isn't really a valid part of a path, but the shell will replace replace it with your home directory's path ... unless it's in quotes:

$ echo ~/Library/Preferences/Adobe InDesign
/Users/gordon/Library/Preferences/Adobe InDesign
$ echo "~/Library/Preferences/Adobe InDesign"
~/Library/Preferences/Adobe InDesign

So you can either put the ~ outside the quotes:

FILES=(
~/"Library/Preferences/Adobe InDesign"
~/"Library/Caches/Adobe InDesign"
~/"Library/Saved Application State/com.adobe.InDesign.savedstate"
)

Or use $HOME (which does get substituted in double-quotes):

FILES=(
"$HOME/Library/Preferences/Adobe InDesign"
"$HOME/Library/Caches/Adobe InDesign"
"$HOME/Library/Saved Application State/com.adobe.InDesign.savedstate"
)
Sign up to request clarification or add additional context in comments.

1 Comment

Can't believe I didn't realize that the shell wasn't expanding ~ because it was quoted. Thank you for catching this. Excellent explanation.
0

I have used this trick sometimes:

with_expanded_paths()
{
  typeset IFS
  IFS=""
  "$@"
}
with_expanded_paths rm -rf "~/file with spaces"

I wrote it from memory so slight adjustments might be needed.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.