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
- find .
Command to find the files in the current dir and any sub-dirs - -type f
Only find items that are files. If you want to run this on directories use “-type d”. - -name “*(2)*”
Find files with a name that contains “(2)”, the asterisks indicate wildcards. - -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