Giter VIP home page Giter VIP logo

twiddle's Introduction

twiddle

A shell script to swap the contents of two files.

Usage

Sometimes you accidentally create two files whose contents you need to exchange:

$ echo "Hello!" > ko; echo "안녕하세요!" > en
$ cat en ko
안녕하세요!
Hello!

Oops! en should have the English text, and ko the Korean text. Using twiddle, we can fix this:

$ twiddle en ko
$ cat en ko
Hello!
안녕하세요!

Why?

A common mistake in this situation is to run mv en ko; mv ko en. This overwrites the contents of ko, permanently deleting the data Hello! and causing an error in the second command.

The correct solution is to use a temporary file: mv en tmp; mv ko en; mv tmp ko. twiddle is a failsafe shorthand for this sequence of steps. It checks that both files exist, creates a temporary file safely (using mktemp) in the /tmp/ directory to guard against overwriting files and certain kinds of snooping attacks, and then performs the exchange operation.

Installation

Put the contents of twiddle.sh into a file twiddle somewhere on your path. You could use the following two commands:

$ sudo curl "https://raw.githubusercontent.com/maxkapur/twiddle/main/twiddle.sh" -o /usr/local/bin/twiddle
$ sudo chmod +x /usr/local/bin/twiddle

The first command copies the contents of twiddle.sh to the file /usr/local/bin/twiddle. The second command grants permission to execute the /usr/local/bin/twiddle as a program.

Contributing

Contributions and bug reports are welcome. I have only tested this script on Ubuntu 22.04 and Ubuntu Server 22.04.

twiddle's People

Contributors

maxkapur avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.