0

I am having an array filesToDelete and getting the following output for echo $filesToDelete

value1 value2 value3 value4

When i am trying to loop through the array to delete the files from the current folder:

for i in '${filesToDelete[@]}'
    do
               # rm $i
              rm $i*.*;
    done

it says the following:

rm: ”${filesToDelete[@]}*.*” can not be deleted: no such file or directroy

the files are exists, and i am in the right directory to be able to execute the deleting method, but still getting the error.

if i change the looping for:

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

i am getting the Bad substitution error for the line containing done

As i looked up for google it was recommended to use #!/bin/bash in the first line, and this is the same for me.

Any idea how to delete the files from the array?

2 Answers 2

1

There were a simple anwer here, but the user deleted it for some reason, so here it is, simple and brilliant :)

for file in $filesToDelete 
    do
    rm "$file"*.*;
done

This is much simpler then my apprach. Thanks for him, whoever was he!

Sign up to request clarification or add additional context in comments.

2 Comments

Then there's no array involved.
If filesToDelete is an array, then this won't work. It will only delete the first file in the array. If this works, it suggests that you have a string with multiple words in it, instead of an array. You should edit your question to show how the variable is defined.
1

If echo $filesToDelete prints the list of elements, it's not an array; if it were an actual array that would only print the first element. I'm pretty sure what you actually have is just a string with spaces separating the elements.

The immediate problem you have is that you've put the variable (/array) reference in single-quotes, and the shell doesn't perform variable expansion inside single-quotes. In fact, it doesn't do any parsing of the contents of single-quotes (except to look for the close quote). In the shell, there are 3 main quoting contexts:

  • Inside single quotes, no interpretation (of variable references, wildcards, etc) is performed at all.
  • Inside double-quotes, variable references (and other things starting with $) are done, as are backquoted commands and some escape (\) sequences. Once variables (etc) have been substituted, no parsing is done on the substituted values (which is almost always what you want).
  • When unquoted, variable references (and many other things) are done, and then the substituted values are split into words (separated by spaces and other whitespace), and any wildcards are substituted.

Since filesToDelete is apparently a plain string with spaces separating the filenames, you need to refer to it unquoted to get it split into words. But this can fail catastrophically if any of the filenames contain spaces and/or wildcards. My standard example of this was Apple's installer for iTunes 2.0, which contained a command to delete the old version; something like rm -R $1. If the old version was located at e.g. /Volumes/Data 2/Applications/iTunes.app, this would delete the entire contents of /Volumes/Data, then complain that 2/Applications/iTunes.app didn't exist. Apple released version 2.0.1 very very quickly after that, and the only change was to add double-quotes around the reference to $1. IIRC they also wound up buying data recovery software for some of their customers.

If you need to store multiple filenames safely, use a real array. Create it with something like filesToDelete=(value1 value2 value3 value4) (note that word splitting and wildcard expansion is performed here as well, so wrap any variable references in double-quotes). Then use it with something like:

for i in "${filesToDelete[@]}"; do
    rm "$i"*.*
done

Note that in the rm command, I put double-quotes around the variable reference, but not the parts that need to be interpreted as wildcards. (Also, in the shell a semicolon is not needed at the end of a line, so I left it off.)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.