0% found this document useful (0 votes)
58 views9 pages

Linux Command Shell Scripting

This document summarizes common Linux shell scripting commands. It describes redirecting output with > and appending with >>. It also covers concatenating files, displaying file content from the beginning or end with head and tail, piping output between commands, finding files with find, searching files for text with grep, and working with table data using AWK. The document also discusses using sudo to override permissions, setting variables, and taking user input.

Uploaded by

sachin singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
58 views9 pages

Linux Command Shell Scripting

This document summarizes common Linux shell scripting commands. It describes redirecting output with > and appending with >>. It also covers concatenating files, displaying file content from the beginning or end with head and tail, piping output between commands, finding files with find, searching files for text with grep, and working with table data using AWK. The document also discusses using sudo to override permissions, setting variables, and taking user input.

Uploaded by

sachin singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

LINUX COMMAND

SHELL SCRIPTING
COMMANDS
> (redirect) :- it use to use the output in other command.
eg: echo "hello there" > new.txt
output:
cat new.txt
hello there

>> (append) : work like redirect but add data to existing file without overriding.
echo "hello there sachin" > new.txt
output:
cat new.txt
hello there
hello there sachin

concatinate two files :


eg: cat file1.txt file2.txt
{one file after one file}
**it is important when we want to copy data of two file into one file.

tail : display content from end:-


syntax:
tail -(no. of line want to display) filename
head : display content from start:-
syntax:
head -(no. of line want to display) filename
pipe :- using of one command output in input of second command.
syntax:-
command1 | command2
eg:-
let assume a folder a
:- ls a/ | less
find : use to find file .
syntax:-
find (location were to find) (name of file want to find)
eg:-
find a sac.txt
**in output it return the path of the file.

syntax:-
find (location were to find) -name (name of file want to find)
eg:-
find a -name sac.txt
**in output it return the path of the file.
syntax:-
find (location were to find) -type d
eg:-
find a -type d
**in output it return the path of all the directories present inside that location.

grep :- use to grab the word from any file.this return the word if the given word
appear in it.
syntax :-
grap (word) (file name)
**this is a case sensitive.

but if we use:- "grep -i" instead of "grep" it ignore cases.

grep -v (word) filename :- it return the all data which is NOT containing the word
in command.

AWK
awk :-works for the table related data.

syntax:
awk '{print}' filename or awk '{print $0}' filename: to print complete data of the file.
awk '{print $1 $2}' filename : to concatinate data of colume 1 and 2 and print.
awk '{print $1,$2}' filename : to print data of coloumn 1 and 2 without concatination.
awk 'NR==row number {print $0}' filename: to print perticular row.
awk '{print $NR}' filename : to print thr last row.
awk '{print $(NR-i)}' filename: to print perticular row from the last.
awk 'NR==i,NR==j{print $0}' filename :from i to j is the limit of row to print.
awk 'NR==i;NR==j{print $0}' filename : only print i and j row.
case 2 :- let the data is written in continuous format with a PERTICULAR SEPRATOR to print
according to seprator :
syntax:
awk -F "seprator" '{print }' filename:
for all above case operation just add :- -F "seprator"

case 3 :- when we have to find a perticular word in the table.


syntax :
awk '/word which want to find/{print}' filename : all data will display which have that word in
row.
awk '/word which want to find| second word/{print}' filename :- | or operator
awk '$i~/word which want to find/{print}' filename : for perticular row .
**
awk '{print length($i)}' filename :-length of perticular columne of each row.

SUDO
if we want to edit a file which have not the permission to edit. so here we have
two way
i)change the owner
ii)sudo :-command before any command.to over rite the permission.
example:- sudo nano filename.txt
*if we create the file in root folder.
test :- sudo bash :- it changes the root folder name.
owner change :
syntax: sudo chown new_ownername filename
change group :
syntax: sudo chgrp new_groupname filename
**To change the permission in linux we use :-
chmod
u :-user r :-read
g :-guest w :-write
o :-other x :-exicutable
syntax :-
chmod u=rw filename :- when we want to give user permission of read and write.
shotcut for giving exicutable permission :
syntax
chmod +x filename

UNIX Login:
if we declare a variable in terminal and after re opening of the terminal we are not able to
access the variable value because it is temprory in the terminal .
to use the value of that variable we have to create that in the ".bashrc" file of the linux which
run very first just after the opening of the terminal.

** when you want to know the place were command is stored use which command
synatax
which command_name
eg :-
which ls
which pwd
.bashrc main change karo:
PATH="bash wala folder ka location:${PATH}"
export PATH
to check if string is empty or not :
read -p "enter the string" str
if[ -z "$str" ];then
echo "empty"
fi
for searching of files.
Eg -e $myfile : file exist or not.
! -e $myfile : file not exist.
-d $myfile :
basename : used to extarct the name of the file.

VARIABLE
syntax:
variable_name="value want to assign"
eg:-
a=5
a="sachin"
when want to access the value of variable place $ sign before the name;
eg
a=5
echo $a or
or
echo ${a} :-here benfit is you can conacatinate the value to the variable.

**use command output in the variable :


eg :-
a=$(pwd)
echo $a
or
echo ${a} :- :-here benfit is you can conacatinate the value to the variable.

user input to the variable:


syntax :-
read variable_name
*let the user know which value they have to write .
syntax :
read -p "message want to display before input " variable_name
*want to hide what you are writing due to privacy reasons .
eg: we want to write the password .
syntax :
read -s variable_name
*want to hide what you are writing due to privacy reasons and the user know
which value they have to write
syntax :
read -sp "message want to disaplay before input " variable_name
direct running command through the variable :
syntax
1)variable_name=command_want_to_store
2)$variable_name
eg :-
1)a=date
$a
2)b=pwd
$b

You might also like