Last active
April 28, 2023 08:55
-
-
Save mekb-turtle/4635c23aa16ed3bcd019b987fcd9c287 to your computer and use it in GitHub Desktop.
bash script to swap two files around
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/bash | |
if [[ "$#" != "2" ]]; then echo "Usage: swapfile <file1> <file2>"; exit 1; fi | |
f1="$1" | |
f2="$2" | |
if [[ ! -e "$f1" ]]; then echo "File not found: $f1"; exit 1; fi | |
if [[ ! -e "$f2" ]]; then echo "File not found: $f2"; exit 1; fi | |
while [[ "$f1" == */ ]]; do f1="${f1%\/}"; done | |
while [[ "$f2" == */ ]]; do f2="${f2%\/}"; done | |
echo "Swapping $f1 and $f2" | |
IDTEMP="$(uuidgen|tr -d '-')" | |
mv -v "$f1" "$f1$IDTEMP" && mv -v "$f2" "$f1" && mv -v "$f1$IDTEMP" "$f2" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment