How do I Mass delete folders containing a file extention and all files in that folder?


#1

I made a script to upload my files to gdrive and it messed things up. Now that I fixed the script im trying to clean up the mess I made in the beginning.

Using rclone I need to recursively find directory’s with “.mkv” in the name. And delete that dir with all files inside that dir.

EDIT: I need to recursively find directory’s with “.mkv” in the name. Then move the files in that dir back one dir. Then delete the dir with ".mkv"

Here is a dir structure.

List of DIR’s in path “gdrive:Media Server/TV Shows/The Daily Show/Season 23”

rclone lsd “gdrive:Media Server/TV Shows/The Daily Show/Season 23”
-1 2017-11-07 05:02:25 -1 The Daily Show - S23E15 - 2017-11-01 - Hillary Clinton WEBDL-1080p.mkv
-1 2017-11-07 04:14:17 -1 The Daily Show - S23E16 - 2017-11-02 - Gabrielle Union WEBDL-1080p.mkv
-1 2017-11-07 12:15:55 -1 The Daily Show - S23E17 - 2017-11-06 - Sen. Jeff Flake & Tig Notaro WEBDL-1080p.mkv
-1 2017-11-08 05:05:09 -1 The Daily Show - S23E18 - 2017-11-07 - Jeff Ross WEBDL-1080p.mkv

List of FILES in path “gdrive:Media Server/TV Shows/The Daily Show/Season 23”

