How can I copy all hidden files/directories in the home directory into a separate directory from command line ? I wasn't able to find a lot of material concerning usage of hidden files .
What needs to be done
In principle not a complicated question, however, you need to take care of a few things:
- You need (as I understand) to copy hidden files/directories recursively
- You will run into name clashes (duplicate names, I tried)
The (python) script below takes care of both:
- It searches for files and directories recursively in given (source-) directory
- it copies found files and directories into a given (target) directory
- In case of duplicates, it renames them to
duplicate_2<filename>(or directory-) etc.
#!/usr/bin/env python3 import shutil import sys import os directory = sys.argv target = sys.argv def handle(name): n = 1; name_orig = name while os.path.exists(target+"/"+name): name = "duplicate_"+str(n)+"_"+name_orig n = n+1 return target+"/"+name for root, dirs, files in os.walk(directory): for name in files: if name.startswith("."): subject = root+"/"+name newfile = handle(name) try: shutil.copyfile(subject, newfile) except: pass for dr in dirs: if dr.startswith("."): subject = root+"/"+dr newdir = handle(dr) try: shutil.copytree(subject, newdir) except: pass
How to use
Copy the script above into an empty file, save it as
search_hidden.py. Run it with the source- and target directories as arguments:
python3 /path/to/search_hidden.py <source_directory> <target_directory>