Skip to content

A list of commandline snippets I find useful or always need to look up. Also some useful shell scripts apparently.

Notifications You must be signed in to change notification settings

James-Firth/useful-commandline-snippets

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

39 Commits
 
 
 
 
 
 
 
 

Repository files navigation

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
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: THEKEYand 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 [email protected]:/some/remote/directory

To copy a directory:

scp -r "path/to/directory" [email protected]:/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 <search string> - shows a lot of output including the path of the file and args (more detailed) ps -e | grep <search string> - 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
    • like sed for JSON data

Git - Useful snippets and common gotchas

TODO

  • Eventually split this into better chunks/files.
  • Add useful snippets for diffregits

About

A list of commandline snippets I find useful or always need to look up. Also some useful shell scripts apparently.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages