Learn a lot of Unix commands with ease here: http://www.grymoire.com/Unix
#
# Download Ubuntu organization file from here: here
# important commands
# command to know if the machine is 32bit or 64bit
file /sbin/init
# display contents of directory (both files and folder) with size and sort by size
du -sh * | sort -n
# trick to remove block comment (uncomment multiple lines) in bash script in vi editor
Press <ESC> and then do the following -- the following command will remove '#' from the beginning (^) of every line from line number 5 to 10.
:5,10s/^#//
# trick to give block comment (comment out multiple lines) in bash script in vi editor
Press <ESC> and then do the following -- the following command will add '#' to the beginning (^) of every line from line number 5 to 10.
:5,10s/^/#/
# trick to remove block comment (uncomment multiple lines) in bash script in vi editor
Press <ESC> and then do the following -- the following command will remove '#' from the beginning (^) of every line from line number 5 to 10.
:5,10s/^#//
# command to know if the RAM is DDR2 or DDR3
sudo dmidecode -t memory
sudo lshw -short -C memory
# you can also install 'hardware lister' in ubuntu that gives a gui, and run it via 'lshw-gtk' command
# command to get the disk usage
df -h
# command to get the file / folder size
# The Linux "du" (Disk Usage) is a standard Unix/Linux command, used to check the information
# of disk usage of files and directories on a machine. The "du" command has many parameter options
# that can be used to get the results in many formats. The "du" command also displays the files
# and directory sizes in a recursively manner.
#
# Using "-h" option with "du" command provides results in "Human Readable Format".
# Means you can see sizes in Bytes, Kilobytes, Megabytes, Gigabytes etc.
# To get the summary of a grand total disk usage size of an directory use the option "-s"
# Using "-a" flag with "du" command displays the disk usage of all the files and directories
du -sh
# command to check any resource exists (file / folder / symlink etc)
# possible options:
# -b filename :: Block special file
# -c filename :: Special character file
# -d directoryname :: Check for directory Existence
# -e filename :: Check for file existence, regardless of type (node, directory, socket, etc.)
# -f filename :: Check for regular file existence not a directory
# -G filename :: Check if file exists and is owned by effective group ID
# -G filename set-group-id :: True if file exists and is set-group-id
# -k filename :: Sticky bit
# -L filename :: Symbolic link
# -O filename :: True if file exists and is owned by the effective user id
# -r filename :: Check if file is a readable
# -S filename :: Check if file is socket
# -s filename :: Check if file is nonzero size
# -u filename :: Check if file set-user-id bit is set
# -w filename :: Check if file is writable
# -x filename :: Check if file is executable
if [ -e "$file" ]; then
echo "File exists"
else
echo "File does not exist"
fi
# command to check if a port is used by any process
netstat -nlp | grep 6080
lsof -ti:6080
# command to kill if any process is running on a port
kill -9 $(lsof -ti:6080)
netstat -nlp | grep 6080 | awk '{print ($7)}' | sed 's/\/.*//' | xargs kill -9
# command to kill if any process is running by a given name / pattern
ps -aef | grep src/server.ts | awk '{print $2}' | xargs kill -9
# command to change case of text to lower
echo "HELLO" | awk '{print tolower($0)}'
echo "HELLO" | tr '[:upper:]' '[:lower:]'
TXT="HELLO" && echo "${TXT,,}"
# command to change case of text to upper
echo "hello" | awk '{print toupper($0)}'
echo "hello" | tr '[:lower:]' '[:upper:]'
# command to check host-name and ip of a machine
host `hostname`
# commands to get external / public ip of a machine
curl ifconfig.me
curl ifconfig.me/all.json
curl -s http://checkip.dyndns.org/ | sed 's/[a-zA-Z<>/ :]//g'
dig -4 TXT +short o-o.myaddr.l.google.com @ns1.google.com
curl checkip.amazonaws.com
curl icanhazip.com
curl -4 ip.sb
# python code to get internal ip of a machine
import socket
def extract_ip():
st = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
try:
st.connect(('10.255.255.255', 1))
IP = st.getsockname()[0]
except Exception:
IP = '127.0.0.1'
finally:
st.close()
return IP
print(extract_ip())
# command to search text in files in a given directory
grep -inr "Text" folder/to/be/searched/
# command replace substring with another value in a string.
# "sed" command can be used for this purpose, the format of "sed" command is: sed 's/<text to be replaced>/<replacement text>/'
# "sed" command can also be used to replace texts in a file.
# The follwoing command replaces ":def" with ":xyz" and prints "abc:xyz" on screen (notice the usage of regex and how we piped the output to the next command).
echo abc:def | sed 's/:.*/:xyz/'
# More about "sed" command is here: http://www.grymoire.com/Unix/Sed.html
# the following command shows a good example of getting the id of the
# JAVA process that is using the port 10981, and then killing the process
# (notice the usage of escape '\' to feed special character '/' as input to "sed" command).
netstat -nlp | grep 10981 | awk '{print ($7)}' | sed 's/\/.*//' | xargs kill -9
# 'awk' is a very powerful command.
# The following command shows how to read the latest file from the current directory.
# ('NF' signifies the number of columns in a line)
ls -ltr | awk '{print $NF}' | tail -n 1 | xargs less
# Another way to read the latest file without 'awk'
ls -t1 | head -1 | xargs less
# way to run any process at background
nohup ./the_process_script.sh &
# extract last 100 lines from a file, and more from "tail" command
tail -100 filename
tail -f filename : display grows as the original file grows in real-time
# use "2>/dev/null" to channelize output to NULL device (nowhere)
# > file redirects stdout to file
# 1> file redirects stdout to file
# 2> file redirects stderr to file
# &> file redirects stdout and stderr to file
# nohup ./qpid-server > /dev/null 2>&1 & redirects stdout and stderr to NULL device and also returns back to command shell
du -sh * 2>/dev/null
# command to compress file / folder using zip / unzip (-e is to make password-protected zip)
zip -r filename.zip folder_to_zip_recursively
zip -er filename.zip folder_to_zip_recursively
unzip filename.zip
# command to compress file / folder using tar
tar -zcvf filename.tar.gz folder_to_zip_recursively/
tar -zxvf filename.tar.gz
# command to change mode of directory contents recursively after entering the directory
chmod 777 -R .
# command to find selective processes and kill them
ps -aef | grep domainA5 | awk '{print ($2)}' | xargs kill -9
# command to find selective files / folders and delete them
find . -name '*server1.lok' | xargs rm -rvf
# command to find count of lines where a text occurred in a file, and other good uses of "wc" command
cat thefile.txt | grep "Text to find" | wc -l
wc -l : Prints the number of lines in a file.
wc -w : prints the number of words in a file.
wc -c : Displays the count of bytes in a file.
wc -m : prints the count of characters from a file.
wc -L : prints only the length of the longest line in a file.
# command to copy / transfer files from one remote unix machine to another remote unix machine
scp FILE*.TXT u121212@remoteserver.ny.net:/apps/target
# command to change command prompt
set "PS1='$PWD #'" in "vi /home/calcadm/.bashrc" file to change command prompt
# use echo -e to print new line character
echo -e "\n\t..."
# Codes for symbols -- Search unicodes for symbol here: https://www.compart.com/en/unicode/
printf '\n \xF0\x9F\x8F\x83 \t This is Runner'
printf '\n \xF0\x9F\x8D\xBA \t This is Beer Mug'
printf '\n \xF0\x9F\x8D\xBB \t This is Clinking Beer Mugs'
printf '\n \xE2\x9B\x94 \t This is Failure Sign'
printf '\n \xF0\x9F\x92\x83 \t This is Dancer'
printf '\n \xE2\x9C\x8B \t This is Raised Hand'
printf '\n \xF0\x9F\x9A\x97 \t This is Car (side)'
printf '\n \xF0\x9F\x9A\x98 \t This is Car (front)'
printf '\n \xF0\x9F\x9B\xBA \t This is Auto Rickshaw'
printf '\n \xF0\x9F\x9A\xA2 \t This is Ship'
printf '\n \xF0\x9F\x9B\xA9 \t This is Airplane'
printf '\n \xF0\x9F\x9A\x80 \t This is Rocket'
printf '\n \xF0\x9F\x91\x80 \t This is Eyes'
printf '\n \xF0\x9F\x91\x81 \t This is Eye'
printf '\n \xF0\x9F\x91\x93 \t This is Eyeglasses'
printf '\n \xF0\x9F\x98\x8A \t This is Smily'
printf '\n \xF0\x9F\xA5\xBA \t This is Sad Face'
printf '\n \xF0\x9F\x98\xA2 \t This is Crying Face'
printf '\n \xF0\x9F\xA4\xA3 \t This is ROFL'
printf '\n \xF0\x9F\x93\x9C \t This is Scroll'
printf '\n \xF0\x9F\x9B\x92 \t This is Shopping Cart'
printf '\n \xF0\x9F\x94\xA5 \t This is Fire'
printf '\n \xF0\x9F\xA7\xA8 \t This is Firecracker'
printf '\n \xF0\x9F\x92\xA3 \t This is Bomb'
printf '\n \xF0\x9F\x8C\xBB \t This is Sunflower'
printf '\n \xF0\x9F\x8D\xB2 \t This is Food'
printf '\n \xF0\x9F\x8F\xA0 \t This is House'
printf '\n \xF0\x9F\x8F\xA2 \t This is Office'
printf '\n \xF0\x9F\x93\x9E \t This is Telephone'
printf '\n \xF0\x9F\x93\xB1 \t This is Mobile phone'
printf '\n \xF0\x9F\x92\xBB \t This is Laptop / Computer'
printf '\n \xF0\x9F\x92\xB0 \t This is Money'
printf '\n \xF0\x9F\x8E\xB2 \t This is Dice'
printf '\n \xF0\x9F\x92\xA1 \t This is a Electric Bulb'
printf '\n \xF0\x9F\x8D\xB0 \t This is a Cake'
printf '\n \xF0\x9F\x8E\x82 \t This is a Birthday Cake'
printf '\n \xF0\x9F\x92\xA4 \t This is a Sleeping Symbol'
printf '\n \xE2\x8F\xB1 \t This is a Stop Watch'
printf '\n \xE2\x8F\xB0 \t This is a Alarm Clock'
printf '\n \xF0\x9F\x9A\xA9 \t This is a Flag'
printf '\n \xE2\x9D\x8C \t This is a Negative Red Cross'
printf '\n \xF0\x9F\x94\xA8 \t This is a Hammer'
printf '\n \xF0\x9F\x9B\xA0 \t This is a Hammer and Wrench'
printf '\n \xF0\x9F\x90\x8D \t This is a Snake'
printf '\n \xF0\x9F\x91\x87 \t This is Finger Down'
printf '\n \xF0\x9F\x96\x90 \t This is Hi Five'
printf '\n \xF0\x9F\xA4\x9E \t This is Crossed Finger'
printf '\n \xF0\x9F\xA4\xAB \t This is Hush Up'
printf '\n \xF0\x9F\x91\x8D \t This is Thumbs Up'
printf '\n \xF0\x9F\x91\x8E \t This is Thumbs Down'
printf '\n \xF0\x9F\x94\x94 \t This is Bell'
printf '\n \xF0\x9F\x94\x95 \t This is No-Bell / No-ring / Silence'
printf '\n \xF0\x9F\xA7\xB9 \t This is Broom'
printf '\n \xF0\x9F\x8E\x89 \t This is Party'
printf '\n \xF0\x9F\x93\x94 \t This is Notebook'
printf '\n \xF0\x9F\x93\x96 \t This is Book'
printf '\n \xF0\x9F\x93\x9A \t This is Books'
printf '\n \xF0\x9F\x94\x96 \t This is Bookmark'
printf '\n \xF0\x9F\x93\x82 \t This is File / Folder'
printf '\n \xF0\x9F\x96\x8B \t This is Pen'
printf '\n \xF0\x9F\x94\x92 \t This is Closed Lock'
printf '\n \xF0\x9F\x94\x93 \t This is Opened Lock'
printf '\n \xF0\x9F\x94\x90 \t This is Lock with Key'
printf '\n \xF0\x9F\x9B\x91 \t This is Stop Sign'
# write a function in bash in single line / inline
# notice: how a ';' is to be added before closing '}'
print_message() { printf "\n \xF0\x9F\x9A\xA9 ${1}\n\n"; }
# convert DOS to UNIX file format
# PERL one liner (s/ for 'regex replacement', and /g for 'replace-all' or 'global-replace')
perl -pi -e 's/\r\n/\n/g' *.*
# 2 possible ways to find full path of currently executing script and its parent folder
# ${BASH_SOURCE[0]} (or, more simply, $BASH_SOURCE[1] ) contains the (potentially relative)
# path of the containing script in all invocation scenarios, notably also when the
# script is sourced, which is not true for "$0" -- thus prefer BASH_SOURCE[0] over "$0"
SCRIPT_PATH="$(cd "$(dirname "${BASH_SOURCE[0]}")" > /dev/null 2>&1; pwd -P)"
SCRIPT_PATH="$(cd "$(dirname "$0")" > /dev/null 2>&1; pwd -P)"
ROOT=$(dirname "$SCRIPT_PATH")
echo "Current script's path: "$SCRIPT_PATH
echo "Root path is: "$ROOT
# use $# to check the number of inputs, and $* to see list of inputs
#!/bin/sh
if [ $# -eq 0 ]
then
echo -e "\nNo input given.."
exit 1
else
echo -e "\nInputs given: "$#
echo -e "\nInputs are: "$*
COUNT=1;
for ip in $*
do
echo -e "\nInput_"$COUNT": "$ip
COUNT=$((COUNT+1))
done
# use '`' character to evaluate expression / command
NUM=0;
NUM=$((NUM+`cat abc.txt | grep "^A\^B" | wc -l`))
echo "Number of lines in abc.txt file that starts with string 'A^B' is: "$NUM
fi
# for loop example
#!/bin/sh
COUNTER=0
while [ $COUNTER -lt 10 ];
do
echo The counter is $COUNTER
let COUNTER=COUNTER+1
done
# another example
# working with input as files
#!/bin/sh
for file in $*
do
# read the file row by row
while read row
do
# use regex to chek if row starts with DEK or DEP
if [[ $row =~ ^DE(K|P) ]]
then
echo -e "\nRow: "$row
fi
done < $file
# the above line shows how to get input stream from file
done
# another example
# working with awk command
#!/bin/sh
line="this,is-a,comma,separated,line"
echo $line | awk -F , '{
# split "is-a" in two distinct words "is" and "a"
JOIN=split($2,ARRAY,"-")
IS=ARRAY[1]
A=ARRAY[1]
if(JOIN==2) {
A=ARRAY[2]
}
print "Got the \x27line\x27: "$1" "IS" "A" "$3" "$4" "$5"
}' >> output.txt
# the above line snds output to a text file
# the output will be Got the 'line': this is a comma separated line
# notice how we used \x27 for single-quote, as awk command's function was already wrapped in single-quote
# another example
# use '`' character to evaluate expression / command
#!/bin/sh
for file in $*
do
ROW_COUNT=0
while read row
do
ROW_COUNT=$((ROW_COUNT+1))
if [ `expr $ROW_ROW_COUNT % 500` -eq 0 ]
then
echo "Read rows: "$ROW_ROW_COUNT
fi
done < $file
done
# array in bash example
#!/bin/sh
COUNTER=0
VALUE=0
echo 'Populate array'
while [ $COUNTER -lt 10 ];
do
array[$COUNTER]=$VALUE
let COUNTER=COUNTER+1
let VALUE=VALUE+1
done
echo 'Array population done'
echo 'The array is: '${array[*]}
# commons script to different types of messages
#!/bin/bash
# call: print_message "hello world"
# prints: " 🚩 hello world"
print_message() {
printf "\n \xF0\x9F\x9A\xA9 ${1}\n\n"
}
# call: print_error "hello world"
# prints: " ⛔ hello world"
print_error() {
printf "\n \xE2\x9B\x94 ${1}\n\n"
}
# call: print_start "hello world"
# prints: " 🏃 hello world"
print_start() {
printf "\n \xF0\x9F\x8F\x83 ${1}\n\n"
}
# call: print_process_triggered "hello world"
# prints: " 🚀 hello world"
print_process_triggered() {
printf "\n \xF0\x9F\x9A\x80 ${1}\n\n"
}
# call: print_end "hello world"
# prints: " 🍺 hello world"
print_end() {
printf "\n \xF0\x9F\x8D\xBA ${1}\n\n"
}
# call: print_alert "hello world"
# prints: " ❌ hello world"
print_alert() {
printf "\n \xE2\x9D\x8C ${1}\n\n"
}
# call: print_comment "hello world"
# prints: " 💡 hello world"
print_comment() {
printf "\n \xF0\x9F\x92\xA1 ${1}\n\n"
}
# call: print_crash "hello world"
# prints: " 🔥 hello world"
print_crash() {
printf "\n \xF0\x9F\x94\xA5 ${1}\n\n"
}
# call: print_success "hello world"
# prints: " 👍 hello world"
print_success() {
printf "\n \xF0\x9F\x91\x8D ${1}\n\n"
}
# call: print_failure "hello world"
# prints: " 👎 hello world"
print_failure() {
printf "\n \xF0\x9F\x91\x8E ${1}\n\n"
}
# call: print_notification "hello world"
# prints: " 🔔 hello world"
print_notification() {
printf "\n \xF0\x9F\x94\x94 ${1}\n\n"
}
# call: print_sleeping "hello world"
# prints: " 💤 hello world"
print_sleeping() {
printf "\n \xF0\x9F\x92\xA4 ${1}\n\n"
}
# call: print_waiting "hello world"
# prints: " ⏱ hello world"
print_waiting() {
printf "\n \xE2\x8F\xB1 ${1}\n\n"
}
# call: print_timing "hello world"
# prints: " ⏰ hello world"
print_timing() {
printf "\n \xE2\x8F\xB0 ${1}\n\n"
}
# call: print_stats "hello world"
# prints: " 📜 hello world"
print_stats() {
printf "\n \xF0\x9F\x93\x9C ${1}\n\n"
}
# call: print_instructions "hello world"
# prints: " ✋ hello world"
print_instructions() {
printf "\n \xE2\x9C\x8B ${1}\n\n"
}
# call: print_cleanup "hello world"
# prints: " 🧹 hello world"
print_cleanup() {
printf "\n \xF0\x9F\xA7\xB9 ${1}\n\n"
}
# call: print_celebration "hello world"
# prints: " 🎉 hello world"
print_celebration() {
printf "\n \xF0\x9F\x8E\x89 ${1}\n\n"
}