Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to swap two files in Linux command line?
As system administrators or DevOps professionals, we often need to exchange file contents. For example, you might have a backup file /etc/passwd.backup that you want to restore to /etc/passwd, while preserving the current content by moving it to the backup file. This operation is called file swapping exchanging the actual content of two files, not their locations.
When we talk about swapping two files on Linux, we mean exchanging their content while keeping their names and locations unchanged. This tutorial demonstrates practical methods to accomplish this task using command line tools.
Example Files Setup
Let's create two sample files to demonstrate the swapping process:
$ echo "Hi Welcome in Linux!" > f1.txt $ echo "Hi Welcome in Linux shell scripting" > f2.txt $ cat f1.txt Hi Welcome in Linux! $ cat f2.txt Hi Welcome in Linux shell scripting
Goal: After swapping, f1.txt should contain "Hi Welcome in Linux shell scripting" and f2.txt should contain "Hi Welcome in Linux!"
Method 1: Using mv Command with Temporary File
The mv command can rename and move files. We'll use it for renaming to implement the classic three-step swap algorithm using a temporary file:
# Step 1: Move first file to temporary name mv f1.txt temp.txt # Step 2: Move second file to first file's name mv f2.txt f1.txt # Step 3: Move temporary file to second file's name mv temp.txt f2.txt
This approach follows the standard variable swapping pattern:
temp = x x = y y = temp
Verification
$ cat f1.txt Hi Welcome in Linux shell scripting $ cat f2.txt Hi Welcome in Linux!
Method 2: Shell Script for Automated Swapping
Create a reusable script to automate the swapping process:
#!/bin/bash if (( $# == 2 )); then # Create temporary file in same directory as first file TMPFILE=$(mktemp $(dirname "$1")/swap_temp.XXXXXX) # Perform three-step swap mv "$1" "$TMPFILE" && mv "$2" "$1" && mv "$TMPFILE" "$2" echo "Successfully swapped contents of $1 and $2" else echo "Error: Two valid file paths required" echo "Usage: $0 file1 file2" exit 1 fi
Script Explanation
$#Number of arguments passed to the scriptmktempCreates a unique temporary file in the same directory&&Ensures eachmvcommand succeeds before proceedingError handling for incorrect usage
Usage
$ chmod +x swap_files.sh $ ./swap_files.sh f1.txt f2.txt Successfully swapped contents of f1.txt and f2.txt
Method 3: One-liner with mktemp
For quick swaps, you can use this one-liner command:
$ TEMP=$(mktemp) && mv file1 "$TEMP" && mv file2 file1 && mv "$TEMP" file2
Important Considerations
File permissions Ensure you have write permissions for both files and their directory
Atomic operations The
&&operator ensures all steps complete successfullyTemporary files
mktempcreates unique temporary files to avoid conflictsSame filesystem
mvworks fastest when files are on the same filesystem
Conclusion
Swapping file contents in Linux can be accomplished using the mv command with a temporary file approach. The shell script method provides automation and error handling, making it suitable for frequent use. Both methods preserve file permissions and efficiently exchange content without data loss.
