Home > Blockchain >  Swapping the contents of two files on Linux
Swapping the contents of two files on Linux

Time:12-16

I have two different files used for configuring a certain program I run. They are the same except for a few lines. The easiest way I found to do this was with a simple swap using three copy, cp, commands. Is there a better way to do this? I am running in a CentOS system.

As stated above, this is the script I came up with (file names would be more descriptive in use, but removed for privacy).

cp file1.txt temp.txt
cp file2.txt file1.txt
cp temp.txt file2.txt
rm temp.txt

CodePudding user response:

I believe (no proof here) that using mv would be more efficient, as you would just change the name of the files, instead of copying them.

mv file1.txt temp.txt
mv file2.txt file1.txt
mv temp.txt file2.txt

And that's one command less than yours.

  • Related