Tuesday, 15 April 2014

bash - Recursively iterating through subdirectories and removing certain file -


I have music collections with many folders and sub-folders (cover art etc.), so manually remove hundreds Instead of Folder.jpg, Desktop.ini and Thumb.db files, I decided to do a simple Bash script but things really got messy.

I did a simple test by creating dummy folders like this: / home / dummy / all 1 - all 1 sub 1 sub 1 sub 1 sub 1 sub 1 sub 1 all 2 - all 2 sub 1 all 2 sub 2 All 2 SUB 2 SUB 1

And some random jpg in these memories. MP3, my bass script currently looks like this:

  function delete_jpg_ini_db {if [[$ f == * .jpg]]; Then resonance ".jpg file, $ f removal" gvfs-trash $ f elif [[$ f == * .i]]]; Then remove the ".inie file", $ f, "gvfs-trash -f $ f elif [[$ f == * .db]]; then" echo ".db file, extract $ f" gvfs-trash -f $ F else echo "is not a .jpg, .ini or .db file, $ f 'fi} for function iterate_dir {d *; if [-d $ d]; then inserting" sub-directory: $ d " For amp; CD $ d for PWD F *, if [-f $ f]; then check # .jpg, .ini or .db, then delete delete_jpg_ini_db elif [-d $ f]; then Repeat the center sub-diary and repeat again if echo "[$ (ls-A $ f)"]; then iterate_dir else "sub-directory $ f is empty!" It successfully goes through all 1, all 1 SUB1 and all 1 SUB 1 SUB1, but it stops there instead of going back home and searching for the next subbase.  < P> I am new in Bash scripting, all help is appreciated .. Thanks.   

and In an order you can run:

  find / home / dummy / all 1-name "* .jpg" -o -name "* .ini" -o -name "* .db "-Detailed  

And if you want to see it What files will be deleted, replace -delete with -print (just the filename) or with -ls (like Ls -l output).


No comments:

Post a Comment