Useful CLI commands

Posted by Richard Lucas on Aug 14 2015

Some CLI Commands that I’ve found useful:

Viewing processes and killing them

$ lsof -Pi | grep LISTEN

  • Displays processes running and ports
  • The PID is the second number (2nd column from left)

$ kill -9 $PID

  • This kills the process at the entered PID with extreme prejudice

Using find

$ find . -name '*.css'

  • This will recurse all directories and list all CSS files (and directories ending with “.css”) under the current directory (represented by “.”)

$ find . -type f -name '*.css'

  • This will only match CSS files (case-sensitively)

$ find . -name "*.css" -exec grep -l "#content" {} \;

  • This example finds all CSS files that do something with your HTML ID #content

$ find . -mtime -1 -type f

  • find files changed in the last 1 day

$ find . \! -path "*CVS*" -type f -name "*.css"

  • find CSS files, omitting results containing “CVS”

$ find ~/src -newer main.css

  • find files newer than main.css in ~/src

$ find . -name \*.css -print0 | xargs -0 grep -nH foo

  • combine with xargs for more power than -exec

Deleting a folder and it’s contents

Please use carefully

$ rm -rf [folder_name]

  • Always include a folder name

Copying a folder and contents

$ cp -avr /from/folder /to/new_folder

  • -a : Preserve the specified attributes such as directory an file mode, ownership, timestamps, if possible additional attributes: context, links, xattr, all.
  • -v : Explain what is being done (verbose).
  • -r : Copy directories recursively.

What is grep

  • It is a command that finds text/strings inside of files
  • It can take regular expressions

  • recurse through a directory

$ grep -r 'lookforthisexpression' files/*

  • case insensitive search

$ grep -i 'anycaseterm' file.txt

Make a file (…for testing…)

  • mkfile command
  • input number, then size (i.e. k for kb or g for gb), then filename

$ mkfile -n [size][b|k|m|g] [filename]

Heroku commands

  • Get to the console/bash on the remote server

$ heroku run bash --app [put_app_name_here]

Compiling bash scripts

$ chmod +x ./[bash_script.sh]

Redirect to port 80 on linux server

$ sudo iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 80 -j REDIRECT --to-port 3000