Score:0

How to delete a list of filenames without path that are matched in multiple subdirectories?

uz flag

How to delete a list of filenames without path that are matched in multiple subdirectories?

Delete_list.txt may contain (each filename is per line):

a.jpg
b.jpg
c.jpg

These files may exists in multiple subdirectories. I want to delete them all. I tried but no success:

for f in $(delete_list.txt); do find . -name "$f" -type f -delete  ; done 

2nd try

  f in $(delete_list.txt);  do find . -name "$f" -type f -exec rm -f {} \;

3rd try

while IFS= read -r file ; do rm -r -- "$file" ; done < delete_list.txt

4rth try

for f in $(delete_list.txt); do if [ -f $f ]; then rm $f; fi; done
ar flag
Could you please rewrite the script so that each command is in separate lines and indented where appropriate? This is very hard to read. It seems like you have 3 different attempts of the script all jumbled together.
bac0n avatar
cn flag
Note: even if it's not recommended, and I strongly advise against it, the syntax is `$(<delete_list.txt)`, which is a substitute for `$(cat delete_list.txt)`.
Score:3
hr flag

In a shell such as bash that supports arrays you could assemble all of the names into a single find predicate

#!/bin/bash

declare -a namep=()

while IFS= read -r name; do 
  namep+=( -name "$name" -o )
done < delete_list.txt

unset namep[-1]    # remove the extraneous trailing -o

find . -type f \( "${namep[@]}" \) -print

Change -print to -delete (or -print -delete) only when you are certain it is finding the right files.

Elias Estatistics avatar
uz flag
So fast! In few minutes deletes many thousand files under thousand subdirectories!!! Can you explain more what is "array" in bash and how it is utilized / used in the system? And why it is much quicker than my option?
Elias Estatistics avatar
uz flag
What it means "extraneous leading"?
hr flag
@EliasEstatistics we want the sequence to start with a `-name` not a `-o`, so we remove the 0th array element
hr flag
... I just modified the sequence a bit - maybe it's clearer this way?
Elias Estatistics avatar
uz flag
I must read about arrays in linux and "extraneous leading". Thanks anyway.
Score:0
uz flag

This did the trick for me. It is deleting all matched filenames in the given files under all subdirectories.

However, it is extremely slow. The delete_list.txt contains around 100 filenames to be found under 250 thousands+ subdirectories.

 while read -r FILE; do echo "$(find . -name "$FILE" -type f -delete): $FILE"; done < delete_list.txt
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.