2017-12-20-system-toolbox.md 8.2 KB


description: > Tips, and tricks covering builtins, and other common CLIs.

title: "System Toolbox"

Table of Contents
  • TOC {:toc}

Help

| Description | Command | |-------------------------------|----------------------| | \'s usage | ` --help` | | Get info on \ | `info ` | | Show \'s manual | `man ` | | \ summary | `whatis ` | | Search command by \ | `apropos ` | | Query the type of \ | `type ` | | Binary used on \ | `which ` |

Display

| Description | Command | |-----------------------------------|------------------| | Clear screen | `clear` | | Reset terminal display | `reset` |

Directory management

| Description | Command | |-----------------------------|---------------------------------| | Current directory | `pwd` | | Go to directory | `cd [directory]` | | Go back a directory | `cd ..` | | Go back 2 directories | `cd ../../` | | Go `HOME` | `cd` or `cd ~` | | Go to root | `cd /` | | Last directory opened | `cd -` | | New directory | `mkdir [directory]` | | New path | `mkdir -p [nested/directories]` | | Remove empty directory | `rmdir [directory]` | | Remove directory & contents | `rm -r [directory]` |

Directory stack

pushd and popd work according to the LIFO principle

| Description | Command | |--------------------------------|---------------| | Show stack | `dirs` | | Show indexed stack | `dirs -v` | | Clear stack | `dirs -c` | | Add `` to top and go | `pushd ` | | Only add `` to top | `pushd -n` | | Toggle top two dirs | `pushd` | | Remove top dir; go to new top | `popd` | | Only remove top dir | `popd -n` | | Go dir index `` from top | `pushd +` | | Go dir index `` from bottom | `pushd -` |

File management

| Description | Command | |--------------------------------|-----------------------------| | List files | `ls` | | List files incl. hidden | `ls -a` | | List files (human friendly) | `ls -lh` | | New file | `touch [file]` | | View file | `less [file]` | | Remove a file | `rm [file]` | | Force removal w/o confirmation | `rm -f [file]` | | Copy file to new file | `cp [file] [newfile]` | | Copy file to directory | `cp [file] [dir]` | | Move/Rename | `mv [file] [new path/name]` | | New file from command output | `[command] > [file]` | | Append output to file | `[command] >> [file]` | | Read file contents w/command | `[command] < [file]` |

Search

| Description | Command | |-------------------------------------|---------------------------------| | Search for filename in /some/dir | `find /some/dir -name "file"` | | Lines in file with pattern | `grep "" file` | | Lines in dir files with pattern | `grep -r "" /some/dir` | | Lines missing the pattern | `grep -v "" file` | | Lines with case-insensitive pattern | `grep -i "" file` |

Command chains

| Description | Command | |---------------------------------|----------------------| | Run command A, then B | `[cmd-A]; [cmd-B]` | | Run command B if A succeeded | `[cmd-A] && [cmd-B]` | | Run command B if A failed | `[cmd-A] || [cmd-B]` | | Run command A, pass result to B | `[cmd-A] | [cmd-B]` |

Command history

| Description | Command | |---------------------------------------|--------------| | Show last n commands | `history n` | | Show last command that starts w/value | `![value]:p` | | Run last command that starts w/value | `![value]` | | Run the last command | `!!` | | Show the last command | `!!:p` |

Prepend any command with a space to exclude it from history.

$  sensitive_cmd

Jobs

| Description | Command/Keybinding | |----------------------------------|--------------------| | Show running jobs | `jobs` | | Run \ in background | ` &` | | Suspend job/process | `C-z` | | Bring last job to foreground | `fg` | | Bring job \ to foreground | `%` | | Resume job \ in background | `bg %` | | End process | `C-c` | | EOF running process; exit shell. | `C-d` |

SSH key creation

We can create SSH keys using ssh-keygen.

$ ssh-keygen -f ~/.ssh/id_ed25519 -t ed25519 -C "public_email@example.com"

For legacy systems where ed25519 isn't available: (avoid whenever possible!)

$ ssh-keygen -f ~/.ssh/id_rsa -t rsa -b 4096 -o -a 100 -C "public_email@example.com"

If more than one set of keys are needed consider ~/.ssh/id_ed25519_<profile> as a naming format. Also, add -C "descriptive comment".

To copy the new public key to the clipboard:

$ pbcopy < ~/.ssh/id_ed25519.pub

On systems with xclip rather than pbcopy we can:

alias pbcopy='xclip -selection clipboard -in'

Use a config file to ease key management. Also, consider adding custom git details from .gitconfig file.

Permissions

Permission is usually handle by the ch* family of commands.

On the terminal, ls -l outputs something like:

drwxr-xr-x 2 pete penguins 4096 Dec 1 11:45 .

where

| type | owner | group | other | | d | rwx | r-x | r-x |
  • d. Directory. If -, then is a file.
  • r. Readable. Value 4.
  • w. Writable. Value 2.
  • x. Executable. Value 1.
  • -. Empty ie. permission off.
| who | r | w | x | mode | |-----------|---|---|---|------| | owner (u) | t | t | f | 6 | | group (g) | t | f | f | 4 | | other (o) | t | f | f | 4 |

Owner is also known as 'user' to avoid being confused with 'other'.

Change Permissions

allow user to exec <file>

$ chmod u+x <file>

disallow group from writing on <file>

$ chmod g-w <file>

allow user, and group to write on <file>

$ chmod ug+w <file>

modify <file> permission to match code

$ chmod 755 <file>

transfer <file> ownership to <user2>

$ sudo chown <user2> <file>

set <file>'s owner group

$ sudo chgrp <group2> <file>

set <file> owners to <user2> and <group2>

$ sudo chown <user2>:<group2> <file>

Sticky bit

This permission sticks files or directories, that is, prevents others from deleting and/or modifying them.

$ sudo chmod +t <dir>
# or
$ sudo chmod 1755 <file>

There are other special bits concerning SUID, and SGID which are mostly use to securely grant special permissions to non-root users in shared environments.