Re: command for swapping of two files or two dirs?
From:
Greg Wooledge
Subject:
Re: command for swapping of two files or two dirs?
Date:
Fri, 26 Mar 2021 16:58:23 -0400
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.