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).The script
How to use
reverse.py
,Run it by the command:
Warning
First try on a test directory if I understood well what you need to achieve!
If the sourcedirectory is "flat"
In case the source directory has no sub-directories, the script can even be simpler:
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.
TL;DR:
All files that are present in both
src
anddest
can be removed fromdest
like this:For a step-by-step, explanation, see below.
Simplifying the problem:
To understand what the command we want to undo actually did, we start by simpifying it:
The command we want to undo is
For understanding how to undo,
sudo
is not relevant.I'll use the directory names
src
forFrom_SOURCE
anddest
forTo_DESTINATION
.Now, our command is:
If
src
contains the filesf1
,f2
andf3
, and the directoriesd1
andd2
, the shell expands that command to:Without options like
-r
,-R
or-a
, the commandcp
does not copy directories.That means, the command will leave them out, showing an error for each of them:
That means, we have only copied simple files, and no directories, to
dest
.Deciding which files to remove:
Possibly there were files in
dest
of the same name as files insrc
. In this case, the files were overwritten (1). It's too late for them, sorry. Get them back from the latest backup.Regarding the files that are there, we want to remove only files that have been copied over. These files exist in both directories, with the same name, and the same content.
So we look for these files:
First, we
cd
intosrc
, because it makes the followingfind
commands much simpler, and set a variable to the absolute path of dest:Then, we list all files in src:
and, for each file found, use
cmp
to check whether there is a file with the same content in dest:Removing the files, carefully:
These files are the ones we want to remove. But to be sure, we move them into a different directory first - and take a look at the commands before running them:
Looks good! Now we can leave out the
echo
to run the realmv
commands:All the files from
dest
that were copied fromsrc
, and still actually the same insrc
anddest
, are now in/tmp/toDelete/
, and can be removed after taking a last look.Notes:
(1) Check whether
cp
is an alias tocp -i
or so, that would have prevented overwriting files by asking first.This is old, but I just wanted to post a pure bash answer:
First change to the directory where you copied the files.
cd dest
Then,
ls
the source directory and pipe the output intorm
ls source | xargs rm