Useful Find Commands Find Files by name: find /path -name filename find /tmp /home . $HOME -name Find Files Larger than 1Gb find /path -type f -size +1048576 Find with wildcard: find / -name file\*name Find and process find / -type f -mtime -3 | xargs tar -rf archieve.tar [should not use -c option here,
Bash Script to check if a host is alive Script to check if a host is alive: #!/bin/bash<br></br> ping -c 2 www.amitnepal.com > /dev/null<br></br> if [ $? -eq 0 ]; then<br></br> echo "Alive";<br></br&
Adding a Start/Stop Script to chkconfig In order to add a script to be manageable by chkconfig, you should create the /etc/init.d/example script that has the ability to start and stop script. Now put the following two comments around the header of the script # chkconfig: 345 80 20<br></br&
Export Putty Sessions regedit /e "%userprofile%\desktop\putty-registry.reg" HKEY_CURRENT_USER\Software\Simontatham
Sample mysql queries Delete Rows based on output from a query : DELETE FROM Table1 WHERE EXISTS(SELECT 1 FROM Table2 WHERE Table2.Group = Table1.Group)<br></br> or<br></br> delete from Table1 where id in (select id from table1 inner join table2 on Table1.
Handling arguments in bash #!/bin/bash<br></br> echo Total Number of Arguments Passed: $#<br></br> i=1;<br></br> for var in "$@"<br></br> do<br></br> echo "Argument $i
Handling parameters in Bash Short Mode Only : <br></br> #!/bin/bash<br></br> while getopts :a:b:c opt #define options here : at the front supress error for unknown options<br></br> do<br></br> case $opt in&
Nice and Renice Nice command can be used to start a process with low/high priority: nice -n command value = -20 for highest priority and 20 for lowest priority To reset the priority of a running process, find the pid of the process and run: renice renice +1 102 -u daemon root -p
Shell Script to Backup and Rotate Mysql Database #!/bin/bash<br></br> #Author : Amit K Nepal<br></br> #Last Modified : Nov 01 2011<br></br> BACKUP_DIR=/db_backups<br></br> DB_NAME=dbname<br></br> TSTAMP=`date
Writing Iptables rules by hand *nat :PREROUTING ACCEPT [92:7226] :POSTROUTING ACCEPT [62:3916] :OUTPUT ACCEPT [62:3916] COMMIT *filter :INPUT DROP [0:0] :FORWARD DROP [0:0] :OUTPUT ACCEPT [0:0] COMMIT
Sample Kickstart file for Redhat / Centos platform=x86, AMD64, or Intel EM64T # System authorization information auth --useshadow --enablemd5 # System bootloader configuration bootloader --location=mbr --password=grub # Clear the Master Boot Record zerombr # Partition clearing information clearpart --all --initlabel # Use text mode install text # Firewall configuration firewall --enabled --http --ftp --ssh --telnet --smtp # Run the Setup Agent
write to a file using php $myFile = "testFile.txt"; $fh = fopen($myFile, 'a') or die("can't open file"); $stringData = "New Stuff 1\n"; fwrite($fh, $stringData); $stringData = "New Stuff 2\n"; fwrite($fh, $stringData); fclose($fh);
shell script to monitor port and notify by email !/bin/bash ADMIN_EMAIL="email" PID=$$ POLL_INTERVAL=2 trap traphup HUP sleep 1 i=1 laststate="STARTUP" notify() { #echo "Service State Changed to $1" | mail -s "Service Status Notification" $ADMIN_EMAIL echo "Admin has been notified $1" } while [ $i
One liner to change ssh port You can pass port as parameter too. (/usr/sbin/sshd -t : to check for syntax errors) sed -ie 's/Port.*[0-9]$/Port '2222'/gI' /etc/ssh/sshd_config
Shell script to run command on multiple servers This is great script to run commands on multiple servers at once, especially if you have setup password less connections. !/bin/bash servers="srv1.example.com srv2.example.com srv3.example.com" SSH_PORT=22 cmd=$1 if [ "$cmd" == "" ]; then echo "Usage sh
C,C++ compiler cannot create executables C++ compiler cannot create executables : Install gcc, gcc-c++
System Monitoring , disk,cpu and memory Displays top cpu eaters ps -eo pcpu,pid,user,args | sort -k 1 -r | head -10 Top Memory Eaters: ps -auxf | sort -nr -k 4 | head -10 Load Average : uptime | awk '{print $10}' | sed 's/,//g' Total No. of Cpus [cores]: cat /proc/cpuinfo | grep processor
connect to 127.0.0.1[127.0.0.1]:10024: Connection refused. This is an error with amavisd . Check the proper hostname, that could be guilty.. vi /etc/amavisd.conf and check for hostname Or, vi /etc/postfix/main.cf find content_filter and comment that out ( Temp solution ) Amavis dead but subsys locked : rm /var/amavis/amavisd.pid rm /var/amavis/
Using remote mail server with postfix. Setting up Postfix to use a remote mailserver Edit /etc/postfix/main.cf and add the following > Set this to your email provider’s smtp server. relayhost = yourisp.smtp.servername:25 #or your smtp port if different smtpd_sasl_auth_enable = yes smtpd_sasl_path = smtpd smtp_sasl_password_
Iptables tutorial and examples Delete existing rules: iptables -F or iptables --flush Set Default Chain Policies: iptables -P INPUT DROP iptables -P FORWARD DROP iptables -P OUTPUT DROP Block an IP Address: iptables -A INPUT -s a.b.c.d -j DROP iptables -A input -i eth0 -p tcp -s <ip_addr>
Awk Tutorials and examples Using Awk to sum the list of output: ps aux | awk '{sum +=$4}; END {print sum}' Print Line that match a pattern: awk ‘/string1/ > /string2/’ filename Print specific fields : awk ‘{print $1,$3,$5,$NF;}’ /etc/passwd [ $NF means the last field ] Initialization and Finalization: BEGIN {actions}
Using TcpDump to monitor network activity Note : Default packet size sniffed is 96 bytes, you can override that with -s , you can use –s 0 for complete size capture. Example usage : tcpdump -w capture.pcap -i eth0 tcp port 22 To capture or view for multiple ports : tcpdump -w capture.pcap -i eth0 tcp port 22