Skip to content

Instantly share code, notes, and snippets.

@charleslouis
Last active October 12, 2019 06:37
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save charleslouis/c339790ebb8a882acf3a5453cb5caa1a to your computer and use it in GitHub Desktop.
Save charleslouis/c339790ebb8a882acf3a5453cb5caa1a to your computer and use it in GitHub Desktop.
Cheat sheet CLI Zsh bash

Vim

-ci' - change inside the single quotes -ciw - change inside a word -ci( - change inside parentheses -dit - delete inside an HTML tag, etc -80i * ^[ make a line of 80* -fn12 = nohls -:FZF to bring FuzzyFile -^N to open NerdTree -^WW change window -/ find -s/foo/bar/g search foo and replace by bar globally (not sure about g)

Vim fold

The actions are based on the command z associated with the action option:

zc — close the fold (where your cursor is positioned) zM —close all folds on current buffer zo — open the fold (where your cursor is positioned) zR — open all folds on current buffer zj — cursor is moved to next fold zk — cursor is moved to previous fold

If all foo words need to be changed to bar :

  • Put the cursor on foo .
  • Press * to search for the next occurrence.
  • Type ciw (change inner word) then bar then press Escape.
  • Press n (move to next occurrence) then . (repeat change).
  • Repeat last step.

Git

git push --set-upstream origin master so you can use gp for git push origin master and gpu for git pull origin master

get all the files changed between two branches git diff --name-only <notMainDev> $(git merge-base <notMainDev> <mainDev>)

SSH

Create ssh key

ssh-keygen -t rsa -C "your_email@example.com" Private key is supposed to be chmod 600

Create ssh key on a user folder

Ex: for CI deployment sudo -u myuser ssh-keygen -t rsa

Define SSH key to use for a host

Ex: one key per repo vim ~/.ssh/config Then : Host my-host IdentityFile /home/myname/.ssh/id_rsa_name

.htpasswd

Create user/pw pair in htpasswd

htpasswd /etc/nginx/.htpasswd mysuser (it will prompt for password)

htpasswd -c /etc/nginx/.htpasswd mysuser (happend -c if there is no htpasswd file and you need to create one. Beware as it will erase the previous one if a file already exists).

Tell nginx to look for the credentials

In Froxlor look for *Own vHost-settings: * in domain settings :

location / { auth_basic "Restricted"; auth_basic_user_file /etc/nginx/.htpasswd; }

Email on VPS

Install

Test postfix

echo "body of your email" | mail -s "This is a Subject" -a "From: root@yoursite.com" youremail@gmail.com

SMTP Mandrill

Find things

Based on file name

find . -regex '.*/some_text_in_file_name/[^/]*.jpg'

Grep

Find a file contaioning some expressiom:

grep -rnw '/path/to/somewhere/' -e 'pattern'

  • '-r' or '-R' is recursive,
  • '-n' is line number, and
  • '-w' stands for match the whole word.
  • '-l' (lower-case L) can be added to just give the file name of matching files. Along with these, '--exclude', '--include', '--exclude-dir' flags could be used for efficient searching:

This will only search through those files which have .c or .h extensions:

'grep --include=*.{c,h} -rnw '/path/to/somewhere/' -e "pattern"'

This will exclude searching all the files ending with .o extension:

'grep --exclude=*.o -rnw '/path/to/somewhere/' -e "pattern"'

For directories it's possible to exclude a particular directory(ies) through --exclude-dir parameter. For example, this will exclude the dirs dir1/, dir2/ and all of them matching *.dst/:

'grep --exclude-dir={dir1,dir2,*.dst} -rnw '/path/to/somewhere/' -e "pattern"' This works very well for me, to achieve almost the same purpose like yours.

see https://stackoverflow.com/a/16957078 For more options check man grep.

VPS security

prevent root login

prevent password login

Add user to replace root

adduser username && adduser username sudo

Upload public key to new user on VPS without login

cat ~/.ssh/id_rsa.pub | ssh newuser@vps-alias "mkdir -p ~/.ssh && chmod 700 ~/.ssh && cat >> ~/.ssh/authorized_keys"

Files

Replace file names by new file name based on CSV file

