Giter Club home page Giter Club logo

useful-commandline-snippets's Introduction

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

useful-commandline-snippets's People

Contributors

james-firth avatar

Stargazers

Kurt Hanel avatar

Watchers

 avatar James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.