https://github.com/james-firth/useful-commandline-snippets
A list of commandline snippets I find useful or always need to look up. Also some useful shell scripts apparently.
https://github.com/james-firth/useful-commandline-snippets
bash snippets
Last synced: about 1 month ago
JSON representation
A list of commandline snippets I find useful or always need to look up. Also some useful shell scripts apparently.
- Host: GitHub
- URL: https://github.com/james-firth/useful-commandline-snippets
- Owner: James-Firth
- Created: 2015-09-01T16:10:54.000Z (almost 11 years ago)
- Default Branch: master
- Last Pushed: 2022-01-24T18:10:13.000Z (over 4 years ago)
- Last Synced: 2025-10-23T23:11:43.977Z (8 months ago)
- Topics: bash, snippets
- Language: Shell
- Homepage:
- Size: 34.2 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
Awesome Lists containing this project
README
# Useful Snippets of commandline code
## General Information o7
* The `^` (carat) symbol is often used to denote the `Ctrl` button so `^C` means `Ctrl+C`
* `NO_PUBKEY` error? Make sure to add keys via something like
```bash
wget http://download.opensuse.org/repositories/PATH/TO/KEY/Release.key
apt-key add - < Release.key
```
* Use `^L` (`Ctrl+L`) to clear the terminal, even if using a sub-program like mongo or python's interactive shells.
* `<(echo "hello world")` will put the results of your process in a temp file and pass the file path to a different process, `cat <(echo "Hello world")` for instance will invoke something like `cat /proc/self/fd/11`. Very useful for diffs, etc.
## File Mangling
Accidentally spew the entire HTTP header, including the Authentication header, to a log file?
This will look for `Api-Key: THEKEY`and replace `THEKEY` with a redaction, in-place.
`gawk -i inplace '{IGNORECASE=1; r = gensub(/(Api-Key:)\s*(.+)$/, "\\1 ***REDACTED***", "g"); print r;}' oops.log`
## SCP
References: http://www.hypexr.org/linux_scp_help.php
Copy the file "foobar.txt" from the local host to a remote host
`scp foobar.txt your_username@remotehost.edu:/some/remote/directory`
To copy a directory:
`scp -r "path/to/directory" your_username@remotehost.edu:/some/remote/directory`
## SSH keys
http://www.thegeekstuff.com/2008/11/3-steps-to-perform-ssh-login-without-password-using-ssh-keygen-ssh-copy-id/
Copy your ssh key to another machine to avoid typing in passwords
`ssh-copy-id -i ~/.ssh/id_rsa.pub remote-host`
Alternatively use my script `the_keymaker.sh` to automate this process a bit. Also maintains a nicer ssh config file.
## Tar
untar
`tar -xvf filename`
## Process Management
Find all the processes that contain a word, such as all python processes
`ps aux | grep ` - shows a lot of output including the path of the file and args (more detailed)
`ps -e | grep ` - shows the PID and the process name (much cleaner)
Parallelize processes.
Using the code below command1 and command2 will be run in the background and command3 will be run normally. The benefit is when a SIGINT (via `^C`) is sent, all 3 commands will be killed.
```
trap 'kill %1 kill %2' SIGINT
command1 & command2 & command3
```
## Grep
Use status code to determine if that line of text exists in that file.
`grep -Fxq "line of text" /path/to/file`
Search a directory recursively for a term. (Ignores case, only searches text files, and shows a line above and below the matched term)
`grep -ainr -B 1 -A 1 "TERM" /DIR/TO/SEARCH`
## Moving files
If you want to move everything in the current working directory into a sub-folder named `newdir` and want an exit status code of `0`, you can't use `mv` because it will try to move `newdir` into itself and error. Instead use this
`find . -maxdepth 1 ! -path . | grep -v "newdir" | xargs -i mv {} newdir`
## Useful CLI programs to install/uses
* Woof
* Simple file server
* mpv
* Commandline media player
* tmux
* Terminal multiplexer. Better than screen (debatably) and useful for servers.
*
* [jq](https://stedolan.github.io/jq/download/)
* like `sed` for JSON data
## Git - Useful snippets and common gotchas
* [Oh crap I commited to the current branch but I wanted to make a new one](http://stackoverflow.com/questions/1628563/move-the-most-recent-commits-to-a-new-branch-with-git)
*
## TODO
* Eventually split this into better chunks/files.
* Add useful snippets for diffregits