bash 在bash中交换两个文件的最短方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1115904/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 18:19:26  来源:igfitidea点击:

Shortest way to swap two files in bash

linuxbashcommand-line

提问by flybywire

Can two files be swapped in bash?

可以在bash中交换两个文件吗?

Or, can they be swapped in a shorter way than this:

或者,它们可以以比这更短的方式交换:

cp old tmp
cp curr old
cp tmp curr
rm tmp

采纳答案by Hardy

Add this to your .bashrc:

将此添加到您的 .bashrc 中:

function swap()         
{
    local TMPFILE=tmp.$$
    mv "" $TMPFILE
    mv "" ""
    mv $TMPFILE ""
}

If you want to handle potential failure of intermediate mvoperations, check Can Bal's answer.

如果您想处理中间mv操作的潜在故障,请查看Can Bal 的回答

Please note that neither this, nor other answers provide an atomicsolution, because it's impossible to implement such using Linux syscalls and/or popular Linux filesystems. For Darwin kernel, check exchangedatasyscall.

请注意,这和其他答案都没有提供原子解决方案,因为使用 Linux 系统调用和/或流行的 Linux 文件系统不可能实现这样的解决方案。对于 Darwin 内核,请检查exchangedata系统调用。

回答by Can Bal

$ mv old tmp && mv curr old && mv tmp curr

is slightly more efficient!

效率稍微高一点!

Wrapped into reusable shell function:

包装成可重用的shell函数:

function swap()         
{
    local TMPFILE=tmp.$$
    mv "" $TMPFILE && mv "" "" && mv $TMPFILE ""
}

回答by cube

tmpfile=$(mktemp $(dirname "$file1")/XXXXXX)
mv "$file1" "$tmpfile"
mv "$file2" "$file1"
mv "$tmpfile" "$file2"

回答by ?ukasz Rysiak

do you actually want to swap them? i think its worth to mention that you can automatically backup overwritten file with mv:

你真的想交换它们吗?我认为值得一提的是,您可以使用 mv 自动备份被覆盖的文件:

mv new old -b

you'll get:

你会得到:

old and old~

if you'd like to have

如果你想拥有

old and old.old

you can use -S to change ~ to your custom suffix

您可以使用 -S 将 ~ 更改为您的自定义后缀

mv new old -b -S .old
ls
old old.old

using this approach you can actually swap them faster, using only 2 mv:

使用这种方法,您实际上可以更快地交换它们,仅使用 2 mv:

mv new old -b && mv old~ new

回答by Leslie Viljoen

Combining the best answers, I put this in my ~/.bashrc:

结合最好的答案,我把它放在我的 ~/.bashrc 中:

function swap()
{
  tmpfile=$(mktemp $(dirname "")/XXXXXX)
  mv "" "$tmpfile" && mv "" "" &&  mv "$tmpfile" ""
}

回答by Zyphrax

You could simply move them, instead of making a copy.

您可以简单地移动它们,而不是制作副本。

#!/bin/sh
# Created by Wojtek Jamrozy (www.wojtekrj.net)
mv  cop_
mv  
mv cop_ 

http://www.wojtekrj.net/2008/08/bash-script-to-swap-contents-of-files/

http://www.wojtekrj.net/2008/08/bash-script-to-swap-contents-of-files/

回答by Richard

This is what I use as a command on my system ($HOME/bin/swapfiles). I think it is relatively resilient to badness.

这是我在系统 ( $HOME/bin/swapfiles)上用作命令的内容。我认为它对坏事相对有弹性。

#!/bin/bash

if [ "$#" -ne 2 ]; then
  me=`basename 
function swap()
{
  if [ ! -z "" ] && [ -e "" ] && [ -e "" ] && ! [ "" -ef "" ] && (([ -f "" ] && [ -f "" ]) || ([ -d "" ] && [ -d "" ])) ; then
    tmp=$(mktemp -d $(dirname "")/XXXXXX)
    mv "" "$tmp" && mv "" "" &&  mv "$tmp"/"" ""
    rmdir "$tmp"
  else
    echo "Usage: swap file1 file2 or swap dir1 dir2"
  fi
}
` echo "Syntax: $me <FILE 1> <FILE 2>" exit -1 fi if [ ! -f ]; then echo "File '' does not exist!" fi if [ ! -f ]; then echo "File '' does not exist!" fi if [[ ! -f || ! -f ]]; then exit -1 fi tmpfile=$(mktemp $(dirname "")/XXXXXX) if [ ! -f $tmpfile ]; then echo "Could not create temporary intermediate file!" exit -1 fi # move files taking into account if mv fails mv "" "$tmpfile" && mv "" "" && mv "$tmpfile" ""

回答by cayhorstmann

A somewhat hardened version that works for both files and directories:

适用于文件和目录的稍微强化的版本:

function swp2file()
{   if [  !=  ] ; then
    local TMPFILE=tmp.$$
    mv "" $TMPFILE
    mv "" ""
    mv $TMPFILE ""
    else
    echo "swap requires 2 different filename"
    fi
}

This works on Linux. Not sure about OS X.

这适用于 Linux。不确定 OS X。

回答by tsoomo

Hardy's idea was good enough for me. So I've tried my following two files to swap "sendsms.properties", "sendsms.properties.swap". But once I called this function as same argument "sendsms.properties" then this file deleted. Avoiding to this kind of FAIL I added some line for me :-)

哈代的想法对我来说已经足够好了。所以我尝试了以下两个文件来交换“sendsms.properties”、“sendsms.properties.swap”。但是一旦我将这个函数称为相同的参数“sendsms.properties”,那么这个文件就被删除了。避免这种失败,我为我添加了一些行:-)

swap() {
    if (( $# == 2 )); then
        mv "" /tmp/
        mv "" "`dirname `"
        mv "/tmp/`basename `" "`dirname `"
    else
        echo "Usage: swap <file1> <file2>"
        return 1
    fi
}

Thanks again Hardy ;-)

再次感谢哈迪 ;-)

回答by adam_0

One problem I had when using any of the solutions provided here: your file names will get switched up.

我在使用此处提供的任何解决方案时遇到的一个问题:您的文件名将被切换。

I incorporated the use of basenameand dirnameto keep the file names intact*.

我合并了使用basenamedirname保持文件名完整*。

dir1/file2: this is file2
dir2/file1: this is file1

I've tested this in bash and zsh.

我已经在 bash 和 zsh 中测试过了。



*So to clarify how this is better:

*所以要澄清这如何更好:

If you start out with:

如果你开始:

dir1/file2: this is file1
dir2/file1: this is file2

The other solutionswould end up with:

其他的解决方案最终会得到:

dir1/file1: this is file1
dir2/file2: this is file2

The contents are swapped but the file names stayed. My solution makes it:

内容被交换,但文件名保持不变。我的解决方案是:

##代码##

The contents andnames are swapped.

内容名称互换。