Basic Unix Commands
Find all files within a directory and it’s subdirectories.
find . -type f
Find files containing specific pattern.
find . -type f | xargs grep -i “javac”
Find all files whose name matches a pattern.
find . -type f -name “” | xargs grep -i “14:19:54”
Sort files by size:
find /home -size +9999999c -xdev -exec ls -als {} \;|sort -k 6,6nr|more
Find and replace pattern inside a file.
:%s/op169/op142/g
Search pattern in all files, in all sub-directories:
du -a|awk ‘{print $2}’|xargs grep “LOG_DIR=” > /tmp/1.log
Check current running process
ps -eo pid,comm,lstart,etime,time,args | grep veeamconfig
List all non-commented lines from a file.
egrep -v “^#|^$” plugin_bestprd.conf
Run script as nohup (background mode)
nohup script.sh > /dev/null 2>&1 &
Immutable file (unchangeable file)
lsattr veeamsnapdata_stretch_{2fd8457f-6a8f-48ff-b279-d946b106cc04}_#0
chattr -i veeamsnapdata_stretch_{2fd8457f-6a8f-48ff-b279-d946b106cc04}_#0
Check Resident Set Size of a process:
ps -o pid,user,vsz,rss,comm,args
Broadcast message to logged in user on terminal.
wall “System will go down for 2 hours maintenance at 13:00 PM”
Send page/message to phone/pager:
/usr/localcw/bin/beep $Pager $Message
Search with exclude specific match/patterns:
grep -v -e ‘ORA-39083’ -e ‘ORA-01917’ imp_struct_wpacprod_20120508.log | grep -i ORA-
Mail as attachment:
uuencode drop_synonyms_classic.zip drop_synonyms_classic.zip | mail -s ‘DROP SYN LOGS’ ravinder.rg.gupta@outlook.com
Gzip a file:
gzip -c ggserr.log > ggserr.log.gz_bkp
Count items in output:
ls -1 | wc -l
List and Delete file older than 30 days:
find /opt/app/p1c1m2069/pmtool/ -mtime +30 -exec ls -lrt {} \;
find /opt/app/p1c1m2069/pmtool/ -mtime +30 -exec rm {} \;
Check for Space Usage:
du /backup/* | sort -n -r | more
du -csh /apps/* | sort -n -r | more
du -csh –block-size=1G /apps/* | sort -n -r | more
Truncate a file content:
truncate -s 0 access.log
List duplicates in a file:
awk ‘array[$0]++’ XBFWRCIRCUITDETAILS_T_2015-05-15_15-11-17_data_SE.dsv
Remove duplicates from a file:
awk ‘!array[$0]++’ XBFWRCIRCUITDETAILS_T_2015-05-15_15-11-17_data_SE.dsv > temp.file
cat temp.file > XBFWRCIRCUITDETAILS_T_2015-05-15_15-11-17_data_SE.dsv
Or,
for i in *.dsv
do
awk ‘!array[$0]++’ $i > temp.file
cat temp.file > $i
rm -f temp.file
done
List Uniques:
ls NOTI2015-05-22SE*| cut -d “-” -f1 | awk ‘!array[$0]++’ | wc -l
List of groups:
cut -d: -f1 /etc/group
List members of a group:
groups repadm
Search group details:
awk -F: ‘/^sybase/ {print $4;}’ /etc/group
Append at the end of each file:
for i in *.properties
do
echo “writer.metacols.opcode.updatepk.chars=U” >> $i
echo “dsvwriter.metacols.opcode.updatepk.chars=U” >> $i
echo “” >> $i
done
Remove files older than timestamp:
find /opt/app/p1edg1d3/ggstrail02/flatfiles_send -type f -name “.dsv” -mmin +640 -exec ls -l {} \; find /opt/app/p1edg1d3/ggstrail02/flatfiles_send -type f -name “.dsv” -mmin +640 -exec rm -f {} \;
/usr/bin/find /apps/CGIADM/Files/ADMPRD01 -maxdepth 1 -type d | find -type f mvls *.dsv | grep -i "xbfwrcircuitdetails_t"temp_dsv
Replace pattern in all matching files:
perl -pi -e ‘s/APPLICATION_READER/CMAS_READ/g’ *.sql
Compare two files:
grep -vf file1 file2
diff file1 file2
Sort file contents
sort prod_idx.log -o prod_idx.sorted
Get total file size matching particular file name patterns.
find . -type f -name ‘*.out’ -exec du -s {} + | grep total
Insert a string “go” after each line in the file.
awk ‘ {print;} NR % 1 == 0 { print “go”; }’ 1.sql> 2.sql
Find files with setuid set as permission
find /grid/12.1.0 -perm -4000 -exec ls -ldb {} \;
Send mail using smtp specification:
mailx -v -s “test mail” -S smtp=smtp://156.3.123.34 -S from=”oracle@mydomain.com” gupta_ravinder@xyz.com
Create file of specific size:
dd if=/dev/zero of=output.dat bs=10M count=1024
Check group exists in bash
getent group asmadmin
Troubleshooting ulimit :
–check number of processes running for user
# ps -u advadmin -L | wc -l
–check process limit
ulimit -a
–check login errors
cat /var/log/secure
–change nproc in /etc/security/limits.conf
–change nproc value
/etc/security/limits.d/20-nproc.conf
Heap Size (linux)
java -XX:+PrintFlagsFinal -version | grep HeapSize
Add timestamp in history
vi /etc/bashrc
export HISTTIMEFORMAT=”%Y-%m-%d – %H:%M:%S “
Passwordless ssh between two servers
./sshUserSetup.sh -user oracle -hosts “host1.com host2.com” -noPromptPassphrase
Check if port is listening
nc -l <port>
Postfix mailbox size limit
postconf -d | grep mailbox_size_limit mailbox_size_limit = 51200000
–to increase the size
/etc/postfix/main.cf
mailbox_size_limit = 5120000000
Version locked packages
cat /etc/yum/pluginconf.d/versionlock.list
Get hardware info
yum install lshw
lshw
lshw -short
Xauth commands
–Xauth after sudo or su
xauth add $(xauth -f ~7.RGupta/.Xauthority list|tail -1)xauth list|grep
uname -n
xauth add $DISPLAY . hexkeyDISPLAY=:0; export DISPLAY
xauth add $DISPLAY . 9c956431bf069aabe2c16acecb1009a4
xauth remove $DISPLAY
System Performance Monitoring Commands
System resources:
cat /proc/cpuinfo
cat /proc/meminfoawk -F: ‘/^physical/ && !ID[$2] { P++; ID[$2]=1 }; /^cpu cores/ { CORES=$2 }; END { print CORES*P }’ /proc/cpuinfo
Disk Statistics:
iostat -x 2 5
iotop
System Activity Report, SAR
–basic system activity report
sar–system activity report per device
sar -d–show memory usage
sar -r–statistics for a particular day of the month
–here 11 is day of the month
sar -f /var/log/sa/sa11–sar metrics for a particular time period
sar -f /var/log/sa/sa11 -s 09:30:00 -e 10:00:00–sar metrics for a particular time period (With option A)
–display all metrics collected by SAR during the time period
sar -f /var/log/sa/sa11 -s 08:00:00 -e 10:25:00 -A > sar_0800_1030.log
sar -f /var/log/sa/sa22 -s 14:00:00 -e 15:30:00 -A > sar_1400_1530.logsar -f /var/log/sa/sa20 -s 05:10:00 -e 05:12:00 -A > sar_0500_0515.log
–network statistics
sar -n DEV
Disks and File System Commands
File System mount commands:
mount -o remount,discard /dev/mapper/mpathn1 /backup
Mount CIFS NAS Share LINUX
//10.123.12.123/VEEAMAgent /nas_storeonce cifs uid=640,gid=640,credentials=/root/cifs.creds,file_mode=0777,dir_mode=0777 0 0
mount -t cifs -o credentials=/root/cifs.creds,uid=640,gid=640,file_mode=0777,dir_mode=0777 // //10.123.12.123 /VEEAMAgent /nas_storeonce
mount -t cifs -o credentials=/root/cifs.creds,file_mode=0777,dir_mode=0777 // //10.123.12.123 /VEEAMAgent /nas_storeonce
mount -t cifs -o username=svcsobkup //10.123.12.123 /VEEAMAgent /nas_storeoncemount -t cifs -o credentials=/root/.smbcredential,uid=640,gid=640,file_mode=0777,dir_mode=0777 //10.123.12.123/VEEAMAgent /tmp/veeam/10.123.12.123VEEAMAgent
mount -t cifs -o credentials=/root/.smbcredential,uid=640,gid=640,file_mode=0777,dir_mode=0777 //10.123.12.123/VEEAMAgent /tmp/veeam/ 10.123.12.123VEEAMAgent
Network Related Commands
Test UDP Port
netcat -u host port
Check port without telnet:
nc -zv 127.0.0.1 80
nc -zv 127.0.0.1 22 80 8080
nc -zv 127.0.0.1 20-30
Linux Repository Commands
Disable a repository
subscription-manager repos –disable=rhel-7-server-satellite-tools-6.2-rpms
cd /etc/yum.repos.d
Manage Linux Registration
–delete subscription
subscription-manager list –consumed
subscription-manager remove –serial=3507154374683028506
subscription-manager status Or,
subscription-manager remove –all
subscription-manager unregister
subscription-manager clean–attaching subscription
subscription-manager attach –pool=8a76858e652a59f9016562bf558f0212
subscription-manager list –available
subscription-manager attach –auto
Registering subscription
subscription-manager register –username –password –auto-attach
subscription-manager register –org=”Default_Organization” –activationkey=”NPDBAK”
System Auditing Commands
Check package modification time
rpm -qa –last | grep kernel
Check for last login (works on HP UX):
cat wtmp | /usr/sbin/acct/fwtmp |more
Service Management Commands
List all services
systemctl -r –type service –all | more
Linux Package Management Commands
Rolling back a package
yum history list all
yum history info 8
yum history undo 8
yum history summary kernel
yum history rollback 1
yum history info kernel
Miscellaneous Commands
du -a|grep “.sh”|awk ‘{print $2}’|xargs cat -|grep “rm “
Random Number Generator
sleep
shuf -i 1-10 -n 1s; exitshuf -i 1-10 -n 1
Systemctl Units
systemctl list-units -all| grep Jboss
systemctl is-active JbossDomain.service
systemctl cat JbossDomain.service
systemctl list-dependencies JbossDomain.service
systemctl show JbossDomain.service
find . -type f | xargs grep -i “jboss”
/etc/systemd/system/multi-user.target.wants
find . -print | grep -i jboss
–Linux Devices
cd /dev/disk/by-label
–map dm (device mapper) with disk
find /dev/ -name “dm-*” -exec readlink -n {} \; -exec echo ” –>” {} \;
dmsetup info -c
–disable multicast
for i inifconfig |grep -i multi | cut -d ":" -f1
do
echo “ifconfig $i -multicast”
ifconfig $i -multicast
done
–Check ILO from LINUX
dmidecode | grep -i Proliant
To discover the iLO IP address, check it with ipmitool
hponcfg -w iLO_ouput.out