How to use Linux Command with example

How to read a data from linux or unix command prompt:
?
echo -n "Please enter the components name : "
read compName

or 

read -p "Please enter the components name : "compName
echo "$compName"


How to hold current directory path in command prompt:
?
var=$(pwd)
echo "$var"
How to change any directory path like (/opt/ctier/kartik/soap) to other format like ( \/opt\/ctier\/kartik\/soap):
?
var=$(pwd)
#CHANGE_PATH_VALUE="\/opt\/ctier\/kartik\/$compName"
CHANGE_PATH_VALUE="$var/$compName"
IFS='/' read -ra NAMES <<< "$CHANGE_PATH_VALUE" #Convert string to array
#Print all names from array $1
concatLine="";
#CHANGE_PATH_VALUE="\/opt\/ctier\/kartik\/$compName"
constantVal="\/"
count=0
incr=1
for i in "${NAMES[@]}"; do
   if [ $count -eq 0 ];
    then
concatLine="$concatLine$i"
count=$((count+incr))
   else
    concatLine="$concatLine$constantVal$i"
count=$((count+incr))
   fi
done

How to update a file using seed command:
?
A>File information

################################################################################

installLocation=/usr/local/kartik/soap

################################################################################

CONFIGDBINFO.databaseHost=dummyhost
CONFIGDBINFO.databaseSid=dummy

CENTER.machineID=
CENTER.instanceID=



B>

REST_NEW_DATABASE_HOST="kcm"
REST_NEW_DATABASE_SID="kartik15"
REST_NEW_MACHINE_ID="192.168.0.1"
REST_NEW_INSTANCE_ID="21"

appendMode=.answer
read -p "Please enter the components name : "compName
echo "$compName"
var=$(pwd)
fileName="$var/$compName/$compName$appendMode"


#CHANGE_PATH_VALUE="\/opt\/ctier\/kartik\/$compName"

CHANGE_PATH_VALUE="$var/$compName"
IFS='/' read -ra NAMES <<< "$CHANGE_PATH_VALUE" #Convert string to array
#Print all names from array $1
concatLine="";
#CHANGE_PATH_VALUE="\/opt\/ctier\/kartik\/$compName"
constantVal="\/"
count=0
incr=1
for i in "${NAMES[@]}"; do
   if [ $count -eq 0 ];
    then
concatLine="$concatLine$i"
count=$((count+incr))
   else
    concatLine="$concatLine$constantVal$i"
count=$((count+incr))
   fi
done



    sed -i "s/databaseHost=.*/databaseHost=${REST_NEW_DATABASE_HOST}/g" $fileName

     sed -i "s/databaseSid=.*/databaseSid=${REST_NEW_DATABASE_SID}/g" $fileName
     sed -i "s/machineID=.*/machineID=${REST_NEW_MACHINE_ID}/g" $fileName
     sed -i "s/instanceID=.*/instanceID=${REST_NEW_INSTANCE_ID}/g" $fileName
     sed -i "s/installLocation=.*/installLocation=${concatLine}/g" $fileName


c>Output

################################################################################

installLocation=/opt/ctier/kartik/soap

################################################################################

CONFIGDBINFO.databaseHost=kcm
CONFIGDBINFO.databaseSid=kartik15

CENTER.machineID=192.168.0.1
CENTER.instanceID=21

How to change the directory:
?
 cd "${compName}"


How to download some file from remote machine:
?
scp -r -c arcfour $login@$IP:$url

Example
scp -r -c arcfour ctier@192.168.0.1:/opt/citier/mandal/

How to upload a file to a remote location:
?
 scp -r "${warFileName}" $jBossUserName@$jBossIp:$jBossfolder

like 

 scp -r "/opt/ctier/example/exaample.txt" ctier@$192.168.0.1:/opt/ctier/newexample/example.txt

How to search a class in the list of jar files in linux command
?
#!/bin/sh

