Linux: Find files matching a search and move them retaining directory structure

I recently had an issue with Google Drive which resulted in a large number of duplicate files and folders being created. On Google Drive, confusingly, these folders actually have the same name, but in my local directory structure (synced using InSync HQ) the folders and files are properly renamed to “name (2)”.

Now tracking all of these down via a GUI file explorer was just going to be painful. I could probably utilise some package for duplicate file checking but I wanted to play around with my command line skills a bit and came up with the following solution for backing up these files that had “(2)” appended, and then deleting them.

find . -type f -name "*(2)*" -exec cp --parents {} ~/GdriveDuplicateBackup  \;
find . -type f -name "*(2)*" -exec rm {} \;

Explanation

  1. find .
    Command to find the files in the current dir and any sub-dirs
  2. -type f
    Only find items that are files. If you want to run this on directories use “-type d”.
  3. -name “*(2)*”
    Find files with a name that contains “(2)”, the asterisks indicate wildcards.
  4. -exec cp –parents {} ~/GdriveDuplicateBackup  \;
    Execute the following command on each result. In this case I’m executing the copy command. The –parents flag instructs copy to retain the files’ folder structure.

“{}” indicates where the current result of the find command is being placed in our exec flag. So the actual command executed on each find result might look like this:

cp --parents ~/gdrive/folder/file.txt ~/GdriveDuplicateBackup \;

After that’s complete I run the same ‘find’ command but instead exec an rm command on each find result.

I hope that makes sense – if you have any questions please don’t hesitate to leave a comment below.

Comments

  • Donncha

    Nice. I used a complicated loop to mkdir -p, dirname, etc a list of files in the past when copying video files out of my photos directory into my videos directory for Plex to find but this is so much easier 🙂

    • Cool, glad this was useful. I’m still in noob land with Linux but really enjoying daily use and it’s making me much more comfortable when I SSH into our server 🙂