"Simple" Bash question

Here’s a question for bash aficionados out there:

I need to “normalize” the values of a variable that contains file paths: Some of these file paths contain blank spaces (go figure!). After normalization, I need to delete all files at each address. It sounds like a very simple question, yet I’ve struggled with it for most part of today, to no avail. Here’s the best I have, and it doesn’t work:

location=$(find /home/"$user"/.config/google-chrome -type d -name "GPUCache")
# readarray -d '' location < <(find /home/"$user"/.config/google-chrome -type d -name "GPUCache" -print0)
path=$(echo "$location" | sed 's/ /\\ /')
rm "$path"/*

Questions:

  1. Can this be done w/o appeal to sed? Is there something simpler that cleans up the path?
  2. Why does the rm line not work???
  1. Can this be done w/o appeal to sed? Is there something simpler that cleans up the path?

You can use double quotes around a variable that contains a path with spaces like “rm $path”.

  1. Why does the rm line not work???

Because all paths that are found are put in one long string in $path and $location.
Put some echo statements in after the assignments, i.e. “echo $path” and “echo $location” to see this.

This should do:

find /home/“$USER”/.config/googlechrome -type d -name GPUCache | while read path
do
rm “$path”/*
done

1 Like