How would I go about writing a script to loop through every folder on a hard drive, find any video files, extract them to the root of the hard drive, and then delete everything else?
|
Typically, if you need to "loop" over a set of files in Linux, you don't want to write an actual Bash loop (e.g. with Be careful with the following commands. If you don't know what you're doing or don't check the results first, you could irreversibly lose data.
find /mount/drive -type f \( -iname "*.mp4" -o -iname "*.avi" \) \
-exec echo mv '{}' /mount/drive/ \;
This would print a list of Note:
In a second step, we will delete everything that's not in the root of the drive. To do so, we need two commands, since you first want to delete all the files … find /mount/drive -mindepth 2 -type f Once you're sure this lists everything you want to delete, add Then, find the remaining empty folders: find /mount/drive -mindepth 1 -depth -type d -empty Similar to above, add |
|||||||
|
find, and in particular its-execswitch. Match file names via-iregexor-iname. – Daniel Andersson Feb 9 at 15:04