Showing posts with label Linux. Show all posts

Useful terminal snippets :)

by Developer @ pebbl.co.uk on Thursday, 28 July 2011

If you are anything like me, then you'll see any kind of server maintenance or management as something to be avoided if possible. Not that I'm against keeping things maintained or secure... it's just I'd rather not be the one doing it on a server ;) I'm still confused as to how anyone can actually enjoy the role of "Server Admin".

Anyway, it seems that no matter how hard I try to avoid server positions in my day job, I still somehow end up getting landed with the odd task here and there. So ... usually, after a while of head-scratching, googling, and learning some interesting facts and figures about whatever flavour of Linux I'm using, I'll eventually manage to find a solution to what ever the problem was. And as if by magic all will be right with the World.

... then I'll forget everything that I've just learned...

So in an attempt to avoid the above this time around (and possibly to help out other pseudo server admins out there) I've compiled a list of some useful commands:

Count Unique IPs from Access Log

grep "GET /?ac=" access_log.processed | grep 25/Jan/2010 | awk '{print $1}' | sort -u | wc -l

Touch files recursively

find . -exec touch {} \;

Copy Src to Dst ignoring .svn

- test
rsync -avCn [SRC_PATH] [DST_PATH]

- real
rsync -avC [SRC_PATH] [DST_PATH]

- check by checksum rather than date time
rsync -avcCn [SRC_PATH] [DST_PATH]

Find just .svn folders

find . -name "*.svn" -exec ls -al {} \;

Remove just .svn folders

find . -name ".svn" -exec rm -rf {} \;

Connect to a server with port forwarding

ssh [USER]@[DOMAIN] -p [DOMAIN_PORT] -L:[LOCAL_PORT]:[REMOTE_ADDR]:[REMOTE_PORT] -Y

Fix the MYSQL max allowed packet problem (usual Drupal fix)

set global net_buffer_length=1000000; 
set global max_allowed_packet=1000000000;

Server Copy

scp [FILE] [USER]@[DOMAIN]:[DESTINATION_FOLDER]

AWK - count length of field at particular CSV offset (BEWARE: wont ignore commas inside quotes)

awk 'BEGIN { FS = "," } ; { if (length($1)>12) print $1 }' file
awk 'BEGIN { FS = "," } ; { if (length($2)>6) print $2 }' file

DIFF - compare folders (ignore svn)

diff -rq folder1 folder2 | grep -vE '\.svn|\.DS_Store'

SSHFS

sshfs -p [PORT] [USER]@[DOMAIN]:[REMOTE_PATH] [LOCAL_FOLDER] -o follow_symlinks

Remove the "Downloaded from internet" alert (Mac OSX)

defaults write com.apple.LaunchServices LSQuarantine -bool NO

Line Count

wc -l [FILE]

Upload Using rSync (with exclusions)

rsync -vaucCP --exclude 'db.php' --exclude '.*' --exclude 'local.php' --exclude 'tools/*' --delete-after [SRC_PATH] [DEST_PATH]

Find out about the MySQL service

netstat -tunlp | grep ":3306"

Find out about remaining resources

free -m

Using Backticks?

joe `which [name of something]`
ls -alh `which php`
mysql -e "show status;" > log_`date +%y_%m_%d_%H_%M`