Category: bash
-
Read a text file with bash line by line
Read a text file in bash line by line with while. It will read till the end of line. You can modify the end of line setting a new value to EOF variable. while read line; do echo “${line}”; done
-
Dd clone partition
Easiest way to clone a partition in command line. dd if=/dev/sda1 of=/dev/sdb1
-
Find and list files containing a string
Recursively search all files matching a regexp in the current directory and then listing them. find . -type f -name “*.file” | xargs grep -l “STRING” This could also be done like this: find . -type f -name “*.file” –exec grep -l “STRING” {} \;
-
DDos attack: Display IP connection count
If you notice high load in your server or unresponsive services it might be you are suffering an ddos attack. If you want to have one snapshot of ips connected to your server, just type this command: $ netstat -ntu | awk ‘{print $5}’ | cut -d: -f1 | sort | uniq -c | sort…
-
Watch a page for changes with watch
This is a recursive description. Watch if a page changes with watch. watch -d -n 5 “echo `date` `wget –timeout=5 -qO – URL
-
How to change MAC address
Easy easy. Just like this. Dont forget to down/up the interface #sudo ifconfig eth0 down hw ether 11:22:33:44:55:66 #sudo ifconfig eth0 up
-
Parse Apache access logs to get all ips
Don’t use it as a way to count visits, as all these ips are not real visits, but bots, crawlers, etc. cat access.log | grep -o ‘\([0-9]\{23\}.\)\{3\}\.[0-9]\{23\}’ | sort | uniq | wc -l
-
Get ip the geek way
If typing ifconfig is too simple for you here it is the geeky way to get the ip of your interfaces. ifconfig | sed -n “/en0//netmask/ p” | grep “inet ” | awk ‘{print$2}’ copy to .bash_profile: alias getip=’ifconfig | sed -n “/en0//netmask/ p” | grep “inet ” | awk ”{print\$2}”’
-
Extract tar archive to specified location
How to extract a tar file to a directory tar -cvvf archive.tar /pathtoextract
-
Deleting Blank Lines Using AWK
Here’s the trick: cat /tmp/test | awk ‘NF > 0’ > /tmp/test1