2

I have been trying to use the command line to delete all files in all subdirectories with the name s_1_1102_c.jpg.

This question is similar to what I need How to remove folders with a certain name but it is removing directories and I only want to delete the files with the name s_1_1102_c.jpg.

I will need to remove this file from 260 subdirectories under the L001 directory. My directory structure is like this:

L001
    C5.1
        s_1_1101_a.jpg
        s_1_1101_c.jpg
        s_1_1101_g.jpg
        s_1_1101_t.jpg
        s_1_1102_a.jpg
        s_1_1102_c.jpg
        s_1_1102_g.jpg
        s_1_1102_t.jpg
        s_1_1103_a.jpg
        s_1_1103_c.jpg
        s_1_1103_g.jpg
        s_1_1103_t.jpg
    C6.1
        s_1_1101_a.jpg
        s_1_1101_c.jpg
        s_1_1101_g.jpg
        s_1_1101_t.jpg
        s_1_1102_a.jpg
        s_1_1102_c.jpg
        s_1_1102_g.jpg
        s_1_1102_t.jpg
        s_1_1103_a.jpg
        s_1_1103_c.jpg
        s_1_1103_g.jpg
        s_1_1103_t.jpg

Ultimately I need to remove several files from all subdirectories (s_1_1101_g.jpg, s_1_1101_t.jpg, s_1_1102_a.jpg, s_1_1102_c.jpg, s_1_1102_g.jpg, s_1_1102_t.jpg). So maybe there is a way to provide a list of the file names I need to delete.

How can I delete these files?

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
aminards
  • 309
  • 2
  • 11
  • `rm L001/*/s_1_1102_c.jpg`? – Cyrus Jan 11 '22 at 18:04
  • `rm -f $(for f in in s_1_1101_g.jpg s_1_1101_t.jpg s_1_1102_a.jpg s_1_1102_c.jpg s_1_1102_g.jpg s_1_1102_t.jpg; do find . -name $f; done) ` if there are not more than maybe a hundred (command line length issues) – Peter - Reinstate Monica Jan 11 '22 at 18:06
  • I found this question https://stackoverflow.com/questions/27331548/how-can-i-delete-all-files-starting-with-from-the-shell-in-linux and can delete all of the same single file in my subdirectories using find /hpc/home/L001 -name "s_1_1102_c.jpg" -type f -delete Is there a way to expand this to remove all files in all subdirectories from a list of file names? – aminards Jan 11 '22 at 18:06
  • @aminards Yeah, do that in a for loop with a simple list of file names as in my example. `for f in name1 name2 name3; do find /hpc/home/L* -name $f -delete; done` – Peter - Reinstate Monica Jan 11 '22 at 18:07
  • Stack Overflow is for programming questions. General Linux usage questions should be posted to [unix.se] or [su] instead. – John Kugelman Jan 11 '22 at 18:09
  • @Cyrus I didn't know bash would substitute entire directories like that. Interesting! – Peter - Reinstate Monica Jan 11 '22 at 18:12
  • @Peter - Reinstate Monica thanks for the help. That worked. The syntax to add all of my file names was what ultimately helped. – aminards Jan 11 '22 at 18:17

3 Answers3

7
find . -name "s_1_1102_c.jpg" -exec rm -f {} \;

Note: This will find and delete the file in any subdirectory of the current one. So you could execute it in L001 or wherever else you want to do this.

Christian Fritz
  • 20,641
  • 3
  • 42
  • 71
1
for i in s_1_1101_g.jpg s_1_1101_t.jpg s_1_1102_a.jpg s_1_1102_c.jpg s_1_1102_g.jpg s_1_1102_t.jpg; do
  echo rm L001/*/"$i";
done

If output looks fine, remove echo.

Cyrus
  • 84,225
  • 14
  • 89
  • 153
0

The final method I used to delete my files was given by @Peter - Reinstate Monica

for f in s_1_1101_t.jpg s_1_1102_a.jpg s_1_1102_c.jpg s_1_1102_g.jpg s_1_1102_t.jpg s_1_1103_a.jpg s_1_1103_c.jpg s_1_1103_g.jpg s_1_1103_t.jpg s_1_1104_a.jpg s_1_1104_c.jpg s_1_1104_g.jpg s_1_1104_t.jpg s_1_2101_g.jpg s_1_2101_t.jpg s_1_2102_a.jpg s_1_2102_c.jpg s_1_2102_g.jpg s_1_2102_t.jpg s_1_2103_a.jpg s_1_2103_c.jpg s_1_2103_g.jpg s_1_2103_t.jpg s_1_2104_g.jpg s_1_2104_t.jpg; do find /hpc/home/L001 -name $f -delete; done

I was concerned that my file list would be too long but it worked in this situation.

aminards
  • 309
  • 2
  • 11