78480

Bash script for searching directory and moving files to a new directory, deleting any copies

Question:

I have a 2TB hard drive full of backed up data, including hundreds of movies, most of which have been copied several times due to sequential back-ups of the same HD. The hard drive is organized as a list of back-up folders, and every back-up contains a movies folder that has all the movies that were on my laptop HD at the time of the back-up.

I'd like to create a new movies folder and move all movies from every "movies" subfolder into the new one, making sure not to move the same movie twice. How can I go about this if I want to do everything via Bash?

Answer1:

Assuming that each copy of a movie in various folders has the same name and all have the same extension, let's say .divx, you can use find to find them and copy them to a different folder and then delete the old folders.

find / -iname "*.divx" -type f -print0 | xargs -0 -I '{}' mv "{}" /path/to/new_folder/

Or you can loop through all the files and copy them to new_folder only if they are not already present. If already present, delete other copies. Something like this:

for file in $(find . -iname "*.divx" -type f) do filename=$(basename ${file}) if [ ! -f ./movie/${filename} ]; then mv ${file} ./movie/${filename} else rm ${file} fi done

Recommend

  • Migrated project from SVN to Gitlab CE and struggling with workflow and history
  • Allow users to rate a comment once PHP MySQL
  • Convert pandas._period.Period type Column names to Lowercase
  • Janrain RPXnow (Engage) (https://rpxnow.com/) bypasses popup-blockers… how?
  • Adding a pivot to a rollup
  • AngularJS ngSanitize Error
  • How do I use a hyperlink in link variable?
  • cakephp 2 css, javascript and links not working on local machine/localhost
  • Setting Access-Control-Allow-Origin header in Angular2 development mode
  • Firefox addon(JPM) - How to pass message from panel's script to content script?
  • How should I organize my c# solution?
  • Get data file from microphone in windows phone 7
  • Robocopy - Compare modified dates of files?
  • Implementation of monitors with semaphores
  • Batch file to delete all folders in a directory except the newest folder
  • app_Offline.htm doesn't work for subfolders of my application in IIS
  • Xamarin iOS debugger not hitting breakpoints
  • Git cleanup/garbage collection on remote VSO git repository
  • How to separate filename from path? basename() versus preg_split() with array_pop()
  • Pyglet uses too much cpu
  • Open an application in a space using applescripts
  • Organizing large javascript files [closed]
  • Visual Studio - PathTooLongException even when the path length is less than 260 characters
  • What is this strange character in chrome's resource css viewer?
  • Is there any way to call saveCurrentTurnWithMatchData without sending a push notification?
  • Synchronize windows folders
  • Angular2 - Template reference inside NgSwitch
  • uml Composition relationships to RDF and OWL
  • Azure table store snapshot/backup capability
  • xtable package: Skipping some rows in the output
  • Paperclip, set path outside of rails root folder
  • How to rebase a series of branches?
  • Change JButton Shape while respecting Look And Feel
  • Proper folder structure for lots of source files
  • Load html files in TinyMce
  • Turn off referential integrity in Derby? is it possible?
  • Add sale price programmatically to product variations
  • Append folder name and increment by 1 using batch script
  • Unable to use reactive element in my shiny app
  • How do I use LINQ to get all the Items that have a particular SubItem?