Skip to content

Instantly share code, notes, and snippets.

@epoz
Last active October 17, 2019 11:37
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save epoz/c5b95a3e17e04d7d039d1a418da3b4b1 to your computer and use it in GitHub Desktop.
Save epoz/c5b95a3e17e04d7d039d1a418da3b4b1 to your computer and use it in GitHub Desktop.
HardLink all files into single directory
#!/usr/bin/env python
u = """Link all files from a directory and its descendants into a specified destination directory.
This 'flattens' the source dir into the destination dir
Usage: %s source_dir destination_dir
"""
import os
import sys
from progress.bar import Bar
if __name__ == '__main__':
if len(sys.argv) < 3:
print(u % __file__)
sys.exit(1)
if not (os.path.isdir(sys.argv[2]) and os.path.isdir(sys.argv[1])):
print('Both src and dest need to be directories')
print(u % __file__)
sys.exit(2)
files = [os.path.join(dirpath, filename) for dirpath, dirnames, filenames in os.walk(sys.argv[1]) for filename in filenames]
bar = Bar('Making links', max=len(files))
for filepath in files:
filename = os.path.split(filepath)[-1]
dst = os.path.join(sys.argv[2], filename)
if not os.path.exists(dst):
os.link(filepath, dst)
bar.next()
bar.finish()
print(len(files))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment