Linux-Commands
From DevRandom
Note: Some of these commands are derived from several online articles/examples, Co-workers etc and the credit goes to the respective authors
| Description | Command | |
|---|---|---|
| Using curl to find different times | curl -w '
Lookup :%{time_namelookup}
Connect :%{time_connect}
PreTransfer :%{time_pretransfer}
Transfer :%{time_starttransfer}
Total :%{time_total}
' -o /dev/null -s http://www.sriramrajan.com
| |
| Using netstat to count connections |
netstat -anp | awk '{print $5}' | cut -d: -f1 \
| sort | uniq -c | sort -n
| |
| Tcpdump fun
Show all syn and ack on eth0 Show packets with the Reset bit Show SYN and FIN packets Show packets with more than 1024 bytes | tcpdump -i eth0 -n 'tcp[13] == 18' tcpdump -ni fxp0 'tcp[13]&4!=0' tcpdump 'tcp[13] & 3 != 0' tcpdump 'ip[2:2] > 1024' | |
| Counting connections |
while true; do j=`netstat -s | awk '/active connections/{print $1}'`; \
echo $((j-i)); i=$j; sleep 1; done
| |
| tar fun. Copy test2 directory to a remote server using tar | tar cf - test2 | ssh <remote server> "( cd <remote dir>; tar xf - )" | |
| Print A to Z using bash |
for i in {A..Z}; do echo -n "$i "; done
| |
| Serve the current directory
on port 8080 and on all the interfaces | python -m SimpleHTTPServer 8080 | |
| Save a file with sudo privileges
This helps if you have started editing a file and the realised that you don' have permissions. Assumes the user is in the sudoers file | :w !sudo tee % | |
| Count URLs in apache log |
cat access_log | awk '{print $7}' | sed 's/\/$//g' \
| sort | uniq -c | sort -rn
| |
| Use GDB to dump a bash shells command history.
This will dump the command history of the bash shell under /tmp/bash_history-APID.txt |
APID=<process id of the shell> ; gdb -batch --eval "attach $APID" \ --eval "call write_history(\"/tmp/bash_history-$APID.txt\")" \ --eval 'detach' --eval 'q' | |
| List memory percentage in sar -r output |
(unset LANG ;sar -r) | \
awk '$3~/[0-9]/{total=$3+$2; usedbc=$3-($5+$6); pc_used=(100*usedbc)/total;print $0,pc_used} $3!~/[0-9]/{print $0}'
| |
| Find 'c:\documents and settings\<3 digit number>\' and
replace it with '|<3 digit number>|' |
cat test.txt | sed -e :a -e 's/c:\\documents and settings\\\([0-9]\{3\}\)\\/|\1|/g'
| |
| Extract a table from mysqldump |
cat backup_data.sql | sed -n '/-- Table structure for table `tablename`/,/UNLOCK TABLES;/p' > restore.sql | |
| Use sar data to show 'real' (memory minus buffers) memory use by hour, for every day in the last month |
for i in $(ls /var/log/sa/ | grep -v sar); do (unset LANG ;sar -f /var/log/sa/$i -r) | \
awk '$3~/[0-9]/{total=$3+$2; usedbc=$3-($5+$6); pc_used=(100*usedbc)/total;print $0,pc_used} $3!~/[0-9]/{print $0}' | \
awk '{print ($1" - "$4" - "$11)}'| head -1 | \
grep -v "memused"; echo "Time - Real memory used %";(unset LANG ;sar -r -f /var/log/sa/$i) | \
awk '$3~/[0-9]/{total=$3+$2; usedbc=$3-($5+$6); pc_used=(100*usedbc)/total;print $0,pc_used} $3!~/[0-9]/{print $0}' | \
awk '{print ($1" - "$11)}'| grep "00:01" | grep -v "memused"; echo; done
| |
| Run netcat every 10 seconds to check conenctivity and log to a file in the current working directory. |
do echo `date` `nc -n -w 2 -z <destination ip> <destination port>` >> nc.log; sleep 10; done | |
| Purge mysql binary logs older than 2 hours. Useful when adding to cron. |
echo "PURGE BINARY LOGS BEFORE '"`date '+%Y-%m-%d %H:%M:00' -d "-2 hours"`"';" | mysql | |
| Check the reserved percentage on a ext3 filesystem |
tune2fs -l /dev/sda1 | egrep 'Block count|Reserved block count'|
awk -F: '{ gsub(/[[:space:]]*/,"",$2) } /^Block/ { tot=$2 } /^Reserved/ { res=$2 } END {print (res/tot)*100}'
|