replace ";" by "," on CSV as Excel creates CSV with ";"

Use Vim :%s/foo/bar/g

Or use sed :

sed -i 's/original/new/g' file.txt source [https://askubuntu.com/questions/20414/find-and-replace-text-within-a-file-using-commands]

sed = Stream EDitor -i = in-place (i.e. save back to the original file)

The command string:

  • s = the substitute command
  • original = a regular expression describing the word to replace (or just the word itself)
  • new = the text to replace it with
  • g = global (i.e. replace all and not just the first occurrence)
  • file.txt = the file name

Batch rename files

Rename based on CSV :

sed 's/"//g' 201805010-NDF-Bluecraft-2017-gmail.csv | while IFS=, read orig new; do mv "$orig" "$new"; done OR avoid encoding iussues with C_CTYPE=C && LANG=C C_CTYPE=C && LANG=C && sed 's/"//g' 201805010-NDF-Bluecraft-2017.csv | while IFS=, read orig new; do mv "$orig" "$new"; done

Change part of the files' name

zmv '(*)--Gmail - (*)' '$1--Gmail-$2'

Change file extension

for x in *.JPG; do mv "$x" "${x%.JPG}.jpg"; done

Remove ? from file extension if filname is dirty

for f in *?*.pdf; do mv -- "$f" "${f//?/}"; done

Check file integrity

Chec SHA sum : openssl sha -sha256 filepath

GIT

Patche a file based on several commits

  • get the commits git log --oneline css/style.css

  • get the patches

git show 7c02d19 -- css/style.css >> patchfile7c02d19;
git show 0d432f3 -- css/style.css >> patchfile0d432f3;
git show b02eec0 -- css/style.css >> patchfileb02eec0;

  • since I've done this on an empty file, it could not find the lines This will create rejection files from patch
patch -p1 < patchfile7c02d19; cp css/style.css.rej stylepatches/style.css.rej.7c02d19;
patch -p1 < patchfile0d432f3; cp css/style.css.rej stylepatches/style.css.rej.0d432f3;
patch -p1 < patchfileb02eec0; cp css/style.css.rej stylepatches/style.css.rej.b02eec0;
  • concat all the patches - then finish manually cat stylepatches/* > stylepatches/stylepatches.css

Images

Resize images (mac)

sips --resampleHeightWidthMax 1800 *.png sips -s dpiHeight 144 -s dpiWidth 144 *.png sips -s dpiHeight 72 -s dpiWidth 72 *.png sips -z 256 256 my-icon.png --out my-icon@256.png If you'd like to resize all the images in a directory, enter sips --resample[mode] [number] /[pathtomydirectory]/*.* sips -Z 640 *.jpg sips -s format jpeg -s formatOptions 80 "Beach party.tiff" --out "Beach party.jpg

Batch conversions are essentially the same as single-file conversions, but it’s a bit trickier due to how Terminal processes multiple files. Here’s a real-world example: I have a folder of 24 large (1.5MB to 2MB each) PNG files that I want to convert to JPEGs while keeping the original files intact. Once I’m in the proper directory (steps one and two in the above process), I need to use a loop to read all the PNG files, and convert each one with sips. That loop command looks like this:

for i in *.png; do sips -s format jpeg -s formatOptions 70 "${i}" --out "${i%png}jpg"; done

use-sips-to-quickly-easily-and-freely-convert-image-files bulk-resize-images-with-sips-on-mac/

Remove all spaces in a doc

Note : I use this to deal with large JS objects in Pug (ex. jade). I use Pug as a static site generator and it only accepts single lined arrays of objects. - var projects=[{"name":"Project\ name cat src/human-readable.json | tr -d " \t\n\r" > dist/no-space.json

Eslint

/*eslint no-unused-vars: ["error", { "varsIgnorePattern": "someVar"}]*/

Images

SVG to PNG

Install brew install librsvg if needed, then :

for i in *; do rsvg-convert $i -x 3 -y 3 -a -o `echo $i | sed -e 's/svg$/png/'`; done

-x 3 (ratio Width X 3)
-y 3 (ratio Height X 3)
-a (keep aspect ratio)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment