Shell skills take away
1. Empty log files in batch
find . -name "*.log" -exec bash -c ">{}" \;
2. Copy files in batch
find . -maxdepth 1 -type f -exec grep -q "pattern" '{}' ';' -exec cp '{}' /path/to/destination ';'
3. Rename file in batch
for i in `find . -maxdepth 1 -name "*SNAPSHOT*.jar"`; do mv $i `echo $i | sed s/-SNAPSHOT//`;done
4. Diff file (Advanced)
diff -r --ignore-all-space --ignore-blank-lines --ignore-matching-lines='#ident' --ignore-matching-lines='# **' --exclude=MANIFEST.MF --exclude="*test*" --exclude="*svn*"
5. Usage samples
if [ $# -ne 1 ]
then
echo "Usage: `basename $0` {INTG|UA}"
exit -1
else
INPUT=`echo $1 | tr '[:lower:]' '[:upper:]'`
if [ "$INPUT" = "UA" ]; then
DBCONNECT_STRING=$DBCONNECT_STRING_CORP_POS_UAT
elif [ "$INPUT" = "INTG" ]; then
DBCONNECT_STRING=$DBCONNECT_STRING_CORP_POS_INTG
else
echo "Unkown parameter: $1"
exit -1
fi
fi
if [ $# -ne 2 ]
then
echo "Usage: `basename $0` {PORT-NUMBER} {MBEAN-NAME}"
exit -1
else
RET=`expr match $1 "[0-9]*$"`
if [ "$RET" -gt 0 ]; then
echo "Port number is: $1"
else
echo "Invalid port number: $1"
exit -1
fi
echo "MBean name is: $2"
fi
6. SVN revert or delete unversioned files in batch
svn st -u | awk -F "[ ]*" '{print $4}' | xargs svn revert
svn st -u | awk -F "[ \t]*" '{print $2}' | xargs rm -rf
7. Remove version number in artifact id
echo ant-1.7.1.jar | sed -e "s/-[0-9]\+\(\.[0-9]\)*\(_[A-Z][0-9]\)*\(-SNAPSHOT\)\?//g"
8. Find big files by du:
du -sh * 2>/dev/null | egrep "[0-9]+G"
du -s --block-size=M * 2>/dev/null| sort -rn
9. Check if specific class in jar file:
for i in `ls`; do jar tf $i | grep "ClassName" && echo $i; done
10. Collect footprint of CPU/MEM:
top -b -d 1 -p pid > stat.txt
11. Check the exact start time / running time of a specific process
ps -eo pid,lstart,etime | grep [pid]
12. Find PID of process which occupies specified port
/usr/sbin/lsof -i:[port] or netstat -ap | grep [port]
13. Find last reboot time of linux server
last reboot
14. .bashrc
HOME=SOME_FOLDER
eval `dircolors ~/.dircolors`
PS1="\[\e[32;1m\][\$(date '+%m-%d %H:%M:%S')]\[\e[32;1m\][\[\e[36;1m\]\u\[\e[33;1m\]@\[\e[35;1m\]\h\[\e[33;1m\]:\[\e[36;1m\]\w\[\e[32;1m\]]$\[\e[0m\] "
alias ll='ls -ltr --color=tty'
alias p='ps -eo pid,ppid,user,lstart,etime,args --no-headers --sort pid | grep -v $$ | awk '\''BEGIN {format = "\033[32m%5s \033[36m%5s \033[35m%-6s \033[33m%24s \033[36m%12s \033[0m%-s\n"; printf format, "PID", "PPID", "UID", "STARTED", "ELAPSED", "COMMAND"; printf format, "-----", "-----", "------", "------------------------", "------------", "--------------------" }; {printf "\033[32m%5s \033[36m%5s \033[35m%-6s \033[33m%-3s %-3s %-2s %-8s %-4s \033[36m%12s \033[0m", $1, $2, $3, $4, $5, $6, $7, $8, $9; for(i=10;i<NF;i++){printf $i " "}; print $NF}'\'''
function pg() {
p | egrep "PID|-----|$1" | grep -v egrep
}
15. Create root user
#useradd -o -u 0 -g 0 -M -d /root -s /bin/bash admin
#passwd admin
16. Check cron jobs for each user in the system
cat /etc/passwd | cut -d: -f1 | xargs -t -i crontab -l -u {}
17. Create SSH tunnel user
#userdel -r tunneluser
#useradd -M -s /usr/sbin/nologin tunneluser
#useradd -M -s /usr/bin/passwd tunneluser
18. Convert command line option to JVM arguments
if [ $# -gt 0 ]; then
params=( "$@" )
for param in "${params[@]}"; do
if [ "`expr \"$param\" : \"--.*=.*\"`" -ne "0" ]; then
param=${param/--/-D}
JAVA_OPTIONS="$JAVA_OPTIONS $param"
fi
done
fi