move folders with _deleteme to a new location

jimleslie77

New Member
Credits
0
we have a directory structure (for home drives), there is an automated task that renames folders with _deleteme when it hasnt been accessed for while, we currently run through this tree periodically and delete folder with _delete me at the end. I would like to script looking through the folder tree (can be 5 or 6 sub folders here) recursivley looking for any folder with _deleteme at the end and move them (not delete) to a new folder location.
 


JasKinasis

Well-Known Member
Credits
566
You can do it in a single line:
Bash:
find /path/to/search/ -type d -iname “*_deleteme” -exec mv {} /path/to/destination/ \;
Note: I typed the above on my phone, I think those are the wrong double quotation character’s. Just FYI!

That will search the specified search path for directories with _deleteme at the end of the name and move them to the specified directory.

Where /path/to/search/ is the root directory we’re searching from. And /path/to/destination/ is the destination directory.

The only minor problem would be if the destination directory is inside the search directory, you’d get a load of redundant move operations on directories already in the destination directory. In which case, you can filter out results from that particular directory.... But offhand I can’t remember the correct option/switch to use for that, I’ll get back to you on that once I’m on my pc!
 
Last edited:

JasKinasis

Well-Known Member
Credits
566
Update:
If your backup/destination directory is inside your search directory, you can skip any results that are already in there using:
Bash:
find /path/to/search/ -type d -iname “*_deleteme” ! -path /path/to/destination/* -exec mv {} /path/to/destination/ \;
Again - where /path/to/search/ is the start directory for the search and /path/to/destination/ is your backup directory...
 
  • Like
Reactions: Rob


Members online


Top