Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Command-line Magic

Regular Expressions

Not only useful in programming, regular expressions can make complex tasks easy in many different scenarios. Here is a great site for testing your regex:

http://rubular.com

General Shell

Run the previous command again

!!

Run the previous command with sudo

sudo !!

Run previous command replacing foo with bar

^foo^bar

Search your command history

ctrl + R

Run a command every 2 seconds and watch for output changes

watch -n 2 -d '/sbin/ifconfig eth0'

What IPs are bound to the machine

ip addr |awk '/inet/ {sub(/\/.*/, "", $2); print $2}'

or

ifconfig -a |awk 'BEGIN{FS="[ :]+"} /Bcast/{print $4}'

SSH

Run a command over SSH

This command will return the number of Apache processes running on the remote host.

ssh user@host 'ps -ef | grep apache | grep -v grep | wc -l'

This command will grab the first 8 lines from the top output, showing vital system stats.

ssh user@host 'top -b -n 1 | head -n 8'

Apache

Find, Sort, Tally total requests by IP address

cat /var/log/apache2/access_log | awk '{print $1}' | sort | uniq -c | sort -rn | more

Find

Search the current directory (represented by the period) and below it for files and directories with names starting with ex:

find . -name "ex*"

Find only files:

find . -type f -name "ex*"

Find only directories:

find . -type d -name "ex*"

Find within a certain directory:

find /home/example -name "ex*"

Case-insensitive Usage

find . -iname "ex*"

Finding File Extensions

find . -type f -iname "*.php"

Search for multiple file extensions:

find . -type f \( -iname "*.php" -o -iname "*.htm*" -o -iname "*.css" \)

Filtering Searches

Examples of using find switches to filter out items from the search results.

Filter out items owned by apache, named, webalizer or cgi-bin:

find . -not -user apache -and -not \( -iname "webalizer" -prune -or -iname "cgi-bin" -prune \)

Using find Results

The find command is made extra useful by redirecting its output to other commands. Below are some handy examples of using find output.

Moving all files in a directory to another location:

find . -type f -name '*.bak' -exec mv '{}' backup_dir/ \;

Change file and directory permissions:

find . -type f -exec chmod 644 {} \;
find . -type d -exec chmod 755 {} \;

Searching the contents of the found files for a string using grep:

find . -type f -iname "*.php" -print0 |xargs -0 grep -i -n "example"

Filter out files and add results to a compressed tar archive:

find . -type f -not \( -iname "webalizer" -prune -or -iname "_admin" -prune -or -iname "__sd" \) -print0 |xargs -0 tar --no-recursion Szcf myfiles.tar.gz

Create a compressed tar archive of all files modified in the last 30 days:

find . -type f -mtime 30 -print0 |xargs -0 tar --no-recursion -Szcf archive.tar.gz

Screen

Create a new session

ctrl + Ac

Switch to previous session

ctrl + Aa

Switch to next session

ctrl + An

Switch to previous session

ctrl + Ap

Detach and leave sessions running

ctrl + Ad

List screen sessions

screen -ls

Restore a previous screen session

screen -R -d {session_id}

Mac OS X

Open a file from terminal

open {filename}

Open a file with a specific app

open -a {app} {filename}

Python

Serve the current directory

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