#1# Find directory structure in tree format---------------------------------------------tree -d <directory name> #2# Version of OS , bit size. library installed---------------------------------------------------* Version of OScat /proc/versioncat /etc/*-releaselsb_release -a*Bit size on HP-UXgetconf KERNEL_BITS*Library Installed/sbin/ldconfig -pSearch for particular library/sbin/ldconfig -p | grep stdc++The list of compatible versions for above librarystrings /usr/lib/libstdc++.so.6 | grep LIBCXX #3# Kill process------------------ps -ef | grep -i `whoami` | grep -v grepps -ef | grep -i `whoami` | grep -v grep | grep -i apache | awk {'print $2'} | xargs kill -9ps -ef | grep -i `whoami` | grep -v grep | grep -i apache | grep -v tomcat | awk {'print $2'} | xargs kill -9ps -ef | grep -i `whoami` | grep -v grep | grep -v "/usr" #4# Find directory usage---------------------------du -ks *| sort -n -r | head -n 10-n : Numeric sort (sort command) -r : Reverse the result of comparisons (sort command) -n 10 : Display 10 largest file. If you want 20 largest file replace 10 with 20. (head command) Size in GBdu -smh <directory_name> #5# CPU performance----------------------sar [ options... ] [ <interval> [ <count> ] ]auaebs32 as oracle-> sar -Mu 5 5HP-UX auaebs32 B.11.11 U 9000/800 01/25/1010:36:58 cpu %usr %sys %wio %idle10:36:59 0 33 9 7 51 1 26 4 7 63 2 13 7 8 72 4 15 9 11 65 6 30 4 7 59 7 51 2 0 47 system 28 5 7 60 #6# Find no. of CPU (HP -UX)------------------------------sar -Mu 1 1 | awk 'END {print NR-5}' #7# Memory status/Huge page setup----------------------------------------egrep --colour 'Mem|Cache|Swap|Huge' /proc/meminfoegrep --colour 'Huge' /proc/meminfo #8# The actual make/brand name of the server---------------------------------------------auaebs32 as oracle-> model9000/800/rp8440 #9# Unzip a file with tgz extension--------------------------------------tar -zxf <file name> #10# Find printers installed on Unix server----------------------------------------------File where all printer information is stored./etc/printcaplpstat -s -- This gives the printer name and the ip associated with it.#11# Unzip a file in Linux------------------------------gzip -d <filename>.zip#12# stty------------stty erase \^h --- set the backspace as erase keystty -echo ---- this can be used in scripts where password will be entered as parameter. This will not show password on the prompt.eg.#!/bin/kshecho "Please enter the pwd:"stty -echoread pwdstty echoif [ "$pwd" == "secret" ]; thenecho "u have access"else echo "access denied"fi#13# Find and move the files in bulk--------------------------------------ls -ltr | grep -i 2012 | awk {'print $9'} | head -10 | xargs -I list mv list /stage/DW_INV_arc_backup/find /home/spenx/src -name "a1a2*txt" | xargs -n 1 dirname | xargs -I list mv list /home/spenx/dst/#14# Find tree structure under a directory------------------------------------------------find . -type d -print | sed -e 's;[^/]*/;|____;g;s;____|; |;g'or use 'tree' command if its installed#15# Zip a directory on uniz----------------------------------zip -r filename.zip directory_to_zip_recursively#16# Enable history on Unix prompt----------------------------------------You have to manually enable this feature. To do this add the following two line on your ~/.profile file and create the .sh_history file using touch command. Then log in again. You will see it is working.HISTFILE=/.sh_historyexport HISTFILE#17# Command line ---------------------PS1="[\u@\h |\t| \W]\$"PS1='$LOGNAME'@`uname -n`:'$PWD> '#18# use any key as deletion key---------------------------------------Enter this at shell promptCode:stty erase <hit the key for deletion>#19# convert into lower case------------------------------I would use the bash internal typeset or declare command to define a lowercase variable.$ typeset -l lcase$ lcase="LoWeR cAsE"$ echo $lcaselower caseThe chars are lowered when the value is set, not when you typeset the variable. So it is better to typeset at the beginning of the script. For uppercase you can typeset -u.$ typeset -u ucase$ ucase="Upper cAsE"$ echo $ucaseUPPER CASE#20# Disable CTLR+C in shell script-------------------------------------# Signal 2 is Ctrl+C# Okay disable it:trap '' 2command1command2command3# Enable Ctrl+Ctrap 2#21# Size of a directory--------------------------du -sh /dir_namedu -sh *#22# Upper case to lower case--------------------------------export lnewsid=`echo $newsid |tr '[A-Z]' '[a-z]'`#22# memory used per process------------------------------ps -e -orss=,args= | sort -b -k1,1n | pr -TW$COLUMNS | grep -i MaxPermSize| grep -i soapkms | grep -v grep#23# No. of files under each directory----------------------------------------find $APPL_TOP -type d -name bin -print0 | while read -d '' -r dir; do files=("$dir"/*) printf "%5d files in directory %s\n" "${#files[@]}" "$dir"donefind -maxdepth 1 -type d | while read -r dir; do printf "%s:\t" "$dir"; find "$dir" -type f | wc -l; done#23# Unzip multiple file-----------------------Option # 1 : unzip multiple files using single quote (short version)Type the command as follows:$ unzip ‘*.zip’Note that *.zip word is put in between two single quote, so that shell will not recognize it as a wild card character.Option # 2 : unzip multiple files using shell for loop (long version)You can also use for loop as follows:$ for z in *.zip; do unzip $z; done#24# List and copy files to a folder--------------------------------------ls -ltr | grep -i "Feb 11" | awk {'print $9'}| xargs -i cp {} /tmp/ls -ltr | grep -i "Jul 2" | grep -v 2014 |awk {'print $9'}| xargs -i cp -p {} /stage/nalinodb12/asm/#25# Find files one day ago------------------------------find -d "1 day ago"#26# Change timestamp of a file---------------------------------touch -d "2 hours ago" filenametouch -t 201501141920 filenameYYYYMMDDHHMMtouch -t 201501141920#27# Change date minutes/hours ago-----------------------------------d_now=`date "+%H"`date -d '3hours ago' "+%H"date -d '1 hour ago'http://www.thegeekstuff.com/2013/05/date-command-examples#28#Find and replace a whole string with character ---------------------------------------------------Mark here, instead of using "/" as the delimiter, we are using "!" as delimiter%s!old_character_string!new_character_string!ge.g.:%s!services/B2B!services/Testing!g#29# Mailx -----------------Mail CC , TO, FROM -c $EMAIL_CC $EMAIL_TO -- -f $EMAIL_FROM***************************************************#!/bin/bash# Read all unread emailsMAIL_INDEX=$(printf 'h a\nq\n' | mail | egrep -o '[0-9]* unread' | awk '{print $1}')markAllRead=for (( i=1; i<=$MAIL_INDEX; i++ ))do markAllRead=$markAllRead"p $i\n"donemarkAllRead=$markAllRead"q\n"printf "$markAllRead" | mail*************************************************#30 If clause--------------if [ $email_sent -ne 0 ]then echo "mail sent"else echo "mail not sent"fi#31# Commands in "top"------------------------Press (Shift+O) to Sort field via field letter, for example press ‘a‘ letter to sort process with PID (Process ID)Use top command with ‘u‘ option will display specific User process details. top -u oraclePress ‘z‘ option in running top command will display running process in color which may help you to identified running process easily.Press ‘c‘ option in running top command, it will display absolute path of running process.By default screen refresh interval is 3.0 seconds, same can be change pressing ‘d‘ option in running top command and change it as desired as shown below.You can kill a process after finding PID of process by pressing ‘k‘ option in running top command without exiting from top window as shown below.Press (Shift+P) to sort processes as per CPU utilization.You can use ‘r‘ option to change the priority of the process also called Renice.Top output keep refreshing until you press ‘q‘. With below command top command will automatically exit after 10 number of repetition.#32# Find and cut command----------------------------------Normal Find command Outputfind . -mtime -7 -exec ls -ltr {} \; | awk {'print $6" "$7" "$8" " $9'}Oct 19 07:11 ./.grid_nalinodb10.binOct 19 07:13 ./.grid_nalinodb11.binOct 19 07:13 ./.grid_nalinodb12.binOct 19 07:12 ./.grid_nalinodb14.binOct 18 23:50 ./.otm_nalinoap06.binOct 18 23:50 ./.otm_nalinoap07.binOct 18 23:52 ./.oracle_nalingrd01.binRemove sign "./."find . -mtime -7 -exec ls -ltr {} \; | awk {'print $6" "$7" "$8" " $9'} |tr -d "./."Remove last common test "bin" from output find . -mtime -7 -exec ls -ltr {} \; | awk {'print $6" "$7" "$8" " $9'} |tr -d "./." | rev | cut -c 4- | revRemove sign "_" from the outputfind . -mtime -7 -exec ls -ltr {} \; | awk {'print $6" "$7" "$8" " $9'} |tr -d "./." | rev | cut -c 4- | rev | tr -s '_' ' 'Oct 19 07:11 grid nalinodb10Oct 19 07:13 grid nalinodb11Oct 19 07:13 grid nalinodb12Oct 19 07:12 grid nalinodb14Oct 18 23:50 otm nalinoap06Oct 18 23:50 otm nalinoap07Oct 18 23:52 oracle nalingrd01#33# Find and replace a character in all files under directory-------------------------------------------------------------------find ./ -type f -exec sed -i -e 's/apple/orange/g' {} \;#34# Commenting multiple lines on VI mode---------------------------------------------You can do it with the following command:for comment:66,70s/^/#for uncomment::66,70s/^#/#35# Find and move the files-------------------------------find . -name "AdminServer.log*" -mtime +1 | xargs -I list mv list /stage/soa/backup/soawprod/nalinoap18_Admin_logs/#36# To write a range of lines in vi mode to another file you can use----------------------------------------------------------------------:<n>,<m> w filename#37# Convert a file to HTML format-------------------------------------awk 'BEGIN{print "<table>"} {print "<tr>";for(i=1;i<=NF;i++)print "<td>" $i"</td>";print "</tr>"} END{print "</table>"}' <filename>#38# Run cron job on first Monday of every month--------------------------------------------------0 12 1-7 * * [ "$(date '+\%a')" = "Mon" ] && < command>#39# list ls -ltr for a particular year------------------------------------------ls -ltrh | awk '$6 == "2014"'Look out for the column where the year is mentioned#40# To avoid indent in vi editor------------------------------------To avoid the indent in vi editor:set noautoindent#41# Grep mutiple string---------------------------egrep 'string1|string2' filegrep 'string\|string2' file#42# Changing a character in a particular line/s-------------------------------------------------Go to that line in which correction need to be made:s/old/new/gNote % is removed from substitute command#43# Replace All Lines Between line N1 and line N2 --------------------------------------------------------------:{START-n},{END-n}s/word1/word2/gE.g:-Find ‘UNIX’ and replace with ‘Linux’ all lines between line 100 and line 250, enter::100,200s/UNIX/Linux/g#44# Capturing top output to a file-------------------------------------top -n 1 -b > top-output.txtOption -n 1 indicates that only one iteration of the top command should be executed. -b indicates to run the script iin batch, otherwise without it, it will throw junk charactersgrep --line-bufferedtail -f log.txt | egrep --line-buffered 'WARN|ERROR' | tee filtered_output.txt#45# Find class file version on unix----------------------------------------major minor Java platform version 45 3 1.045 3 1.146 0 1.247 0 1.348 0 1.449 0 1.550 0 1.651 0 1.752 0 1.8javap -verbose MyClass | grep "major"javap -verbose MyClass | grep "minor"Dont use the extension .class file using the command#46# Memory usage for previous day--------------------------------------Usually, sysstat, which provides a sar command, keeps logs in /var/log/sysstat/ or /var/log/sa/ with filenames such as /var/log/sysstat/sadd where dd is a numeric value for the day of the month (starting at 01). By default, the file from the current day is used; however, you can change the file that is used with the -f command line switch. check CPU for a daysar -f /var/log/sa/sa26 -s 19:00:00 -e 22:00:00 -u 600-f < file name> sa26 is for 26th day of the month-s start time as in HH:MM:SS-e end time as in HH:MM:SS-u interval 600 is 600 seconds or 10 minscheck mem for a daysar -r -f /var/log/sa/sa26 -s 19:00:00 -e 22:00:00 -u 600check I/O stat for a daysar -b -f /var/log/sa/sa26 -s 19:00:00 -e 22:00:00 -u 600Check Reports run queue and load averagesar -q -f /var/log/sa/sa26 -s 19:00:00 -e 22:00:00 -u 600All:-sar -r -b -q -f /var/log/sa/sa26 -s 19:00:00 -e 22:00:00 -u 600Source : http://www.thegeekstuff.com/2011/03/sar-examples/ http://www.computerhope.com/unix/usar.htm#47# Attach multiple files in mailing--------------------------------------------echo -e "Hi All,\n\nPlease find the attached report.$MAIL_SUB \n\nThank you,\nAdmin Team.\n\nFrom: Admin.Webmethods@acco.com" | mutt -s "$MAIL_SUB" $( printf -- '-a %q ' *"$FILE_NAME"* ) $MAIL_IDHere file name was described as FILE_NAME="ASAP"#48# SFTP using password in the script------------------------------------------lftp sftp://acco_prod:qP498ME1vZ2wtnD@207.108.145.135 -e "ls;bye"lftp sftp://acco_prod:qP498ME1vZ2wtnD@207.108.145.135 #48# Replace character by a character on command line-------------------------------------------------------tr -s '_' ' ' #49# Double quotes within double quotes-----------------------------------------mystr="say \"hi\""echo 'abc'\''abc'echo "abc"\""abc"echo ""\"Hello"\""#50# A variable passed in awk+print command---------------------------------------------awk -v values="$variable_name"ac_num=111awk -v values="$ac_num" {'print $1"_"$2"_"values"_"$4"_"$5"_"$6"_"$7"_"$8'}`#51# If command comparing multiple variable---------------------------------------------if [[ $file_path = "/$SID_L/appl/XXAB/11.5.0"* ]] || [[ $file_path = "/$SID_L/comn/admin/log"* ]] || [[ $file_path = "/$SID_L/comn/admin/out"* ]]; then#52# From in MUTT command---------------------------If we want to change the senders name and email, then we need to Create a file in that particular user’s home directory.cat .muttrc#53# Replace using sed-----------------------Replace / using sed with spacesed -e 's:/::g'#54# Run date command on multiple server-----------------------------------------ssh nalinodb10 date;ssh nalinodb11 date; ssh nalinodb12 date#55# Adding a variable in sed----------------------------------d1=`date -d '1 day ago' +"%Y%m%d"`cat WellsFargo_820__2017Aug03_18_09_27_920.txt | sed 's/.*DA\(.*\)'"${d1}"'/\1/' | cut -c2-11 #56# Edit (find-and-replace) in a file without opening it----------------------------------------------------------$cat test_fileThis is a test file.Today is Monday. $vi -c "%s/Monday/Tuesday/g|wq" test_file"test_file" 2 lines, 38 characters "test_file" 2 lines, 39 charactersvi -c "%s/$old/$new/g|wq" test_file#57# How to Run or Repeat a Linux Command Every X Seconds ------------------------------------------------------------Print in next linefor i in {1..10}; do echo -n "This is a test in loop $i "; date ; sleep 1; donePrint output next stepfor i in {1..10}; do echo -e ".\c"; sleep 1; done ; echo -e "\n" #58# SFTP and port-----------------------sftp -oPort=XXXX username@servername #59# Rename multiple files-----------------------------rename 's/^/MyPrefix_/' * document.pdf renamed to MyPrefix_document.pdf rename 's/^CD RIP //' *CD RIP 01 Song.mp3 to 01 Song.mp3 #60# Find command----------------------------- find $ARCH -name "*.arc" -mtime +1 -exec ll {} \; Specify selection of the files based on three Unix timestamps: the last time a files's "access time", "file status" and "modification time". n is time interval -- an integer with optional sign. It is measured in 24-hour periods (days) or minutes counted from the current moment.n: If the integer n does not have sign this means exactly n 24-hour periods (days) ago, 0 means today. +n: if it has plus sing, then it means "more then n 24-hour periods (days) ago", or older then n, -n: if it has the minus sign, then it means less than n 24-hour periods (days) ago (-n), or younger then n. It's evident that -1, and 0 are the same and both means "today".
--> How to find a "word" or pattern in all files in a directory & subdirectories find . -name "*" -exec grep -l "<pattern>" {} \; for example I want to search for word belowfind . -name "*.log" -exec grep -l "bytes exceeds the configured maximum of" {} \; --> This command will also say where the entry is there for the pattern find . -type f -print | xargs grep "<pattern>" --> Find and grep command together find . -xdev -mtime -3 -exec ll -ltr {} \; | grep -i "<date>" find . -xdev -mtime -3 -exec ll -ltr {} \; | grep -i "Oct 23" --> Find and AWK command together find /abprod/appl/XXAB/11.5.0 -type f -mtime -7 -exec ls -ltr {} \; | awk '{ print $6 " " $7 " " $8 " " $9 }' --> To find files modified in the last 40mins find . -mmin -40 --> Find and replace a character in all files under directory find ./ -type f -exec sed -i -e 's/apple/orange/g' {} \; http://www.thegeekstuff.com/2009/06/15-practical-unix-linux-find-command-examples-part-2/ --> Find a directory find . -type d -name "OracleBIPresentationServicesComponent" -ls --> Find and move find . -name "AdminServer.log*" -mtime +1 | xargs -I list mv list /stage/soa/backup/soawprod/nalinoap18_Admin_logs/ --> Find the count of files under each directory find . -type d -print0 | while read -d '' -r dir; do files=("$dir"/*) printf "%5d files in directory %s\n" "${#files[@]}" "$dir"done --> Find file more than (n) size -size n[cwbkMG]
`b' for 512-byte blocks (this is the default if no suffix is used) `c' for bytes `w' for two-byte words `k' for Kilobytes (units of 1024 bytes) `M' for Megabytes (units of 1048576 bytes) `G' for Gigabytes (units of 1073741824 bytes)
#61# Find inode value in a directory-------------------------------------- df -i /directory_path #62# Change password on command line---------------------------------------echo -e "oldpassword\nnewpasswd123\nnewpasswd123" | passwd #63# Generate public private key pair----------------------------------------$ ssh-keygen -t rsaGenerating public/private rsa key pair.Enter file in which to save the key (/home/apptest/.ssh/id_rsa): bomc (type key file name)Enter passphrase (empty for no passphrase): (Simple hit Enter for passwordless auth)Enter same passphrase again: (Simple hit Enter for passwordless auth) Your identification has been saved in bomc.Your public key has been saved in bomc.pub.The key fingerprint is:98:b2:20:ac:40:63:95:a4:3f:e4:af:8b:da:2f:29:5d apptest@nalinoes02 #64# Remove whitespace from output------------------------------------------In order to wipe all whitespace including newlines you can try:cat file.txt | tr -d " \t\n\r" You can also use the character classes defined by tr cat file.txt | tr -d "[:space:]"For example, in order to wipe just horizontal white space:cat file.txt | tr -d "[:blank:]" #65# Find space used by process in a directory------------------------------------------------lsof /tmp/ #66# Add a path to PATH variable so that files under it is read from anywhere---------------------------------------------------------------------------------export PATH=/home/oraprod/scripts:$PATH #67# How to get a response from any URL or check if any URL is working from a unix prompt.-----------------------------------------------------------------------------------------curl -Is http://www.yourURL.com | head -1 You can try this command to check any URL.Status code 200 OK means that the request has succeeded and the URL is reachable. You can also test URL availability and get the response code using telnet command. 80 is the port number. telnet www.yourURL.com 80 #68# Last date of the previous month-----------------------------------------date -d "$(date +"%Y-%m-01") - 1 day" +"%d"date -d "$(date +"%Y-%m-01") - 1 day" +"%F" #69# Date in MM_DD_YYYY_H_M format------------------------------------------d2=`date +"%m_%d_%Y_%H_%M"` #70# Find packages installed on server--------------------------------------------rpm -qayum list installed #71# Check Disk Partitions and Usage------------------------------------------ Run as root ( ** best viewed report ) fdisk -l --> print the partition table of all mounted block devices:sfdisk -l --> sfdisk offers extra features not available in fdiskcfdisk --> printing and managing disk partitions. parted -l --> displaying and manipulating disk partitions.lsblk --> prints information including name, type, mountpoint concerning all available or particular mounted block device(s) ** df -hT --> -hT switch enables reporting of the disk size, used space, available space and used space percentages in human-readable format. ** #72# Find which partition is mapped with oracle asmdisk----------------------------------------------------------- Run blkid as root user Ref: https://deveshdba.wordpress.com/2016/09/27/find-which-partition-is-mapped-with-oracle-asmdisk/ blkid |grep oracleasm #73# Check if any swap in swap out activity happening on server------------------------------------------------------------------- vmstat -tw 2 20 #74# avoid the indent in vi editor----------------------------------------------:set noautoindent #75# SSH login without password-----------------------------From A servercd .sshssh-keygenssh-copy-id b@B try connecting ssh b@B Example:- [oracle@bmvhe07a .ssh]$ ssh-copy-id oracle@bmvhe08a/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/home/oracle/.ssh/id_rsa.pub"/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keysoracle@bmvhe08a's password: Number of key(s) added: 1 Now try logging into the machine, with: "ssh 'oracle@bmvhe08a'"and check to make sure that only the key(s) you wanted were added. Second methodFrom A servercd .sshssh-keygenssh-copy-id b@B try connecting ssh b@B Example:- [oracle@bmvhe07a .ssh]$ ssh-copy-id oracle@bmvhe08a/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/home/oracle/.ssh/id_rsa.pub"/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keysoracle@bmvhe08a's password: Number of key(s) added: 1 Now try logging into the machine, with: "ssh 'oracle@bmvhe08a'"and check to make sure that only the key(s) you wanted were added.