#
# Module : Jarfind.sh - To search a class in the list of jar files
#
# Usage  : Run this script as
#
#          > sh jarfind.sh transaction
#
# Version : Draft
#
# REVISIT : Provide option to perform search recursively ?
#
argc=$#
if [ $argc -le 0 ];
then
        echo "usage: jarfind <class-name>"
        exit 1
fi
string=$1
JAR_LIST=`ls -1 *.jar`
echo "Searching..."
for jar_file in $JAR_LIST
do
        is_found=`jar tvf $jar_file | grep -i $string`
        if [ x"$is_found" = x ];
        then
                continue
        else
                echo "Found $string in $jar_file"
                jar tvf $jar_file | grep -i $string
        fi
done




Did you know the grep Operation:
?
 First create the following demo_file that will be used in the examples below to demonstrate grep command.

$ cat demo_file
THIS LINE IS THE 1ST UPPER CASE LINE IN THIS FILE.
this line is the 1st lower case line in this file.
This Line Has All Its First Character Of The Word With Upper Case.

Two lines above this line is empty.
And this is the last line.
1. Search for the given string in a single file

The basic usage of grep command is to search for a specific string in the specified file as shown below.

Syntax:
grep "literal_string" filename

$ grep "this" demo_file
this line is the 1st lower case line in this file.
Two lines above this line is empty.
And this is the last line.
2. Checking for the given string in multiple files.

Syntax:
grep "string" FILE_PATTERN

This is also a basic usage of grep command. For this example, let us copy the demo_file to demo_file1. The grep output will also include the file name in front of the line that matched the specific pattern as shown below. When the Linux shell sees the meta character, it does the expansion and gives all the files as input to grep.

$ cp demo_file demo_file1

$ grep "this" demo_*
demo_file:this line is the 1st lower case line in this file.
demo_file:Two lines above this line is empty.
demo_file:And this is the last line.
demo_file1:this line is the 1st lower case line in this file.
demo_file1:Two lines above this line is empty.
demo_file1:And this is the last line.
3. Case insensitive search using grep -i

Syntax:
grep -i "string" FILE

This is also a basic usage of the grep. This searches for the given string/pattern case insensitively. So it matches all the words such as “the”, “THE” and “The” case insensitively as shown below.

 $ grep -i "the" demo_file
THIS LINE IS THE 1ST UPPER CASE LINE IN THIS FILE.
this line is the 1st lower case line in this file.
This Line Has All Its First Character Of The Word With Upper Case.
And this is the last line.
4. Match regular expression in files

Syntax:
grep "REGEX" filename

This is a very powerful feature, if you can use use regular expression effectively. In the following example, it searches for all the pattern that starts with “lines” and ends with “empty” with anything in-between. i.e To search “lines[anything in-between]empty” in the demo_file.

$ grep "lines.*empty" demo_file
Two lines above this line is empty.
From documentation of grep: A regular expression may be followed by one of several repetition operators:

? The preceding item is optional and matched at most once.
* The preceding item will be matched zero or more times.
+ The preceding item will be matched one or more times.
{n} The preceding item is matched exactly n times.
{n,} The preceding item is matched n or more times.
{,m} The preceding item is matched at most m times.
{n,m} The preceding item is matched at least n times, but not more than m times.
5. Checking for full words, not for sub-strings using grep -w

If you want to search for a word, and to avoid it to match the substrings use -w option. Just doing out a normal search will show out all the lines.

The following example is the regular grep where it is searching for “is”. When you search for “is”, without any option it will show out “is”, “his”, “this” and everything which has the substring “is”.

$ grep -i "is" demo_file
THIS LINE IS THE 1ST UPPER CASE LINE IN THIS FILE.
this line is the 1st lower case line in this file.
This Line Has All Its First Character Of The Word With Upper Case.
Two lines above this line is empty.
And this is the last line.

The following example is the WORD grep where it is searching only for the word “is”. Please note that this output does not contain the line “This Line Has All Its First Character Of The Word With Upper Case”, even though “is” is there in the “This”, as the following is looking only for the word “is” and not for “this”.

$ grep -iw "is" demo_file
THIS LINE IS THE 1ST UPPER CASE LINE IN THIS FILE.
this line is the 1st lower case line in this file.
Two lines above this line is empty.
And this is the last line.
6. Displaying lines before/after/around the match using grep -A, -B and -C

When doing a grep on a huge file, it may be useful to see some lines after the match. You might feel handy if grep can show you not only the matching lines but also the lines after/before/around the match.


Please create the following demo_text file for this example.

$ cat demo_text
4. Vim Word Navigation

You may want to do several navigation in relation to the words, such as:

 * e - go to the end of the current word.
 * E - go to the end of the current WORD.
 * b - go to the previous (before) word.
 * B - go to the previous (before) WORD.
 * w - go to the next word.
 * W - go to the next WORD.

WORD - WORD consists of a sequence of non-blank characters, separated with white space.
word - word consists of a sequence of letters, digits and underscores.

Example to show the difference between WORD and word

 * 192.168.1.1 - single WORD
 * 192.168.1.1 - seven words.
6.1 Display N lines after match
-A is the option which prints the specified N lines after the match as shown below.

Syntax:
grep -A <N> "string" FILENAME

The following example prints the matched line, along with the 3 lines after it.

$ grep -A 3 -i "example" demo_text
Example to show the difference between WORD and word

* 192.168.1.1 - single WORD
* 192.168.1.1 - seven words.
6.2 Display N lines before match
-B is the option which prints the specified N lines before the match.

Syntax:
grep -B <N> "string" FILENAME

When you had option to show the N lines after match, you have the -B option for the opposite.

$ grep -B 2 "single WORD" demo_text
Example to show the difference between WORD and word

* 192.168.1.1 - single WORD
6.3 Display N lines around match
-C is the option which prints the specified N lines before the match. In some occasion you might want the match to be appeared with the lines from both the side. This options shows N lines in both the side(before & after) of match.

$ grep -C 2 "Example" demo_text
word - word consists of a sequence of letters, digits and underscores.

Example to show the difference between WORD and word

* 192.168.1.1 - single WORD
7. Highlighting the search using GREP_OPTIONS

As grep prints out lines from the file by the pattern / string you had given, if you wanted it to highlight which part matches the line, then you need to follow the following way.

When you do the following export you will get the highlighting of the matched searches. In the following example, it will highlight all the this when you set the GREP_OPTIONS environment variable as shown below.

$ export GREP_OPTIONS='--color=auto' GREP_COLOR='100;8'

$ grep this demo_file
this line is the 1st lower case line in this file.
Two lines above this line is empty.
And this is the last line.
8. Searching in all files recursively using grep -r

When you want to search in all the files under the current directory and its sub directory. -r option is the one which you need to use. The following example will look for the string “ramesh” in all the files in the current directory and all it’s subdirectory.

$ grep -r "ramesh" *
9. Invert match using grep -v

You had different options to show the lines matched, to show the lines before match, and to show the lines after match, and to highlight match. So definitely You’d also want the option -v to do invert match.

When you want to display the lines which does not matches the given string/pattern, use the option -v as shown below. This example will display all the lines that did not match the word “go”.

$ grep -v "go" demo_text
4. Vim Word Navigation

You may want to do several navigation in relation to the words, such as:

WORD - WORD consists of a sequence of non-blank characters, separated with white space.
word - word consists of a sequence of letters, digits and underscores.

Example to show the difference between WORD and word

* 192.168.1.1 - single WORD
* 192.168.1.1 - seven words.
10. display the lines which does not matches all the given pattern.

Syntax:
grep -v -e "pattern" -e "pattern"

$ cat test-file.txt
a
b
c
d

$ grep -v -e "a" -e "b" -e "c" test-file.txt
d
11. Counting the number of matches using grep -c

