Skip to content

Instantly share code, notes, and snippets.

@jmcerrejon
Created March 26, 2023 17:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jmcerrejon/bb0f8b1884c7b431460d2c1b564f6200 to your computer and use it in GitHub Desktop.
Save jmcerrejon/bb0f8b1884c7b431460d2c1b564f6200 to your computer and use it in GitHub Desktop.
Clean macOS volumes for non mac OS
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()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment