Skip to content

Instantly share code, notes, and snippets.

@hdevilbiss
Last active September 2, 2023 18:42
Show Gist options
  • Save hdevilbiss/9ef364f2af6ecfafddecaaf457f8e253 to your computer and use it in GitHub Desktop.
Save hdevilbiss/9ef364f2af6ecfafddecaaf457f8e253 to your computer and use it in GitHub Desktop.
Collection of commands. WP CLI, Linux, Git

List of Favorite Commands

Linux

Install Windows Subsystem for Linux to get a Linux shell for Windows.

Enter wsl into PowerShell (or your shell of choice) to get started.

๐Ÿ” Search current directory and subfolders for specific text

The search term font-size: 12px is just an example; replace this text with the search pattern.

grep -Rnw . -e 'font-size: 12px';

Source: rakib and CodeWizard on StackOverflow

๐Ÿ—„๏ธ Sort Disc Usage for all Folders โ‰ฅ 1 GB

du -h --threshold=1G

โ™ป๏ธ Sort disc usage by file size

du -sh -- * | sort -h

๐Ÿ”ข Display octal file permissions

Such as 644, 777.

stat -c "%a %n" *

๐Ÿ”‘ Adding SSH key(s)

eval "$(ssh-agent -s)"
ssh-add /mnt/c/Users/Username/.ssh/private_openssh

โญ Remote Sync

rsync: Sync folders and files between a remote server and a local server.

Examples:

  • Sync from a remote server to a local server.
  • Sync between 2 remote servers.
  • WordPress usage: Sync uploads, themes, and plugins.

Source (Remote): username@12.34.567.890:/www/path/public/shared/web/app/uploads/

Destination (Local): /mnt/c/destination/uploads/ (also known as C:\destination\uploads)

โญโ“ Remote Sync Dry-run

rsync -av --dry-run -e 'ssh -i /local/path/to/public_id_rsa -p 22' username@12.34.567.890:/source/home/username/domains/example.com/shared/web/app/uploads /mnt/c/destination/uploads/

โญโ— Remote Sync Regular Run

Remove the --dry-run flag.

rsync -av --progress --delete -e 'ssh -i /path/to/id_rsa -p 22' /mnt/c/source/themes/my-theme/ username@IP:/destination/home/username/domains/example.com/path/to/themes/my-theme/

Notes:

  • Add the --dry-run flag to test the SSH connection from the shell and show which files will be transferred.
  • The $HOME variable will not be the same locally and remotely.
  • The trailing / in uploads/ performs an inner copy, targeting all the files and folders inside of uploads. If the trailing forward slash "/" were omitted, then a new folder would be created inside of the destination uploads folder: /mnt/c/source/uploads/uploads/
  • Consider adding the --progress flag.
  • Carefully consider adding the --delete flag to delete files from the destination, which are not present in the source. Use --delete with caution to avoid losing data!
    • I generally only use --delete to sync development uploads with production so that it exactly matches production.
    • Another common use for --delete is backing up plugin folders before testing new versions. And if the new plugin files need to be overwritten, the --delete flag removes any new files.

Git

Push to a remote subtree.

git subtree push --prefix=path/to/subdir remote-name ref

For example: A static site generator uses a separate remote repository for its theme files.

  1. The path/to/subdir theme is installed at themes/theme-name.
  2. The remote-name was established with git remote add -f theme-name https://github.com/username/theme-name.git
  3. The ref could be master, main, or whichever branch on the remote to be targeted.

Make the Prompt String Green and Bold

Edit the ~/.bashrc file using your editor of choice.

Add an export statement to .bashrc, defining:

  • green-text,
  • the most current folder in path,
  • a couple of spaces, and
  • the $ sign.
export PS1="\[\033[01;32m\]\W \\$ \[$(tput sgr0)\]"

VSCode

๐Ÿ›ธ Select entire line

CTRL+L

๐Ÿ›ธ๐Ÿ›ธ๐Ÿ›ธ Select all occurences

CTRL+SHFT+L

Find settings.json on Windows 10

$HOME/AppData/Roaming/Code/User/settings.json

Make the PowerShell Prompt String Show the Current Folder Name in Green and Bold

Assuming that Get-ExecutionPolicy -List returns RemoteSigned for the current scope, add a prompt function to your PowerShell profile.

Choose your own colors

C:\Users\MyUserName\Documents\WindowsPowerShell\profile.ps1

function prompt {
    $Host.ui.rawui.foregroundcolor = "Green"
    "$([char]27)[1m" + $(Get-Location).Path.Substring((Get-Location).Path.LastIndexOf("\")+1) + " > " 
}

Collapse or Expand an entire file

Too many nested elements? Press CTRL + K + 0 to collapse everything, and CTRL + J + 0 to expand.

"There is a problem in this file"

Go to the problem with CTRL + SHIFT + M

Create HTML5 Scaffold

Within an HTML file, press ! + TAB

Comment Out a Line

CTRL + /

(or) CTRL + K + C to comment. CTRL + K + U to un-comment.

WP-CLI

Command Line Interface for WordPress.

Create new .env file with salts

Install this WP-CLI extension: wp-cli-dotenv-command

Use the wp dotenv init command to create a new environment file with salts:

wp dotenv init --template=".env.example" --with-salts

Check which plugins need update

wp plugin update --all --dry-run

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment