
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Swap Two Files in Linux Command Line
As system administrators or DevOps functions, many times we come with the requirement to exchange files content, for example, suppose you have a backup file of /etc/passwd by name /etc/password.backup and you want to restore the same in /etc/passwd but you also want current content of /etc/password should be copied to /etc/passwd.backup. In other words, exchange content of /etc/passwd with /etc/passwd.backup and /etc/passwd.backup with /etc/passwd.
The Linux operating system's useful tools and commands enable you to achieve/accomplish a wide range of file manipulation goals. For one reason or another, you may need to swap two files in the Linux file system.
When we talk about swapping two files on a Linux operating system, we don't mean swapping/exchanging the location of these two files, but rather their actual content. In this article, we will understand how to swap two files in Linux command line.
Consider the existence of the following files in a Linux operating system environment to better understand the goal of this tutorial.
Create two files f1.txt and f2.txt.
File f1.txt has the following contents
Hi Welcome in Linux
File f2.txt has the following contents
Hi Welocome in Linux shell scripting [sachin@lmdesk swapping]$ vim f1.txt [sachin@lmdesk swapping]$ vim f2.txt [sachin@lmdesk swapping]$ cat f1.txt Hi Welcome in Linux! [sachin@lmdesk swapping]$ cat f2.txt Hi Welcome in Linux shell scripting
If we successfully swap these two files
File f1.txt will read ? Hi Welocome in Linux shell scripting
File f2.txt will read ? Hi Welcome in Linux
Swapping contents of two files by using mv command
The mv command can be used to rename files and can also be used to move them from one location to another. Here the purpose is to swap two files in the command line, so we using mv command for renaming purposes, not for moving files to other locations.
Exchanging values between variables is a common requirement in programming and scripting jobs. When we want to exchange the values of two variables, in most programming languages, the best approach is to use a temp variable. Examine the following demonstration
temp = x x = y y = temp
Lets implement this algorithm with mv command, mv command will be modified like this
mv f1.txt test.txt mv f2.txt f1.txt mv test.txt f2.txt
The output of these command will swap two files.
Swapping contents of two files by using shell script
if (( $# == 2)) ; then TMPFILE=$(mktemp $(dirname "$1")/function.txt) mv "$1" $TMPFILE && mv "$2" "$1" && mv $TMPFILE "$2" else echo "Error: Two valid file paths required" return 1 fi
Let us understand above shell script. $# is the number of arguments to shell script, so first line of script with if statement validating if the argument to shell script is 2 then and only then exchange contents. TMPFILE is a variable holding path of file which hold content temporarily in the process of content exchange, mv command we discussed earlier and here we using same for renaming purpose. If two files are not passed to this script, then error will get generated
We can simply pass the two files to swap as arguments. The function will handle everything else. Bash's auto-completion will come in handy here.
Conclusion
In this article, we learned how to swap the contents of two files using three mv commands. We've also written a shell script to make it easier to carry out this operation. You can either use the command execution approach or can use shell script approach.