Wikimedia Discovery/Command Line Tricks

This is a collection of useful *nix command line tricks and productivity tips that not everyone knows about that could improve your command-line fu.

Commands
Each command here has a brief description, a use case or two, and an example or three if needed.

pushd, popd, pushd

 * description: controls a stack of directories.  moves you to and pushes onto a stack.   pops the stack and moves you back to your previous directory.   (with no argument) swaps the top two items on the stack.
 * use case: jumping back and forth between two directories (config in, running stuff in ), changing directories one or more times and then changing back without having to keep track of the list of directories (e.g., in a bash script)
 * example:

` `

 * description: convert the output of a command into command line arguments
 * example:
 * create a file with today's date as part of the filename using  to get today's date
 * use the output of the previous command (say, something complicated involving ) as command line arguments for
 * open all files in this directory tree that contain  but do not have   in their name.

!!, !<#>, !

 * description:  repeats the last command, possibly in a new context;   repeats the command two steps back in your history.   is the same as  .   repeats command 445 from the output of  .   will re-run your most recent   command.
 * use case: repeat a command but add sudo
 * example:
 * repeat previous command with sudo
 * use the output of the previous command (say, something complicated involving ) as command line arguments for
 * repeat the previous command twice (do it several times and you will repeat the command 4 times, 8 times, 16 times, etc.)
 * See also: https://jaysoo.ca/2009/09/16/unix-history-and-bang-commands/

bash scripting

 * description: shell scripts can be really powerful. Simple loops are super useful...
 * example:
 * copy every .cfg file into directory bk/
 * copy every .cfg file to a .cfg.bk backup in the same directory
 * see http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO-7.html

grep -R

 * description: recursive, searches all files in this directory and every directory below it. Not as powerful as   but easier to remember
 * example:  open all files in this directory tree that contain   but do not have   in their name.


 * description:
 * use case:
 * example:

Config
These are cool things you can add to your .bashrc or .bash_profile to make your command line more efficient

alias
lets you rename a command (including command line options and piping commands together).
 * quit with one keystroke
 * just two keystrokes to get a long directory listing
 * now  will list only lines in your history that include
 * log into stat1002 much more easily
 * if you type it more than twice a day, make an alias!
 * makes a case-insensitive version of
 * pipe into  to get a frequency sorted list of lines; use   as the last command to get the most frequent item first; e.g.,
 * or  alias a common typo to what you intended

history config

 * Only keep one copy of any give command in your history. Very useful is you repeat the same command or set of commands over and over without filling up your history
 * If the histappend shell option is enabled, the lines are appended to the history file, otherwise the history file is over-written.
 * synch up history across all terminals
 * is run every time before a command line prompt is displayed
 * appends this terminal's history to the history file;  appends the history file to this terminal's history
 * See also https://www.gnu.org/software/bash/manual/bashref.html#Bash-History-Builtins
 * See also https://www.gnu.org/software/bash/manual/bashref.html#Bash-History-Builtins

export LC_ALL=en_US.UTF-8
Makes lots of stuff work better with Unicode

Navigation
These are some useful command-line navigation shortcuts.

up-arrow & down-arrow
Move through previous command line commands; edit or just re-run them

CTRL-r
Search back through your history for a command matching what you type. will take you to your last command with  in it. again will jump back to the matching command before that!