Shell Gems

Here are a few useful shell commands, gathered from all around the Internet and my own files.

User rights escalation

It is quite common to forget to open a file or launch a command without the right permissions:

  • Run the previous command as root

This can easily be done thanks to:

$ sudo !!
  • Save a file opened in VIM without the right permissions

This can easily be done thanks to:

:w !sudo tee %

Command execution and history tricks

  • Re-run the previous command correcting a typo

This can easily be done thanks to:

$ ^typo^correction^
  • Run the previous command replacing all occurences of foo with bar

This can easily be done thanks to:

$ !!:gs/foo/bar
  • Check command history (without running it):

This can easily be done thanks to

$ !something:p
  • Invoke $EDITOR to finish editing a long command

Type Ctrl-x e when writing a long shell command.

$ longcomma^x e

File manipulation

  • Empty a file

This can easily be done thanks to:

$ >file
  • Monitor file updates

This can easily be done thanks to:

$ watch --differences -n5 'df; ls -FlAt;'