Hugonweb | Linux Cheat Sheet

Bash String Manipulation

All of these start with a dollar sign, but I\'m having trouble rendering them

Pattern Explanation
${#string} String Length
${string:startpos} Extract substring of string starting at startpos
${string:startpos:len} Extract substring of string starting at startpos with len chars
${string#substring} Remove shortest match of substring from front of string
${string##substring} Remove longest match of substring from front of string
${string%substring} Remove shortest match of substring from back of string
${string%%substring} Remove longest match of substring from back of string
${string/substring/replacement} Replace first match of substring in string with replacement
${string/substring/replacement} Replace all matches of substring in string with replacement
${string/#substring/replacement} If substring matches front of string replace with replacement
${string/%substring/replacement} If substring matches end of string replace with replacement

Bash Globbing

See man bash for details.

Pattern Explanation
* matches anything
? matches any one character
[A-Z] matches any one character in the list

[] can also use character classes surrounded by colons: alnum alpha ascii blank cntrl digit graph lower print punct space upper word xdigit

You can also match things listed in a list (first\|second\|third) with \| between entries using these commands:

Glob Explanation
?(pattern-list) Matches zero or one occurrence of the given patterns
*(pattern-list) Matches zero or more occurrences of the given patterns
+(pattern-list) Matches one or more occurrences of the given patterns
@(pattern-list) Matches one of the given patterns

You can even have wildcards *?[] in those pattern lists. To enable extglob:

shopt -s extglob

then you can do:

!(pattern-list) Matches anything except one of the given patterns

tmux Commands

tmux # create new tmux session
tmux a # attach to already running tmux session

You must use the prefix key to use any of the command keys. My prefix key is C-a, C-b is the default.

Command Key Action
d detach from tmux session
c create new window
w list windows
n next window
p previous window
, rename current window
\% vertical split window into panes
\" horizontal split window into panes
q show pane numbers
<space> toggle between pane layouts
<arrow key> (or hjkl in my config) Move between panes
o Cycle between panes
M-o Rotate pane positions
C-<arrow key> Resize current pane in steps of 1 cell
M-<arrow key> Resize current pane in steps of 5 cell
{ go into copy mode, enter to exit, space to select and enter to finish selection and exit
} past from copy mode

You can get into command mode by doing C-a (or by default C-b) followed by a colon

Command Short Command Required Arguments Action
:break-pane :breakp Break current pane out into it\'s own new window
:join-pane :joinp -t <destination window> Join current pane into destination window number

Source or destination panes are of the format <window number>.<pane number> and can be specified as -s and -t options to the following commands. If you leave out the window number, the current one is assumed. If you leave out -s, the current pane is assumed, if you leave out -t a new window is created.

VNC Through an SSH Tunnel

  1. SSH into the server
  2. Run vncserver on the remote server, this will display a line like New '<hostname>:<port> (<username>)' desktop is <hostname>:<port>
  3. On your local machine run: ssh -N -C -f -L 5900:localhost:5900 <username>@<hostname> where 5900 should be replaced with 5900 + <port> from above
  4. Use a VNC client (like TigerVNC) to connect to localhost:<5900 + port>
  5. When you\'re done, run vncserver -kill :<port> to kill the vnc session