Home Linux Commandline Tips How to Swap Contents of Two Files in Linux

How to Swap Contents of Two Files in Linux

The handy tools and commands provided by the Linux operating system make it possible to achieve/accomplish numerous file manipulation objectives. Under Linux file management, you might need to swap two files for one reason or another.

When we talk of swapping two files on a Linux operating system environment, it does not exactly translate to swapping/exchanging the location of these two files but their actual content.

To better understand the objective of this tutorial, consider the existence of the following files under a Linux operating system environment.

file_one.txt in /home/dnyce/Linuxshelltips/dir1/file_one.txt
file_two.txt in /home/dnyce/Linuxshelltips/dir2/file_two.txt

Let us assume that file_one.txt has the following content:

I am the first file. 

and file_two.txt has the following content:

I am the second file. 

If we are to successfully swap these two files,

file_one.txt will read - I am the second file.
file_two.txt will read - I am the first file.

We can use the head command to list the content of these two files before swapping.

$ head /home/dnyce/Linuxshelltips/dir1/file_one.txt
$ head /home/dnyce/Linuxshelltips/dir1/file_two.txt
List Contents of Files
List Contents of Files

We are now ready to look at a viable solution for swapping these two files:

Swap Contents of Two Files Using mv Command

As per its manual page, the mv command is effective in both renaming and moving files from one location to another.

In most programming languages, when we want to exchange two variables’ values, the most valid approach is to implement a temp variable. Take a look at the following demo:

temp = x
x = y
y = temp

The mv command version of the above two variables’ values exchange is shown below:

mv file_one.txt temp
mv file_two.txt file_one.txt
mv temp file_two.txt

From the above algorithmic implementation, file_one.txt is first moved to the /temp directory, then file_two.txt is renamed to file_one.txt, and file_one.txt inside the /temp directory is renamed to file_two.txt.

However, it is not always a good idea to store temp files in the /tmp directory as the directory (containing sensitive data) is accessible to any user hence a security risk.

To concurrently execute the above three mv commands, we have to concatenate them with the && operator. This operator makes sure that the execution of the preceding command is successful before the latter command is executed.

$ mv /home/dnyce/Linuxshelltips/dir1/file_one.txt /home/dnyce/Linuxshelltips/dir1/tmp.file_one && mv /home/dnyce/Linuxshelltips/dir2/file_two.txt /home/dnyce/Linuxshelltips/dir1/file_one.txt && mv /home/dnyce/Linuxshelltips/dir1/tmp.file_one /home/dnyce/Linuxshelltips/dir2/file_two.txt   

Let us confirm if the two were files successfully swapped:

$ head /home/dnyce/Linuxshelltips/dir1/file_one.txt
$ head /home/dnyce/Linuxshelltips/dir1/file_two.txt
Swap File Contents in Linux
Swap File Contents in Linux

As expected, file_one.txt has file_two.txt content and file_two.txt has file_one.txt content.

Know of other cool ways of swapping the content of two files? Feel free to leave a comment or feedback.

Ravi Saive
I am an Experienced GNU/Linux expert and a full-stack software developer with over a decade in the field of Linux and Open Source technologies. Founder of TecMint.com, LinuxShellTips.com, and Fossmint.com. Over 150+ million people visited my websites.

Each tutorial at UbuntuMint is created by a team of experienced writers so that it meets our high-quality standards.

Was this article helpful? Please add a comment to show your appreciation and support.

Got something to say? Join the discussion.

Thanks for choosing to leave a comment. Please keep in mind that all comments are moderated according to our comment policy, and your email address will NOT be published or shared. Please Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.