rclone ls “gdrive:Media Server/TV Shows/The Daily Show/Season 23”
86048 The Daily Show - S23E01 - 2017-09-25 - Katy Tur WEBDL-1080p-thumb.jpg
804313228 The Daily Show - S23E01 - 2017-09-25 - Katy Tur WEBDL-1080p.mkv
1094 The Daily Show - S23E01 - 2017-09-25 - Katy Tur WEBDL-1080p.nfo
629822097 The Daily Show - S23E02 - 2017-09-26 - Kathryn Miles HDTV-720p.mkv
700086557 The Daily Show - S23E02 - 2017-09-26 - Kathryn Miles Unknown-1080p.mkv
395896569 The Daily Show - S23E02 - 2017-09-26 - Kathryn Miles Unknown-720p.mkv
466828632 The Daily Show - S23E02 - 2017-10-03 - Jim Himes HDTV-720p.mkv
86981 The Daily Show - S23E02 - 2017-10-03 - Jim Himes Unknown-1080p-thumb.jpg
728888422 The Daily Show - S23E02 - 2017-10-03 - Jim Himes Unknown-1080p.mkv
1066 The Daily Show - S23E02 - 2017-10-03 - Jim Himes Unknown-1080p.nfo
399007075 The Daily Show - S23E02 - 2017-10-03 - Jim Himes Unknown-720p.mkv
803245734 The Daily Show - S23E03 - 2017-09-27 - America Ferrera Unknown-1080p.mkv
450819001 The Daily Show - S23E03 - 2017-09-27 - America Ferrera Unknown-720p.mkv
496502782 The Daily Show - S23E03 - 2017-10-04 - John Hodgman HDTV-720p.mkv
82460 The Daily Show - S23E03 - 2017-10-04 - John Hodgman Unknown-1080p-thumb.jpg
682499369 The Daily Show - S23E03 - 2017-10-04 - John Hodgman Unknown-1080p.mkv
1083 The Daily Show - S23E03 - 2017-10-04 - John Hodgman Unknown-1080p.nfo
415422211 The Daily Show - S23E03 - 2017-10-04 - John Hodgman Unknown-720p.mkv
83115 The Daily Show - S23E04 - 2017-10-05 - Kenya Barris Unknown-1080p-thumb.jpg
799393258 The Daily Show - S23E04 - 2017-10-05 - Kenya Barris Unknown-1080p.mkv
1072 The Daily Show - S23E04 - 2017-10-05 - Kenya Barris Unknown-1080p.nfo
447640089 The Daily Show - S23E04 - 2017-10-05 - Kenya Barris Unknown-720p.mkv
654964627 The Daily Show - S23E05 - 2017-10-02 - Robin Thede Unknown-1080p.mkv
366116809 The Daily Show - S23E05 - 2017-10-02 - Robin Thede Unknown-720p.mkv
752840903 The Daily Show - S23E05 - 2017-10-16 - Common Unknown-1080p.mkv
0 The Daily Show - S23E05 - 2017-10-16 - Common Unknown-1080p.nfo
462122665 The Daily Show - S23E05 - 2017-10-16 - Common Unknown-720p.mkv
86491 The Daily Show - S23E05 - 2017-10-16 - Common, From Chicago Unknown-1080p-thumb.jpg
0 The Daily Show - S23E05 - 2017-10-16 - Common, From Chicago Unknown-1080p.mkv
496 The Daily Show - S23E05 - 2017-10-16 - Common, From Chicago Unknown-1080p.nfo
0 The Daily Show - S23E05 - 2017-10-16 - Common, From Chicago Unknown-720p.mkv
77546 The Daily Show - S23E06 - 2017-10-17 - Arne Duncan & Curtis Toler, From Chicago Unknown-1080p-thumb.jpg
0 The Daily Show - S23E06 - 2017-10-17 - Arne Duncan & Curtis Toler, From Chicago Unknown-1080p.mkv
518 The Daily Show - S23E06 - 2017-10-17 - Arne Duncan & Curtis Toler, From Chicago Unknown-1080p.nfo
88203 The Daily Show - S23E07 - 2017-10-18 - Lena Waithe, From Chicago Unknown-1080p-thumb.jpg
779065027 The Daily Show - S23E07 - 2017-10-18 - Lena Waithe, From Chicago Unknown-1080p.mkv
1092 The Daily Show - S23E07 - 2017-10-18 - Lena Waithe, From Chicago Unknown-1080p.nfo
894365783 The Daily Show - S23E08 - 2017-10-19 - Vic Mensa, From Chicago WEBDL-1080p.mkv
490193440 The Daily Show - S23E09 - 2017-10-23 - Khizr Khan HDTV-720p.mkv
87351 The Daily Show - S23E09 - 2017-10-23 - Khizr Khan Unknown-1080p-thumb.jpg
764288936 The Daily Show - S23E09 - 2017-10-23 - Khizr Khan Unknown-1080p.mkv
1075 The Daily Show - S23E09 - 2017-10-23 - Khizr Khan Unknown-1080p.nfo
461455973 The Daily Show - S23E09 - 2017-10-23 - Khizr Khan Unknown-720p.mkv
90804 The Daily Show - S23E10 - 2017-10-24 - Margo Price Unknown-1080p-thumb.jpg
796484900 The Daily Show - S23E10 - 2017-10-24 - Margo Price Unknown-1080p.mkv
1075 The Daily Show - S23E10 - 2017-10-24 - Margo Price Unknown-1080p.nfo
466335593 The Daily Show - S23E10 - 2017-10-24 - Margo Price Unknown-720p.mkv
85891 The Daily Show - S23E11 - 2017-10-25 - Maggie Gyllenhaal Unknown-1080p-thumb.jpg
670109822 The Daily Show - S23E11 - 2017-10-25 - Maggie Gyllenhaal Unknown-1080p.mkv
1066 The Daily Show - S23E11 - 2017-10-25 - Maggie Gyllenhaal Unknown-1080p.nfo
390967146 The Daily Show - S23E11 - 2017-10-25 - Maggie Gyllenhaal Unknown-720p.mkv
85067 The Daily Show - S23E12 - 2017-10-26 - Miles Teller & Jason Hall Unknown-1080p-thumb.jpg
699140022 The Daily Show - S23E12 - 2017-10-26 - Miles Teller & Jason Hall Unknown-1080p.mkv
1100 The Daily Show - S23E12 - 2017-10-26 - Miles Teller & Jason Hall Unknown-1080p.nfo
404499016 The Daily Show - S23E12 - 2017-10-26 - Miles Teller & Jason Hall Unknown-720p.mkv
84278 The Daily Show - S23E13 - 2017-10-30 - Ta-Nehisi Coates Unknown-1080p-thumb.jpg
729639461 The Daily Show - S23E13 - 2017-10-30 - Ta-Nehisi Coates Unknown-1080p.mkv
1098 The Daily Show - S23E13 - 2017-10-30 - Ta-Nehisi Coates Unknown-1080p.nfo
433749658 The Daily Show - S23E13 - 2017-10-30 - Ta-Nehisi Coates Unknown-720p.mkv
85412 The Daily Show - S23E14 - 2017-10-31 - Gretchen Carlson Unknown-1080p-thumb.jpg
710751332 The Daily Show - S23E14 - 2017-10-31 - Gretchen Carlson Unknown-1080p.mkv
1064 The Daily Show - S23E14 - 2017-10-31 - Gretchen Carlson Unknown-1080p.nfo
430816443 The Daily Show - S23E14 - 2017-10-31 - Gretchen Carlson Unknown-720p.mkv
86535 The Daily Show - S23E15 - 2017-11-01 - Hillary Clinton WEBDL-1080p-thumb.jpg
1065699439 The Daily Show - S23E15 - 2017-11-01 - Hillary Clinton WEBDL-1080p.mkv
1062 The Daily Show - S23E15 - 2017-11-01 - Hillary Clinton WEBDL-1080p.nfo
85186 The Daily Show - S23E16 - 2017-11-02 - Gabrielle Union WEBDL-1080p-thumb.jpg
939687339 The Daily Show - S23E16 - 2017-11-02 - Gabrielle Union WEBDL-1080p.mkv
1072 The Daily Show - S23E16 - 2017-11-02 - Gabrielle Union WEBDL-1080p.nfo
83413 The Daily Show - S23E17 - 2017-11-06 - Sen. Jeff Flake & Tig Notaro WEBDL-1080p-thumb.jpg
969803292 The Daily Show - S23E17 - 2017-11-06 - Sen. Jeff Flake & Tig Notaro WEBDL-1080p.mkv
1067 The Daily Show - S23E17 - 2017-11-06 - Sen. Jeff Flake & Tig Notaro WEBDL-1080p.nfo
635705879 The Daily Show - S23E18 - 2017-11-07 - Jeff Ross WEBDL-1080p.mkv
832 The Daily Show - S23E18 - 2017-11-07 - Jeff Ross WEBDL-1080p.nfo
650189281 The Daily Show - S23E19 - 2017-11-08 - Kenneth Branagh WEBDL-1080p Proper.mkv
650253894 The Daily Show - S23E19 - 2017-11-08 - Kenneth Branagh WEBDL-1080p.mkv
253627890 The Daily Show - S23E19 - 2017-11-08 - Kenneth Branagh WEBDL-480p.mkv
378603136 The Daily Show - S23E19 - 2017-11-08 - Kenneth Branagh WEBDL-720p.mkv
761084896 The Daily Show - S23E20 - 2017-11-09 - Van Jones WEBDL-1080p.mkv
283840966 The Daily Show - S23E20 - 2017-11-09 - Van Jones WEBDL-480p.mkv
640088904 The Daily Show - S23E21 - 2017-11-13 - Hari Kondabolu WEBDL-1080p.mkv
370464164 The Daily Show - S23E21 - 2017-11-13 - Hari Kondabolu WEBDL-720p.mkv
649111529 The Daily Show - S23E22 - 2017-11-14 - 2 Chainz WEBDL-1080p.mkv
377465493 The Daily Show - S23E22 - 2017-11-14 - 2 Chainz WEBDL-720p.mkv
654740849 The Daily Show - S23E23 - 2017-11-15 - Elaine McMillion Sheldon WEBDL-1080p.mkv
729831297 The Daily Show - S23E24 - 2017-11-16 - Jordan Peele WEBDL-1080p.mkv
427667103 The Daily Show - S23E24 - 2017-11-16 - Jordan Peele WEBDL-720p.mkv
831364297 The Daily Show - S23E25 - 2017-11-27 - Esther Perel WEBDL-1080p.mkv
479154257 The Daily Show - S23E25 - 2017-11-27 - Esther Perel WEBDL-720p.mkv
627692210 The Daily Show - S23E26 - 2017-11-28 - Greta Gerwig HDTV-720p.mkv
670415844 The Daily Show - S23E26 - 2017-11-28 - Greta Gerwig WEBDL-1080p.mkv
386119014 The Daily Show - S23E26 - 2017-11-28 - Greta Gerwig WEBDL-720p.mkv
713446757 The Daily Show - S23E27 - 2017-11-29 - Talib Kweli WEBDL-1080p.mkv
409140430 The Daily Show - S23E27 - 2017-11-29 - Talib Kweli WEBDL-720p.mkv
550459800 The Daily Show - S23E28 - 2017-11-30 - Henry Louis Gates Jr HDTV-720p.mkv
708526279 The Daily Show - S23E28 - 2017-11-30 - Henry Louis Gates Jr WEBDL-1080p.mkv
389046022 The Daily Show - S23E28 - 2017-11-30 - Henry Louis Gates Jr WEBDL-720p.mkv
600261402 The Daily Show - S23E29 - 2017-12-04 - Frankie Shaw HDTV-720p.mkv
726248998 The Daily Show - S23E29 - 2017-12-04 - Frankie Shaw WEBDL-1080p.mkv
422709185 The Daily Show - S23E29 - 2017-12-04 - Frankie Shaw WEBDL-720p.mkv
662680756 The Daily Show - S23E30 - 2017-12-05 - December 5, 2017 WEBDL-1080p.mkv
378887238 The Daily Show - S23E30 - 2017-12-05 - December 5, 2017 WEBDL-720p.mkv
713569938 The Daily Show - S23E30 - 2017-12-05 - Julia Ioffe HDTV-720p.mkv
604350154 The Daily Show - S23E31 - 2017-12-06 - St. Vincent HDTV-720p.mkv
750525177 The Daily Show - S23E31 - 2017-12-06 - St. Vincent WEBDL-1080p.mkv
609669840 The Daily Show - S23E32 - 2017-12-07 - Tiffany Haddish HDTV-720p.mkv
843574976 The Daily Show - S23E32 - 2017-12-07 - Tiffany Haddish WEBDL-1080p.mkv
499223611 The Daily Show - S23E32 - 2017-12-07 - Tiffany Haddish WEBDL-720p.mkv
724935022 The Daily Show - S23E33 - 2017-12-11 - Pete Souza WEBDL-1080p.mkv
410292203 The Daily Show - S23E33 - 2017-12-11 - Pete Souza WEBDL-720p.mkv
704682697 The Daily Show - S23E34 - 2017-12-12 - Bob Odenkirk WEBDL-1080p.mkv
839916513 The Daily Show - S23E35 - 2017-12-13 - Satya Nadella WEBDL-1080p.mkv
667639426 The Daily Show - S23E36 - 2017-12-14 - Niecy Nash WEBDL-1080p.mkv
773271130 The Daily Show - S23E37 - 2018-01-02 - January 2, 2018 WEBDL-1080p.mkv
307271673 The Daily Show - S23E37 - 2018-01-02 - January 2, 2018 WEBDL-480p.mkv
456488518 The Daily Show - S23E37 - 2018-01-02 - January 2, 2018 WEBDL-720p.mkv
620311668 The Daily Show - S23E37 - 2018-01-02 - Yara Shahidi HDTV-720p.mkv
533113375 The Daily Show - S23E38 - 2018-01-03 - Dan Harris HDTV-720p.mkv
725878579 The Daily Show - S23E38 - 2018-01-03 - Dan Harris WEBDL-1080p.mkv
585865652 The Daily Show - S23E39 - 2018-01-04 - Jodi Kantor HDTV-720p.mkv
789263815 The Daily Show - S23E39 - 2018-01-04 - Jodi Kantor WEBDL-1080p.mkv
298746189 The Daily Show - S23E39 - 2018-01-04 - Jodi Kantor WEBDL-480p.mkv
635705879 The Daily Show - S23E18 - 2017-11-07 - Jeff Ross WEBDL-1080p.mkv/The Daily Show - S23E18 - 2017-11-07 - Jeff Ross WEBDL-1080p.mkv
1065699439 The Daily Show - S23E15 - 2017-11-01 - Hillary Clinton WEBDL-1080p.mkv/The Daily Show - S23E15 - 2017-11-01 - Hillary Clinton WEBDL-1080p.mkv
939687339 The Daily Show - S23E16 - 2017-11-02 - Gabrielle Union WEBDL-1080p.mkv/The Daily Show - S23E16 - 2017-11-02 - Gabrielle Union WEBDL-1080p.mkv
969803292 The Daily Show - S23E17 - 2017-11-06 - Sen. Jeff Flake & Tig Notaro WEBDL-1080p.mkv/The Daily Show - S23E17 - 2017-11-06 - Sen. Jeff Flake & Tig Notaro WEBDL-1080p.mkv

I’ve tried mounting the gdrive and running…

find “/home/rick/.gdrive/Media Server/TV Shows/The Daily Show/Season 23/” -name “*.mkv” -type d -exec rm -rf {} ;

But since the folders I want to “rm -rf” are named the same as an actual file It doesn’t find the dir when mounted.

So basically I need to run a command that will do what this command would do…

find “/home/rick/.gdrive/Media Server/TV Shows/The Daily Show/Season 23/” -name “*.mkv” -type d -exec rm -rf {} ;

But with rclone so it can see those folders.

I’ve tried rclone dedupe but it doesn’t do anything to the folders named the same as a file.

How could I achieve this recursively at “gdrive:Media Server/TV Shows/” as I have lots of shows that need to be cleaned up?

Summary

This text will be hidden


#2

I’d do this through an rclone mount… So assuming you’ve mounted your drive at /mnt

Then find all the directories

find /mnt -type d -name '*.mkv' > dirs-to-move

Then move them something like this (untested!)

for i in `cat dirs-to-move` ; do
  echo moving $i
  parent=`dirname $i`
  mv -i "$i"/* "$parent"/
  rmdir "$i"
done

#3

Thanks I got it all cleaned up now. :slight_smile: