Created
February 4, 2020 01:54
-
-
Save innateessence/a30764ec2b46ddb4bada06d5e128218a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import os | |
import sys | |
def filter_broken_symlinks(files): | |
broken_symlinks = [] | |
for file in files: | |
file = os.path.abspath(file) | |
if os.path.islink(file) and os.path.exists(os.readlink(file)) is False: | |
broken_symlinks.append(file) | |
return broken_symlinks | |
def get_broken_symlinks(path): | |
broken_symlinks = [] | |
for folders, subfolders, files in os.walk(path): | |
broken_symlinks += filter_broken_symlinks(folders) | |
broken_symlinks += filter_broken_symlinks(subfolders) | |
broken_symlinks += filter_broken_symlinks(files) | |
return list(set(broken_symlinks)) | |
def main(path): | |
broken_symlinks = get_broken_symlinks(path) | |
for broken_symlink in broken_symlinks: | |
print(f"{broken_symlink} -> {os.path.abspath(os.readlink(broken_symlink))}") | |
if __name__ == '__main__': | |
main(sys.argv[-1]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment