I copied some file from source folder into destination folder by using bellow command line in terminal.
sudo cp From_SOURCE/* To_DESTINATION/
Now I want to undo this command.
I copied some file from source folder into destination folder by using bellow command line in terminal.
sudo cp From_SOURCE/* To_DESTINATION/
Now I want to undo this command.
If I understand well, the following is the case:
The script below looks in the original (source) directory and lists those files. Then it looks into the directory you copied the files to, and removes only the listed files, as they exist in the source directory.
The try
element is added to prevent errors, for example in case you might have removed some files manually already, or if not all files from the source directory were copied to the destination. If you need sudo privileges, simply run the script with "sudo" (see below).
#!/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
reverse.py
,Run it by the command:
[sudo] /path/to/reverse.py
First try on a test directory if I understood well what you need to achieve!
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
Note
If the copy action overwrote (replaced) a similarly named file in the destination, the file will be removed, but the original file will (of course) not be brought back by the script. The assumption is that there are no name clashes.