help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: command for swapping of two files or two dirs?


From: Reuti
Subject: Re: command for swapping of two files or two dirs?
Date: Fri, 26 Mar 2021 22:32:47 +0100

> Am 26.03.2021 um 22:21 schrieb Peng Yu <pengyu.ut@gmail.com>:
> 
> The problem is that the swap based on multiple commands is not atomic.
> With multiple commands, wouldn't it cause complications for error
> recovery? Also, is it safe? If there is any malicious program delete
> some files during the process?

`mmv` can handle multiple renames (without overwriting each others file), but 
with multiple file pairs it won't handle filenames which space characters or 
alike in a proper way (as it reads the filename pairs directly from stdin).

$ function swap() { mmv <<<"$1 $2"$'\n'"$2 $1"; }

It would be a good addition to `mmv` to handle an atomic swap operation too.

-- Reuti


> 
> On 3/26/21, Alex fxmbsw7 Ratchev <fxmbsw7@gmail.com> wrote:
>> i agree to all excepts the srandom thingy
>> a [[ -e loop passthru maybe.. like always
>> 
>> On Fri, Mar 26, 2021 at 9:58 PM Greg Wooledge <greg@wooledge.org> wrote:
>>> 
>>> On Fri, Mar 26, 2021 at 09:18:10PM +0100, Alex fxmbsw7 Ratchev wrote:
>>>> swap() { declare t= tt= ; [[ $# != 2 ]] && return 1
>>>> mv "$1" "${t:=/tmp/$SRANDOM}"
>>>> mv "$2" "${tt:=/tmp/$SRANDOM}"
>>>> mv "$t" "$2"
>>>> mv "$tt" "$1"
>>>> }
>>> 
>>> 1) Use mktemp(1) or tempfile(1) or whatever.  Your $SRANDOM hack has
>>>   security problems.
>>> 
>>> 2) Don't move the file to /tmp.  That might be on a different file
>>> system.
>>>   You want to keep the file within its current directory, as that's the
>>>   only place you can be sure isn't on another file system.
>>> 
>>> 3) There should only need to be three moves.
>>> 
>>> 4) You probably want && between the moves, to abort if something goes
>>>   wrong.
>>> 
>> 
>> 
> 
> 
> -- 
> Regards,
> Peng
> 

--
Philipps-University of Marburg
AG Berger / AG Frenking / FB Chemie
Reuti
Admin
Hans-Meerwein-Straße 4
35032 Marburg (35043 for the delivery of goods)
Germany
FON +49-6421-28-25549
FAX +49-6421-28-21826
eMail reuti@staff.uni-marburg.de




reply via email to

[Prev in Thread] Current Thread [Next in Thread]