When you want to count that how many lines matches the given pattern/string, then use the option -c.

Syntax:
grep -c "pattern" filename

$ grep -c "go" demo_text
6

When you want do find out how many lines matches the pattern

$ grep -c this demo_file
3

When you want do find out how many lines that does not match the pattern

$ grep -v -c this demo_file
4
12. Display only the file names which matches the given pattern using grep -l

If you want the grep to show out only the file names which matched the given pattern, use the -l (lower-case L) option.

When you give multiple files to the grep as input, it displays the names of file which contains the text that matches the pattern, will be very handy when you try to find some notes in your whole directory structure.

$ grep -l this demo_*
demo_file
demo_file1
13. Show only the matched string

By default grep will show the line which matches the given pattern/string, but if you want the grep to show out only the matched string of the pattern then use the -o option.

It might not be that much useful when you give the string straight forward. But it becomes very useful when you give a regex pattern and trying to see what it matches as

$ grep -o "is.*line" demo_file
is line is the 1st lower case line
is line
is is the last line
14. Show the position of match in the line

When you want grep to show the position where it matches the pattern in the file, use the following options as

Syntax:
grep -o -b "pattern" file

$ cat temp-file.txt
12345
12345

$ grep -o -b "3" temp-file.txt
2:3
8:3

Note: The output of the grep command above is not the position in the line, it is byte offset of the whole file.

15. Show line number while displaying the output using grep -n

To show the line number of file with the line matched. It does 1-based line numbering for each file. Use -n option to utilize this feature.

$ grep -n "go" demo_text
5: * e - go to the end of the current word.
6: * E - go to the end of the current WORD.
7: * b - go to the previous (before) word.
8: * B - go to the previous (before) WORD.
9: * w - go to the next word.
10: * W - go to the next WORD.


How to use grep for find out the running process of a particular process:
?
ps -ef |grep java


How to kill the process:
?
kill -9 $PID

Example:
kill -9 12135


Top 50 comand:
Top 50 Linux Commands You Must Know as a Regular User
--------------------------------------------------
ls - The most frequently used command in Linux to list directories
pwd - Print working directory command in Linux
cd - Linux command to navigate through directories
mkdir - Command used to create directories in Linux
mv - Move or rename files in Linux
cp - Similar usage as mv but for copying files in Linux
rm - Delete files or directories
touch - Create blank/empty files
ln - Create symbolic links (shortcuts) to other files
cat - Display file contents on the terminal
clear - Clear the terminal display
echo - Print any text that follows the command
less - Linux command to display paged outputs in the terminal
man - Access manual pages for all Linux commands
uname - Linux command to get basic information about the OS
whoami - Get the active username
tar - Command to extract and compress files in Linux
grep - Search for a string within an output
head - Return the specified number of lines from the top
tail - Return the specified number of lines from the bottom
diff - Find the difference between two files
cmp - Allows you to check if two files are identical
comm - Combines the functionality of diff and cmp
sort - Linux command to sort the content of a file while outputting
export - Export environment variables in Linux
zip - Zip files in Linux
unzip - Unzip files in Linux
ssh - Secure Shell command in Linux
service - Linux command to start and stop services
ps - Display active processes
kill and killall - Kill active processes by process ID or name
df - Display disk filesystem information
mount - Mount file systems in Linux
chmod - Command to change file permissions
chown - Command for granting ownership of files or folders
ifconfig - Display network interfaces and IP addresses
traceroute - Trace all the network hops to reach the destination
wget - Direct download files from the internet
ufw - Firewall command
iptables - Base firewall for all other firewall utilities to interface with
apt, pacman, yum, rpm - Package managers depending on the distro
sudo - Command to escalate privileges in Linux
cal - View a command-line calendar
alias - Create custom shortcuts for your regularly used commands
dd - Majorly used for creating bootable USB sticks
whereis - Locate the binary, source, and manual pages for a command
whatis - Find what a command is used for
top - View active processes live with their system usage
useradd and usermod - Add new user or change existing users data
passwd - Create or update passwords for existing users

How to Delete a Single Line in Vi/Vim
--------------------------------------
There's a better way of deleting complete lines in Vi and Vim than spamming Backspace on the keyboard.

To delete a line in Vi or Vim, switch to normal mode first. If you're in command mode or insert mode, you can switch back to normal mode by pressing Escape. If you don't know what modes are, it's a good opportunity to learn the basics of Vim.

Highlight the line you want to delete, then hit dd or D on the keyboard. The editor will automatically remove the whole line from the file.

Vi and Vim commands are case-sensitive, which means d and D are identified as two separate commands.

Hitting dd or D multiple times will delete several lines one by one. The next line will be automatically highlighted once the former is deleted.

To delete a specific line in Vi/Vim, press Escape to enter into command mode, then type nd to delete a particular line, where n is the number of the line you want to remove.

For instance, if you want to delete the 150th line in the /var/log/messages file, you'll open it with Vi/Vim and then type 150d to remove the line.

Deleting Multiple Lines in Vi and Vim
-------------------------------------
If you find repeatedly hitting keys on the keyboard inconvenient, you can remove multiple lines at once by tweaking the aforementioned command. Instead of simply pressing dd, you can specify the number of lines you want to delete.

For example, typing 3dd will delete the next three lines from the file.

If you want to delete multiple lines within a specific range, you'll have to switch to command mode first (press Escape while in normal mode). Then, use the following syntax to create your own deletion command:

:[start],[end]d

For example, to delete the lines between the range one and five:

:1,5d

You can also use wildcard characters in the command above.

. (Dot): Refers to the current line
$ (Dollar): Denotes the end of the file
% (Percentage): Matches all the lines in the file

For example:
:.,5d     # deletes lines between the current line and the fifth line
:.,$d     # removes all lines starting from the current line till the end
:%d       # clears the entire file


Remove nth line from the file
----------------------------------
Imagine you have to delete the 7th line number in a file. You can use the sed command like this:

sed -i '7d' filename

Remove the last line using sed
--------------------------------
You learned to delete a particular line but what if you need to remove the last line?

You may always get the total number of lines in a file using wc command and use this number with sed. However, sed has a dedicated way of deleting the last line of a file. You don't need to worry about the number of files anymore now.

sed -i '$d' filename

Delete a range of lines
--------------------------------
Similar to what you saw earlier, you can also delete a range of lines from the file.

Suppose you have to delete the lines from 11 to 15, you can provide the range like this:

sed -i '11,15d' filename
Note that it will also remove the lines 11 and 15, not just the lines that fall between them

Remove lines containing a string
---------------------------------
You can also use sed to delete all the lines that contain a given string or match a specific pattern.

The command below will delete all the lines that contain the word 'string':

sed -i '/string/d' filename

Remove lines starting with a word
----------------------------------
If you want to remove all the lines starting with a particular word or letter, you just have to provide the regex pattern like this:

sed -i '/^word/d' filename

Remove all empty lines
----------------------------------
Before I end this article, let me quickly share how you can remove all empty lines using sed:

sed -i '/^$/d' filename

alias command
----------------------
The alias command can be useful if you want to create a 'shortcut' to a command.
The format is alias name='command'

> alias home='cd /home/dave/public_html'

This will create an alias called home which will put you in the /home/dave/public_html directory whenever you type home at the command prompt. You can alias any command you want, and include options for the command.

> alias list='ls -la'

This will create an alias called list, which will use the ls command to print a long-style listing of all files in the current directory (the -l gives a long-style list, and the -a shows all files - including hidden files).
(Find out more about the ls command)

To see a list of aliases set up on your linux box, just type alias at the prompt.

> alias
alias attrib='chmod'
alias chdir='cd'
alias copy='cp'
alias cp='cp -i'

Previous
Next Post »