UNIX Cheat Sheet -Command List for UNIX server operation

untitled

Unix is a pretty awesome thing if handled carefully. Compared to Windows, it is far more robust and secure. But lack of GUI is the only challenge for several Unix/Linux OS flavors (except Linux Mint of-course). So command line is the only good option which can manipulate environment. In this post, I’m summing up all the necessary UNIX commands that are been used mostly in a daily basis. Hope you will enjoy this.

====================================================

COMMON COMMANDS

====================================================

cd –> to change the directory
ps -ef –> to check the status of the process
Every process has a parent & child process ID
du -hs –> to check the file size
df -kah –> to check the disk space in the file system.
uname -a –> gives the information of the hostname,OS and the version of the OS as in sun solaris sparc v10
uname -n –> hostname
uname -r –> release
prstat -a –> gives the information of CPu usage
iostat -eE —> gives the information fo hard drive
netstat -an | grep <<Port no.>> gives the conneciotn count
telnet <<Server IP>> <<Port server is listening to>>
hostname –> gives us the hostname of the machine
ssh -l <<username>> <<Machine ip>>
ssh <<username>>@<<Machine IP>>
scp <<filename>> <<username>>@<<Machine IP>>:<<destination where the file needs to be copied>>
> –> O/P’s to a file
< –> Takes input from a file
>> —> Appends to a file
cp <<File1>> <<File2>> –> to copy the contents of File 1 to File 2
mv <<File1>> <<File2>> —> to mv the components from File 1 to File 2
mv <<Old_Name>> <<New_Name>> –> Renaimg a file
tar -cvf <<tar file name >> <<File1>> <<File2>> <<File3>> —-> to compress the components of File1,File2 & File3 to the tar file given as the first argument in the command line
tar -tvf <<tar file name>> –> to view the componenets of tar file
tar -xvf <<tar file name>> –> to extratct the componenets fo tar file

gzip <<File1>> <<File2>> <<File3>> <<gzip file name>> —> to zip several file into a single file
gzip -l <<gzip file name>> –> to check the percentage of deflation for all the files gzipped to file
gzip -d <<gzip file name>> —> to unzip a gzip file
gunzip <<gzip file name>> —> to unzip a gzip file

 

====================================================

GREP COMMAND
====================================================

grep <<Pattern>> <<file in which the pattern is to be searched>>

====================================================

OPTIONS IN GREP COMMAND
====================================================

grep -i <<Pattern>> <<File in which the pattern is to be searched>> —-> “i” refers to the ignore case option which ignores the case of the pattern searched
grep -n <<Pattern>> <<File in which the pattern is to be searched>> —-> “n” prints all the line nos. at which the pattern is found
grep -l <<Pattern>> <<File1>> <<File2>> <<File3>> —-> “l” which lists files having the searched pattern
grep -c <<Pattern>> <<File1>> <<File2>> <<File3>> —-> “c” gives us the count of the searched pattern in all the files.
grep -v <<pattern>> <<File in which the pattern is to be searched>> —> gives us everthing except the pattern given as the command line argument.
grep -w <<Pattern>> <<File in which the pattern is to be searched>> —> gives us only the searched pattern

====================================================

SED COMMAND (Scripting Editor Command)
====================================================

sed -n ‘<<Beginning Line no.>>,<<End line no.>>p’ <<File name>> —> Prints all lines from the beginning till the end line no. specified in the sed command
sed -n ‘<<Line no.>>p’ <<File name>> —> Prints the line no. specified in the coammnd line
sed ‘<<Beginning Line no.>>,<<End Line no.>>d’ <<File name>>—-> Deletes all from the beginning till the end(Beginnig and End refers to line nos. given as command line arguments)
sed ‘<<Line no.>>d’ <<File name>> —> Deletes line no. given as the command line argument.

sed ‘n;d’ <<File name>> —> removes all even numbered lines from the files

sed -e :a -e ‘$!N;s/\n<<Pattern to be replaced>>/<<New Pattern>>;ta’ -e ‘P;D’ <<File name>> —> searches a pattern and clubs it to the previous line
sed -n -e ‘$!N;/<<Pattern/p>>’ <<File name>> —–> searches a pattern and its previous line

====================================================
CONDITIONAL CONSTRUCTS
====================================================

if[ $name = “NOVARTIS”]
>then
>echo “HIT”
>else
>echo “MISS”
fi

if[]
then
<<Message>>
el if[]
then
<<Message>>
else
<<Message>>
fi

if[]
then
<<message>>
fi

====================================================
LOOPING CONSTRUCTS
====================================================

====================================================

FOR LOOP
====================================================

for file in ‘ls’
do

cat /dev/null > $filename
done

for i in {1..4}
do
<<Message>>
done

====================================================

WHILE LOOP
====================================================

#!/bin/bash

ehco “Enter File name”
read filename

while read line
do
echo $line >> <<Output File>>
done

====================================================

HASH VALUE OF A STRING
====================================================

x=`echo <<String>> | digest -a sha1 /dev/fd/0`
echo $x

digest -l —> lists of hash algorithms available in a system

====================================================

COMPARATORS for NUMERIC VALUES
====================================================

-gt –> greater than
-lt –> less than
-ge –> greater than or equal to
-le –> less than or equal to
-ne –> not equals
-eq –> equals

====================================================

COMPARATORS FOR STRING
====================================================

= –> assign a value to variable of type String
= –> compares two strings

====================================================

CHECKS
====================================================

-f –> checks if a file exists
-r –> checks is a file has read permissions
-w –> checks if a file has write permissions
-x –> checks if file has executable permissions
-d –> check if is a directory
-s –> check if file is not balnk
-z –> check if an expressions is null
!-n –> checks if an expression is not null

====================================================

SETTING PERMISSIONS ON A FILE
====================================================

chmod 755 <<File name>>

7 –> refers to the user
5 –> refers to group
5 –> refers to others

r w x
0 0 1 –> 1
0 1 0 –> 2
0 1 1 –> 3
1 0 0 –> 4
1 0 1 –> 5
1 1 0 –> 6
1 1 1 –> 7

====================================================
USER ADD COMMAND
====================================================

useradd -u <<User id>> -g <<Gourp id>> -G <<Group Name>> -d <<Default Directory>> loginname=<<name of the user>>or -u <<name of the user>>

====================================================

GROUP ADD COMMAND
====================================================

groupadd -g <<group id>> <<group name>>

wc –> refers to word count

wc -l –> no. of lines
wc -c –> no. of characters
wc -w –> no. of words

====================================================

5 types of shell
====================================================

sh –> bourne shell
bash –> bourne again shell
csh –> c shell
tcsh
ksh –> Korne Shell

====================================================

Scheduling a shell script
====================================================

crontab -l —> List all cron jobs
crontab -e —> edit the cron job
crontab -r –> remove all the cron jobs

rm –> remove command
rm -rf –> forced removal
mkdir –> creates a directory
-v –> verbose mode

sqlplus -s <<Username>>/<<Password>>@<<Database ip>>:<<Database Port>>:<<DBSID>> <<EOF >
select * from dual;
exit;<<EOF

tail -10 <<File name>> –> prints the last ten lines of a file
head -10 <<File name>> –> prints the first ten lines of a file

cut -f1 -d: —> to cut first column

sed ‘s/<<Pattern to be replaced>>/<<Replacing pattern>>’ <<File name>>
Esc:%s/<<Pattern to be replaced>>/<<rreplacing pattern>> —> command is issued to replace a pattern when the file is open in VI mode

Esc:se nu —> Does numbering of the file —> command to be issued when the file is in vi mode

2>&1 outputs error to file called stderr.out

more +<<line no.>> <<File name>> —> prints the file from the line no. specified

Advertisements

About Swapratim

Blog Writer in Medium for Chatbots Magazine - world's biggest chatbot magazine. Also he writes in Bot Publication and Chatbots Journal on regular basis (https://medium.com/@swapratimroy). Experienced in IT development & support pursuing different roles & responsibilities. He has worked on several innovative projects. Android apps developed by him – In Time SMS, AndroLogic which are freely available in Google Play Store. He has several in house tools built available for free usage on internet. Apart from this, he likes to share his learning with everyone which he had learned over years. Sole purpose of this blog is to share the author's knowledge in IT, passion for travel and recent trends with you. Hope you will enjoy his contents.
Aside | This entry was posted in Technology, Various Scripting Tutorials and tagged , , , , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s