Re: Piping commands to a shell but keeping interactivity

Liste des GroupesRevenir à col misc 
Sujet : Re: Piping commands to a shell but keeping interactivity
De : james.harris.1 (at) *nospam* gmail.com (James Harris)
Groupes : comp.os.linux.misc
Date : 07. Mar 2024, 17:24:29
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <uscpnt$14tr1$1@dont-email.me>
References : 1 2 3 4 5
User-Agent : Mozilla Thunderbird
On 28/02/2024 01:07, John-Paul Stewart wrote:
On 2024-02-27 3:46 a.m., Richard Kettlewell wrote:
Rich <rich@example.invalid> writes:
John-Paul Stewart <jpstewart@personalprojects.net> wrote:
On 2024-02-26 3:50 a.m., James Harris wrote:
It can be very useful to pipe commands into a shell such as with
>
   ls sample* | sed 's/^/rm -v /' | sh
>
Can I ask why you pipe to sed and a shell instead of simply using xargs:
>
   ls sample* | xargs rm -vi
>
That seems to be much a simpler solution.
>
Even better, this version which won't choke on spaces or other odd
characters in filenames:
>
find -name sample\* -maxdepth 1 -print0 | xargs -0 rm -vi
>
That handles directories better too. But it doesn’t work, due to the
stdin issue that the OP was originally asking about.
>
The working answer is much simpler:
>
   rm -vi sample*
>
The ‘ls’ versions will also behave quite badly in the face of
directories matching the pattern.
 Well, the OP did say that 'ls' was just an example.  In reality it is
some other code that generates the list of names.
That's right. What this is for is code to list files in a folder which are duplicates of those in another folder (same name, same relative place in the folder hierarchy, etc). For example, say there are two folders, c and d, and one is potentially a copy of the other. The command
$ ./lsdup.py -r c d
lists files in d (and subdirectories due to the -r recurse option) which are duplicates of those in c.
In answer to your other point about using xargs, I would use it if it would do what's required, and do so consistently, but I am not sure whether I can trust it or not. Here's a full example.
$ ./lsdup.py c d -r | sed 's/^/rm -v /'
which generates commands such as
rm -v 'd/contentsame.txt'
rm -v 'd/hardlink.txt'
rm -v 'd/subdir/filesame.txt'
Once the commands have been checked, if required, I pipe them into sh to actually delete the files. (A separate command, lsempty.py, is is used to delete the resultant empty folders.)
In answer to the point about filenames with spaces and odd characters, I currently output names in single quotes, as above.
--
James Harris

Date Sujet#  Auteur
7 Mar 24 * Re: Piping commands to a shell but keeping interactivity5James Harris
8 Mar 24 +- Re: Piping commands to a shell but keeping interactivity1Lawrence D'Oliveiro
8 Mar 24 `* Re: Piping commands to a shell but keeping interactivity3Rich
9 Mar 24  `* Re: Piping commands to a shell but keeping interactivity2James Harris
10 Mar 24   `- Re: Piping commands to a shell but keeping interactivity1Rich

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal