Skip to content

Instantly share code, notes, and snippets.

@jmcerrejon
Created March 26, 2023 17:26
Show Gist options
  • Select an option

  • Save jmcerrejon/bb0f8b1884c7b431460d2c1b564f6200 to your computer and use it in GitHub Desktop.

Select an option

Save jmcerrejon/bb0f8b1884c7b431460d2c1b564f6200 to your computer and use it in GitHub Desktop.

Revisions

  1. jmcerrejon created this gist Mar 26, 2023.
    26 changes: 26 additions & 0 deletions clean_volume.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,26 @@
    import subprocess

    def main():
    volumes = subprocess.run(['mount'], stdout=subprocess.PIPE).stdout.decode('utf-8').split('\n')

    for volume in volumes:
    if '/Volumes/' in volume and 'nodev' in volume:
    volume_name = volume.split()[2]
    clean_volume(f"{volume_name}/pelis" if os.path.isdir(f"{volume_name}/pelis") else f"{volume_name}")

    unmount_volume(volume_name)

    def clean_volume(volume_name):
    print(f"\nFound: {volume_name}. Cleaning...")
    subprocess.run(["rm", "-rf", f"{volume_name}/.Trashes"])
    subprocess.run(["rm", "-rf", f"{volume_name}/.fseventsd"])
    subprocess.run(["rm", "-rf", f"{volume_name}/.Spotlight-V100"])
    subprocess.run([f"find", volume_name, "-type", "f", "-name", "._*", "-depth", "-delete"])
    subprocess.run([f"find", volume_name, "-type", "f", "-name", ".DS*", "-depth", "-delete"])

    def unmount_volume(volume_name):
    print(f"\nUnmounting: {volume_name}")
    subprocess.run(["diskutil", "unmountDisk", f"{volume_name}"])

    if __name__ == "__main__":
    main()