Skip to content

Instantly share code, notes, and snippets.

@adrienjoly
Created December 5, 2015 08:05
Show Gist options
  • Select an option

  • Save adrienjoly/34d9c291141577539e40 to your computer and use it in GitHub Desktop.

Select an option

Save adrienjoly/34d9c291141577539e40 to your computer and use it in GitHub Desktop.
This shell helps you get rid of duplicate "(1)" files created by Google Drive.
# This shell script was written to get rid of duplicate files created by Google Drive.
# On the web UI, duplicates have the same name. On my hard drive, one has a "(1)" suffix.
# This script looks for files that have a (1)-suffixed duplicate, based solely on their filename.
# In the end, it successfully help me delete those duplicates from my hard-drive, but the
# Google Drive software (for Mac) did not delete the corresponding files from their back-end...
# So I ended up using this: http://drivecleaner.softgateon.net/
# But I'm sharing the script because it may be useful for other people, for other reasons.
# 0) back-up your file listing and disk usage of subdirectories
ls -lR >backup-listing.txt
du >backup-du.txt
# 1) find files that contain "(1)"
find . -name '*(1)*' > duplicates.txt
# 2) find files that contain "(1)" but that are actually not a duplicate
sed 's/ (1)//g' duplicates.txt >duplicates_renamed.txt # duplicate listing without "(1)" in their file name
find . \! -name '*(1)*' >others.txt # all files without "(1)" in their file name
comm -12 duplicates_renamed.txt others.txt >common_files.txt # files that exist both with and without "(1)"
diff duplicates_renamed.txt common_files.txt >non-duplicates.txt # files that only exist with a "(1)"
# 3) remove every file from duplicates.txt, minus files from non-duplicates.txt
# (you must do that manually)
# => store the final list of duplicates.txt into todelete.txt
# 4) move files to delete to the __duplicates directory
# mv `cat todelete.txt` __duplicates/
mkdir __duplicates
while read -r line
do
cp "$line" __duplicates/
done <todelete.txt
# 5) after checking its contents, you can delete the __duplicates directory.
# 6) then, compare your backups (see #0) with actual file listing and disk usage
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment