Home > Enterprise >  Delete files in a variable - bash
Delete files in a variable - bash

Time:12-16

i have a variable of filenames that end with a vowel. I need to delete all of these files at once. I have tried using

rm "$vowels"

but that only seems to return the files within the variable and state that there is "No such file or Directory"

CodePudding user response:

Its your use of quotes: they tell rm that your variables contents are to be interpreted as a single argument (filename). Without quotes the contents will be broken into multiple arguments using the shell rules in effect.

Be aware that this can be risky if your filenames contain spaces - as theres no way to tell the difference between spaces between filenames, and spaces IN filenames.

You can get around this by using an array instead and using quoted array expansion (which I cant remember the syntax of, but might look something like rm "${array[@]}" - where each element in the array will be output as a quoted string).

CodePudding user response:

SOLUTION

assigning the variable

vowel=$(find . -type f | grep "[aeiou]$")

removing all files within variable

echo $vowel | xargs rm -v

  • Related