user5849928
user5849928

Reputation:

BASH: How would I modify this script to allow file names with spaces?

iphone=($( find /  -name "*~iphone*" ) )
echo ${#iphone[@]}
rm -r ${iphone[@]} >/dev/null 2>&1

This is a portion of a script I have that should remove all files with "~iphone" anywhere in the name, as well as echoing the amount of files it found. So how might this script be modified to allow file names with spaces, as right now it messes with the count and doesn't delet the files.

Upvotes: 1

Views: 64

Answers (1)

David C. Rankin
David C. Rankin

Reputation: 84561

One quick way to handle this would be with process substitution. (note: the remove rm is commented until you confirm desired behavior)

#!/bin/bash

while read -r fname; do
    echo "$fname"
    # rm -r "$fname" >/dev/null 2>&1
done < <(find / -name "*~iphone")

This would find all files/directories containing "*~iphone" regardless of spaces and allow deletion. If you want to build an array allowing spaces in the filename, just add "$fname" to the array each time through the loop.

Upvotes: 1

Related Questions