0

I'm using find to generate a list of old backups, compressed tars, into a file, then reading that file into while loop. I out put the list to a log and screen, both looking correct.

echo $x works fine; /vol4/backups/forms_02_24_2024.tar.gz

But "rm $x" doesn't work, because it's receiving changed line below;

rm: cannot remove '(arr+=(/vol4/backups/forms_02_24_2024.tar.gz))' :No such file or directory

arr()
while read line; do
   x="(arr+=("$line"))"
   echo $x
   rm $x
done < ./old_backups.txt

Tried using cut and sed to modify the line, but I still didn't get what rm needed, which is simply the path to each old file. But rm is still no getting the correct file names.

6
  • 1
    x is just a string, not an array, so the += is only a literal, not adding anything. Why can't you just rm $line? Commented Mar 14, 2024 at 4:37
  • The error message tells you where the problem lies. The path you are trying to remove is (arr+=(/vol4/backups/forms_02_24_2024.tar.gz)). Do you not realize that is from the third line of your program? Why do you even have a arr() array since yo¨don't use it? Just rm "$line". Commented Mar 14, 2024 at 6:01
  • Does your version of find support -delete? Commented Mar 14, 2024 at 6:39
  • (look into readarray instead of a loop to populate an array with the lines of a file) Commented Mar 14, 2024 at 6:40
  • The output from the echo command you post here, does not fit the error message from the rm command. I don't believe that you posted this information correctly. Provide a screenshot of what happens when you run your script. Commented Mar 14, 2024 at 8:17

3 Answers 3

0

If you want to remove all the files listed in old_backups.txt:

xargs -t rm < ./old_backups.txt
Sign up to request clarification or add additional context in comments.

Comments

0

I assume you have a reason for wanting an array with the list of all the files that were deleted. If so, does this solve your problem:

arr=()
while read line; do
   arr+=($line)
   echo $line
   rm $line
done < ./old_backups.txt
echo "The following files were deleted: ${arr[*]}."

Obviously, the last line was more of an example of how you could use the arr array.

Comments

-1

Before rm, we can check for the file's existence. As a one-liner:

while read line; do [ -f ${line} ] && rm ${line}; done < ./old_backups.txt

Here, [ -f ${line} ] check if file exists, and if and only if, the next instruction (rm ${line}) executes as enforced by the && operator.

3 Comments

The file itself already exists, it's shown in the echo display and log, the issue is that additional symbols are added once that file and path are fed to rm. But thank you,
Yes, that worked like a charm, but any chance you can direct me to any white paper on why. On first glance, the likes look identical, so I'm missing the way? Thank you again, but if you don't mind, the why.
@user23562505, all did is add a simple if check on the file. Updated the answer with references. If you are a novice in shell scripting, bash cookbook is a great book. (Please mark the answer as accepted, if it helped)

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.