Skip to content

Instantly share code, notes, and snippets.

@tsyber1an
Forked from tedslittlerobot/bash tricks
Created September 28, 2015 09:25
Show Gist options
  • Save tsyber1an/55b11d50a6e0b46152a6 to your computer and use it in GitHub Desktop.
Save tsyber1an/55b11d50a6e0b46152a6 to your computer and use it in GitHub Desktop.
Bash Tips and Tricks for Ninja...

Copy (OSX only)

Pipe anything to pbcopy to copy it to the clipboard: cat ~/.ssh/id_rsa.pub | pbcopy

The last argument

To insert the last argument of the previous command wherever the cursor currently is, press alt+. (or, on a mac, esc+.)

File expansions

touch {a,b,c}.txt - writes a.txt, b.txt, c.txt

rm [ab].txt - deletes a.txt and b.txt

End of Line, Flynn

You can use Ctrl+a and Ctrl+e to move the cursor to the start and end of the line respectively

cd

The cd command has a history stack. cd - will take you to the previous directory you were in (running it again will take you back). cd -3 will take you three directories back. (so, cd - is the same as cd -1)

You can use the dirs -v command to see the current stack. The ten most recent ones can be shown in oh-my-zsh with the d command.

Misspellet Commands

lk -al - throws an error - lk: command not found ^lk^ls - re-runs the previous command, replacing lk with ls

BANG!

Bang arguments can be appended with :p to preview the command before executing it (oh-my-zsh enables this all the time automatically)

Bang Bang

The one that everyone has hopefully heard of is !! - repeat the last command. Most commonly used:

rm -rf /
This operation requires root
sudo !!

Bang Number

!3 will run the 3rd command in your bash history !-3 will run the 3rd last command in your bash history !-1 is the same as !!

Bang Search

!ec - runs the last command that starts with “ec” (likely echo).

!?cho - runs the last command that has “cho” anywhere in it (likely echo). Basically, a non-interactive version of Ctrl-r reverse searching.

Bang Arguments

!!:3 or !:3 - expands to the third argument of the preceding command (change the number for the different arguments obviously)

!$ - the last item of the previous command !^ - the first argument of the previous command (same as !:1) !* - all arguments (non-command) from the previous command

Bang Filenames (zsh)

Use !:t to strip the filename out of the last argument of the previous command.

wget ftp://ruby-lang.org/pub/ruby/1.8/ruby-1.8.7-p330.tar.gz tar xzvf !:t

Filepath expansion

The unix shell automatically expands file path patterns. When multiple files match, they are listed, separated by spaces.

Star *

The star character matches 0 or more characters, apart from / (the directory separator.)

For example, on OSX:

echo ~/Do*
/Users/user/Documents /Users/user/Downloads

###  Question Mark ?

The question mark matches exactly one single character, apart from `/`.

```bash
echo /var/logs/nginx/error.log.?.gz
/var/logs/nginx/error.log.1.gz /var/logs/nginx/error.log.2.gz /var/logs/nginx/error.log.3.gz

Brace Expansion {}

Unlike the * and ? matchers, brace expansion will happen regardless of whether or not a file exists.

Brackets can be used to expand a list into a filepath:

echo {a,b,c}.txt
a.txt b.txt c.txt

Brackets can also deal with ranges:

echo error.{9..12}
error.9 error.10 error.11 error.12

echo {a..e}
a b c d e

Zsh Specifics

esc+' will put single quotes around the current command

esc+_ will insert the last argument of the previous command. release all the keys and press again to go back in history

esc+q will clear the line, but repaste that line into the next command (so you can do another command before it)

cd one two will replace all instnaces of one in the CWD with two

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment