Batch File Rename

by Kyle on

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

Block IP Address

by Kyle on

iptables -I INPUT -s 25.22.22.22 -j DROP

Change Permissions For Directories Only

by Kyle on

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

Change Permissions For Files Only

by Kyle on

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

Check How Long A Process Has Been Running

by Kyle on

ls -ld /proc/{pid}

Check Site Load Time

by Kyle on

curl -o /dev/null -w %{time_total}\\n http://www.example.com

Clear Terminal Screen

by Kyle on

clear

Copy Files Without Overwriting Existing Files

by Kyle on

yes n | cp -i source/* dest/

Create A Symbolic Link

by Kyle on

ln -s /source/file ./destination/file

Create An Empty File

by Kyle on

touch myfile.txt

Delete Files Except Ones With Certain Extension

by Kyle on

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

Delete Zero Size Files

by Kyle on

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 }'

Find And Delete Files Older Than X Days

by Kyle on

Delete files older than X days:

find . -type f -mtime +X -exec rm {} \;

If you want to search a specific directory:

find /path/to/files -type f -mtime +X -exec rm {} \;

You can also delete directories by switching the flag type:

find . -type d -mtime +X -exec rm {} \;

Find And Replace First Occurrance

by Kyle on

gsed '0,/find/s//replace/'

Format A Date

by Kyle on

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

Get Filename Without Extension

by Kyle on

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

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

Get Total Inode Usage

by Kyle on

find . -printf '%i\n' | sort -u | wc -l

List And Sort Directory Contents By Date

by Kyle on

ls -alrt

List Inode Count For All Directories

by Kyle on

for i in `find -type d -maxdepth 1`; do echo -n $i; echo -n " "; find $i | wc -l ; done

List Open Connections And Sort By IP Address

by Kyle on

netstat -atun | awk '{print $5}' | cut -d: -f1 | sed -e '/^$/d' |sort | uniq -c | sort -n

Random Password Generator

by Kyle on

#!/bin/bash
w="$1"
 
[[ -n "$w" ]] || w=12
if [[ $w -lt 8 ]]; then
    echo "A password of length $w would be too weak."
    exit 1
fi
passwd=$( cat /dev/urandom | tr -dc "a-zA-Z0-9_\?\$\#\." | fold -w $w | head -1 )
echo "${passwd}"

Recursively Delete .DS_Store Files

by Kyle on

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

Recursively Delete All .svn Directories

by Kyle on

find . -type d -name .svn -print0 | xargs -0 rm -rf

You can also run the following command:

find . -type d -name .svn -exec 'rm -rf {}\;'

Recursively Find And Delete Files

by Kyle on

This command will recursively search for a file and remove it.

For example, if you wanted to delete all index.html files, you would do this:

find ./ -name 'index.html' -exec rm '{}' ';' -print

If you wanted to remove all files ending in .html, you would do this:

find ./ -name '*.html' -exec rm '{}' ';' -print

Recursively Find And Replace Text Inside Files

by Kyle on

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

Remove Iframe Injected Code

by Kyle on

If your index.html and index.php files have suffered from an iframe injection, you can use the following grep/sed command for clearing it out.

Please be careful and test first with just the grep command to ensure you don't clear out anything you don't need.

This command does not backup files!

grep -lr -e '<iframe src="http://.*</iframe>' * | xargs sed -i 's/<iframe src="http:\/\/.*<\/iframe>//g'

Rename Files To Lowercase

by Kyle on

rename 'y/A-Z/a-z/' *

Replace Spaces In File Names With Underscores

by Kyle on

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

Reset MySQL Root Password

by Kyle on

mysqladmin -u root -p OLDPASSWORD NEWPASSWORD

Search Inside Files And List Matches

by Kyle on

find . -type f -exec grep -l 'KEYWORDS' {} /dev/null \;

Search Strings In PHP Code

by Kyle on

find . -name "*.php" -exec grep string {} \; -exec echo -e {} \;

Secure Copy (SCP) Over SSH

by Kyle on

scp -r user@127.0.0.1:/remote/file ./local/file

Set MySQL Root Password

by Kyle on

mysqladmin -u root password NEWPASSWORD

Show Each Process Memory Usage

by Rick on

ps axu

Show full command in ps display:

ps axu --width=500

Show Process CPU Usage

by Rick on

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

Sort Folders By Size

by Kyle on

du --max-depth=1 /home/ | sort -n -r

Strip Whitespace From Bash Variable

by Kyle on

STRING_VAR=`expr "$STRING_VAR" : '[[:space:]]*\(.*\)[[:space:]]*$'`

Untar All Archives In A Directory

by Kyle on

for i in `ls -1 *.gz`; do tar xzvf $i;rm $i; done

Use A Tab In Echo

by Kyle on

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."

View Open Ports

by Kyle on

netstat -tulpn | less

Hide Sidebar