[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Deleting files
From: |
Jean Louis |
Subject: |
Re: Deleting files |
Date: |
Mon, 22 Feb 2021 08:19:32 +0300 |
User-agent: |
Mutt/2.0 (3d08634) (2020-11-07) |
* michael-franzese@gmx.com <michael-franzese@gmx.com> [2021-02-22 02:02]:
> I have two directories with files, one is the "source", the other
> "destination". I would like to have a script that checks file
> in "source". If file name and size matches that at "destination",
> the file at "source" is deleted. Preferably using bash.
By using Emacs
==============
In Emacs that is already solved by using the function as below:
M-x dired-compare-directories
Process would be as following:
0. I advise to set the variable `dired-dwim-target' to TRUE, as "dwim"
means "Do What I Mean". This variable will help Dired file
operations to automatically offer the other window's directory.
1. Open two (or more) Emacs windows, one directory in one window,
other directory in other window.
2. M-x dired-compare-directories
3. It will ask you for Lisp expression, you may then say something
like: (not (= size1 size2)) in the prompt. That will compare
directories by sizes. Those files with different sizes will be
marked in both windows, you will chose from which window and
directory to transfer files to other window.
4. Use `C' in Dired to copy or `R' to rename files to other window and
directory.
It is possible to make it a script with Emacs, as Emacs may run in
batch mode. You would need to first change to specific directory by
using the `source' argument, and then supply `DIR2' variable by using
the `target' argument of a script, and I just guess without checking
that predicate would be the Lisp function as explained.
(dired-compare-directories DIR2 PREDICATE)
More about that may be found in the manual by evaluating this Lisp
expression:
(info "(emacs) Misc Dired Features")
By using Midnight Commander
===========================
Without Emacs, I know that this function exists in software Midnight
Commander, the command being `mc' and key binding C-x d, so by
invoking the function in Midnight Commander you also get it.
Such function depends on probability that the outcome will be as
wanted.
As what if file contains a phone number, but newer file contains newer
and current phone number. That makes both files of the same size and
older file would not get overwritten by the new file. Files may be
changed and still retain the same file size. Probability is law but is
still there. Data may be loss when relying on such function.
For Bash
========
Thus when using the Bash script, I would make sure that each file is
checked for its hash before deciding if to move it and later delete
it.
By using Rsync
==============
Rsync command offers the same functionality automatically. Thus
something like this:
$ rsync --remove-source-files -av /source/directory/ /target/directory/
would make sure that source files are deleted after synchronizing
those files from source directory.
In my opinion rsync would be most appropriate for you and I suggest
that you review its options and manual.
Jean