10 March 2015

Linux Cheatsheet


# Find or remove files older than 30 days
$ find . -mtime +30 | xargs rm
$ find . -type f -mtime +30 -delete
$ find /logs/ -mtime +30 -exec rm {} \;

# Finding the files of specific size
$ find . -xdev -size +100000000c -exec ls -lrt {} \; // 100MB
$ find . -xdev -size +10000000c -exec ls -lrt {} \;   //10 MB
$ find . -xdev -size +1000000c -exec ls -lrt {} \; 
$ find . -size +100000000c
$ du -sk * | sort -n

# Removing specific files
$ rm `ls -rtl |grep Aug |awk '{print $9}'`
$ rm `ls -rtl |grep 2009 |awk '{print $9}'`

# Check file permissions
find . \! -perm 750 -exec ls -ldb {} \;

# Check file ownership
cat /etc/passwd grep 533

# Find string into a FS
grep -rnw . -e "searchString"

# Replace string in vi-editor
$ %s/string1/string2/g

# Replace a string in nested FS
find . -type f xargs perl -pi -e 's;abc;xyz;g'
$ find . -name '*.xml' -o -name '*.conf' -o -name '*.properties' -o -name '*.sh' | xargs perl -pi -e 's?abc?xyz?g'

# Clear cache of server
sync;sync echo3 >/proc/sys/vm/drop_cache

# Check last login details
$ last | grep Mon | awk '{print $1}' | sort -u

# Temporarily increasing the mount size
mount -o remount,size=2G /tmp/

# Add user in Linux
useradd -u 920 -g usrgrp -d /home/punit -m -s /bin/ksh punit ;echo "password" | /usr/bin/passwd --stdin punit

# Add password-less user in Linux
useradd punit -g usrgrp -s /usr/sbin/nologin -f -1 -c "App user for APP-TESTING" -K PASS_MAX_DAYS=-1

# Add user as sudo user
$ vi /etc/sudoers 
$ username ALL=(ALL) NOPASSWD:ALL

# Fetch IP to put in a shell script
$ `ip addr list|grep "inet " |cut -d' ' -f6|cut -d/ -f1|grep -v "127.0.0.1"`
$ `/sbin/ifconfig -a | grep "inet addr" | awk '{print $2}' | grep -v "127.0.0.1"
 

PRODUCT-SPECIFIC COMMANDS

CHECK PRODUCTS INSTALLED ON A UNIX SERVER

UNIX/LINUX/AIX/HP-UX
$ df -P 2>/dev/null | grep "^/" | grep -v "/proc" | awk '{if ($1!~/:/){print "find " $NF " -xdev -name versionInfo.sh -o -name weblogic.jar -o -name catalina.jar -o -name webservd -o -name httpd -o -name jboss-management.jar -o -name domian.xml -o -name standalone.xml -o -name Agent.jar -o -name LLAWP -o -name server.cnf"}}' | sh | grep -v "^/var/tmp" | grep -v "^/tmp"

SOLARIS
$ df -l | grep "^/" | grep -v "/proc" | nawk -F "(" '{print "find " $1 " -xdev -name versionInfo.sh -o -name weblogic.jar -o -name catalina.jar -o -name webservd -o -name httpd -o -name jboss-management.jar -o -name domian.xml -o -name standalone.xml -o -name Agent.jar -o -name LLAWP -o -name server.cnf"}' | sh | grep -v
"^/var/tmp" | grep -v "^/tmp"