Skip to content

Instantly share code, notes, and snippets.

@mekb-turtle
Last active April 28, 2023 08:55

Revisions

  1. mekb-turtle revised this gist Apr 28, 2023. 1 changed file with 8 additions and 3 deletions.
    11 changes: 8 additions & 3 deletions swapfile
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,11 @@
    #!/usr/bin/bash
    if [[ "$#" != "2" ]]; then echo "Usage: swapfile <file1> <file2>"; exit 1; fi
    if [[ ! -e "$1" ]]; then echo "File not found: $1"; exit 1; fi
    if [[ ! -e "$2" ]]; then echo "File not found: $2"; 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 "$1" "$1$IDTEMP" && mv -v "$2" "$1" && mv -v "$1$IDTEMP" "$2"
    mv -v "$f1" "$f1$IDTEMP" && mv -v "$f2" "$f1" && mv -v "$f1$IDTEMP" "$f2"
  2. mekb-turtle created this gist Apr 28, 2023.
    6 changes: 6 additions & 0 deletions swapfile
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,6 @@
    #!/usr/bin/bash
    if [[ "$#" != "2" ]]; then echo "Usage: swapfile <file1> <file2>"; exit 1; fi
    if [[ ! -e "$1" ]]; then echo "File not found: $1"; exit 1; fi
    if [[ ! -e "$2" ]]; then echo "File not found: $2"; exit 1; fi
    IDTEMP="$(uuidgen|tr -d '-')"
    mv -v "$1" "$1$IDTEMP" && mv -v "$2" "$1" && mv -v "$1$IDTEMP" "$2"