Let’s say you’ve copied a giant chunk of data from a source to destination something like this:
sudo cp From_SOURCE/* To_DESTINATION/
Now you wish to undo this command. Phew!!!
Solution:
This can be achieved using a python script for nerds like us,
The script
#!/usr/bin/env python
import os
source_dir = "/path/to/source" # the folder you copied the files from
target_folder = "/path/to/destination" # the folder you copied the files to
for root, dirs, files in os.walk(source_dir):
for name in files:
try:
os.remove(target_folder+"/"+name)
except FileNotFoundError:
pass
How to use this thing?
- Paste the script in an empty file, save it as
reverse.py
, - Insert the correct paths for source and target folder,
- Make it executable for convenience reasons,
- Run it by the command:
[sudo] /path/to/reverse.py
Warning
First try on a test directory, I’m guessing the damage might be negligible but on your risk again!
Second thing to note:-
In case the source directory has no sub-directories, the script can even be simpler:
#!/usr/bin/env python
import os
source_dir = "/path/to/source" # the folder you copied the files from
target_folder = "/path/to/destination" # the folder you copied the files to
for file in os.listdir(source_dir):
try:
os.remove(target_folder+"/"+file)
except FileNotFoundError:
pass
TL;DR:
All files that are present in both src
and dest
can be removed from dest
like this:
find . -maxdepth 1 -type f -exec cmp -s '{}' "$destdir/{}" \; -exec mv -n "$destdir/{}" "$toDelete"/ \;