Hide Sidebar

Block IP Address

by Kyle on · Posted in Bash

iptables -I INPUT -s 25.22.22.22 -j DROP

Print Active Connections To Port 80 Per IP Address

by Rick on · Posted in Bash

netstat -plan | grep :80 | grep -E "(EST|LIST)" | awk {'print $5'} | cut -d: -f 1 | sort | uniq -c | sort -nk 1

Send Email From Shell

by Rick on · Posted in Bash

mail -s "Subject" email@address.com < message.txt

Install CSF (Firewall) And LFD (Log File Daemon)

by Rick on · Posted in Bash

cd /usr/src/
wget http://www.configserver.com/free/csf.tgz
tar -xzf csf.tgz
cd csf
sh install.sh

Show Process CPU Usage

by Rick on · Posted in Bash

Display how much CPU a process is using:

ps -eo pcpu,pid -o comm= | sort -k1 -n -r | head -1

For example, show top five CPU using processes:

ps -eo pcpu,pid -o comm= | sort -k1 -n -r | head -5

Which will output the following:

0.6 24458 bash
0.3 24457 cphulkd.pl
0.2  7704 php5
0.1  7706 php5
0.1  7705 php5

Show Each Process Memory Usage

by Rick on · Posted in Bash

ps axu

Show full command in ps display:

ps axu --width=500

Set MySQL Root Password

by Kyle on · Posted in Bash

mysqladmin -u root password NEWPASSWORD

Reset MySQL Root Password

by Kyle on · Posted in Bash

mysqladmin -u root -p OLDPASSWORD NEWPASSWORD

Strong Password

by Kyle on · Posted in Bash

The criteria for this regular expression is as follows:

  • Contains at least one (1) uppercase letter
  • Contains at least one (1) lower case letter
  • Contains at least one (1) number or special character
  • Contains at least eight (8) characters
(?=^.{8,}$)((?=.*\d)|(?=.*\W+))(?![.\n])(?=.*[A-Z])(?=.*[a-z]).*$

Delete Files Except Ones With Certain Extension

by Kyle on · Posted in Bash

find . ! -name "*.php" -exec rm -f {} \;

Recursively Delete .DS_Store Files

by Kyle on · Posted in Bash

find . -name *.DS_Store -type f -exec rm {} \;

Delete Zero Size Files

by Kyle on · Posted in Bash

This will delete all files in the current directory that have a size of zero.

find . -type f -size 0k -exec rm {} \; | awk '{ print $8 }'

Replace Spaces In File Names With Underscores

by Kyle on · Posted in Bash

find . -type f -iname "*.mp3" -exec rename "s/ /_/g" {} \;

Get Filename Without Extension

by Kyle on · Posted in Bash

Get the basename (full file name with no path), file name (including extension), and file extension:

filename=$(basename $pathandfile)
extension=${filename##*.}
filename=${filename%.*}

Format A Date

by Kyle on · Posted in Bash

# 20090703
date +"%Y%m%d"

Use A Tab In Echo

by Kyle on · Posted in Bash

To use tabs, returns, and other formatting characters when using the echo command, be sure to specify the -e option:

echo -e "test \t\t This text is tabbed twice."

Recursively Find And Replace Text Inside Files

by Kyle on · Posted in Bash

find . -type f -print | xargs sed -i -e 's/FIND/REPLACE/g'

Batch File Rename

by Kyle on · Posted in Bash

Leave off the final pipe to /bin/sh to test:

ls foo*.jpg | awk '{ print("mv "$1" "$1) }' | sed 's/foo/bar/2' | /bin/sh