Favorite Terminal Commands

Terminal

Here’s a list of some random, useful commands and tools to use in the Command Line.

Shut Down right now.

shutdown -h now

See the status of a service.

service nginx status

See the status of all services.

service --status-all

Restart a service.

service nginx restart

Get only the HTTP status code of a site

curl -o /dev/null -w '%{http_code}\n' -s -I URL

What’s my IP?

curl -4 icanhazip.com

What’s my IP, Another method

ip addr show eth0 | grep inet | awk '{ print $2; }' | sed 's/\/.*$//'

What is the current git repo remote URL?

git config --get remote.origin.url

Pretty Git Log

git log --date-order --all --graph --format="%C(green)%h%Creset %C(yellow)%an%Creset %C(blue bold)%ar%Creset %C(red bold)%d%Creset%s"

Git untrack a file

git update-index --assume-unchanged path/to/file

Linux: Update, Upgrade, Autoremove & AutoClean in one line

sudo sh -c "apt-get -y update;apt-get -y dist-upgrade;apt-get -y autoremove;apt-get -y autoclean"

List open files

lsof

List all open ports

sudo lsof -iTCP -sTCP:LISTEN -n -P

List all network ports

netstat -a

Show stats for all network ports

netstat -s

Display directory sizes from current directory

sudo du -h --max-depth=1 -x

Where is php.ini and which is loaded?

php -i | grep "php.ini"

How much free memory is there? -b, -k, -m, -g outputs in KB/MB/GB. -h is human readable, the one i use most often to be honest.

free
free -b
free -h

Better ls. Highly recommend using exa otherwise.

ls -lhaG

SQL dump (export MySQL database)

mysqldump -u root -p database_to_backup > backup_name.sql

Upload/Import MySQL from file

mysql -u username -p database_name < file.sql

NPM: What’s installed globally?

npm list -g --depth 0

What mail records are open?

nslookup -q=mx DOMAIN.com

Query nameserver

nslookup -type=ns DOMAIN.com

Query complete DNS record

nslookup -type=any DOMAIN.com

Spin Up a localhost server via Python

python -m SimpleHTTPServer 9999

Spin Up a localhost server via PHP

php -S localhost:9999

Upload a file from the remote server to local machine

scp your_username@remotehost.com:foobar.txt /some/local/directory

Upload a file to the remote server

scp /file/to/send username@remote:/where/to/put

Scrape a Site quickly with wget

wget -mkEpnp http://DOMAIN.com

Show empty files

print -l **/*(L0)

How long has the machine been running?

uptime

Let’s see what processes are running. Recommend htop as replacement to top.

top

Find all files by Type from the current directory.

find -name *.php

Look inside files, recursively, for a string.

grep -r "127.0.0.1" /etc/nginx/sites-available/

What processes are running? ps -A | grep -i ssh filters ps by process name.

ps -A
ps -A | grep -i ssh

Kill process. First one we get the process ID then pass that ID to the kill command. Second is simpler, uses pkill.

grep -i nginx
kill -9 1285

or

pkill nginx

Kill all by process. Second one kills and restarts a PHP server.

killall php
killall php && php -S localhost:8888

You can repeat commands previous typed obviously by hitting the up arrow on the keyboard. Typing history will list out all commands recently typed. If you want to repeat a longer command without retyping it, just follow up with ! before the history number.

history
!25

Remove duplicate lines using awk.

awk '!($0 in array) { array[$0]; print }' temp

Go to the 100th line in a file using vim.

vim +100 /etc/nginx/sites-available/default

Go to the 100th line in a file using vim.

vim +/SEARCHTERM filename.html

Copy all jpg images to hard-drive

ls *.jpg | xargs -n1 -i cp {} ~/Desktop

Remove a file, but get prompted to confirm before doing so.

rm -i filename.txt

whatis command in Linux is used to get a one-line manual page descriptions.

whatis nginx

Hope this serves as a good reference; Enjoy!

ender