Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Linux: Piping, Alias, Variables & Filters

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 17

Linux

Piping, Alias, Variables & Filters


Linux Shell
• The kernel is a computer program that is the
core of a computer’s operating system, with
complete control over everything in the
system.
• A shell is special user program which provide
an interface to user to use operating system
services.
• Shell accepts human readable commands
from user and converts them into something
which kernel can understand.
• Shell can be accessed by user using a
command line interface. A special program
called Terminal in linux/macOS or Command
Prompt in Windows OS is provided to type in
the human readable commands such as “cat”,
“ls” etc. and then it is being execute.
Pipe
• Pipe is a form of redirection used to transfer standard o/p to some
other destination
• It is used to combine 2 or more commands and o/p of one command
acts as input to another command
• Cmd line processors that do the further processing are called filters
• Pipes are unidirectional i.e. data flows from left to right
• Syntax:
command_1 | command_2 | command_3 | .... | command_N
• 1) Listing all files and giving it as input to the “more
“ command
$ ls -l | more

• The more command takes the output of $ ls -l as its input. The net
effect of this command is that the output of ls -l is displayed one
screen at a time.
• The pipe acts as a container which takes the output of ls -l and gives it
to “more” as input.
• Previous command is equivalent to the sequence of commands listed
below
$ ls -l -> temp
more -> temp (or more temp)
[contents of temp]
rm temp
• 2) Use sort and uniq command to sort a file and print unique values
$ sort record.txt | uniq

• 3) Use head and tail to print lines in a particular range in a file.


$ cat sample2.txt | head -7 | tail -5

• 4) Use ls and find to list and print all lines matching a particular
pattern in matching files.
$ ls -l | find ./ -type f -name "*.txt" -exec grep "program" {} \;

• This command select files with .txt extension in the given directory
and search for pattern like “program” in the above example and print
those lines which have program in them.
• 5) The cat, grep, tee and wc commands are used to read the
particular entry from user and store in a file and print line count.
$ cat result.txt | grep “Neha Sharma" | tee file2.txt | wc -

• Above command selects Neha Sharma and stores them in file2.txt


and print total number of lines matching Neha Sharma
Alias
• This command creates an alias. Aliases allow a string to be substituted
for a word when it is used as the first word of a simple command.
• $ alias shortName="your custom command here"
• If arguments are supplied, an alias is defined for each name whose
value is given. If no value is given, alias will print the current value of
the alias. Without arguments or with the -p option, alias prints the list
of aliases on the standard output in a form that allows them to be
reused as input.
• To create an alias to clear the screen.
$ alias cls='clear'
• Use the alias
$ cls
• To create an alias 'ls' to change the default action of ls.
$ alias ls='ls --a‘
• Use the alias $ ls
• To create various alias using cd command to go into to sub-sub directories.
$ alias ..='cd ..'
$ alias ...='cd ../..'
$ alias ....='cd ../../..‘

• Use the alias


• $ mkdir sample
• $ cd sample
• $ pwd
• /sample
• $ ..
• $ pwd
• /
• To create alias to display present working directory.
$ alias .='echo $PWD‘
• Use the alias
$.
/
• To create alias to prevent accidental deletion.
$ alias rm='rm -i‘
• Remove the sample.txt file interactively
$ rm sample.txt
rm: remove regular file 'sample.txt' ?
Linux Environment variables
• Environment Variables are used to store some values which can be
used by scripts from the shell. You are not limited to the shell
environment variables, you can create your own.
• The bash shell has two types of environment variables:
• Local variables
• Global variables
• Global variables-The Linux system sets some global environment
variables when you log into your system and they are always CAPITAL
LETTERS to differentiate them from user-defined environment
variables.
• To see these global variables, type printenv command:
• There are a lot of global environment variables, to print only one of
them, type echo command followed by $VariableName.
• Ex: to print HOME variable type echo $HOME .

• Local variables
• The Linux system also defines some standard local environment
variables for you by default.
• To view the global and local variables for the shell you are running
and available to that shell, type the set command.
• Declare Local Variables
• To declare your own environment variables directly from the shell,
type variable Name you want, followed by an equal sign and the
variable value WITHOUT any spaces:
mysite=likegeeks
• And to print the variable value, use the echo command:
echo $mysite
• What if your variable is more than one word; maybe a long string, you
can put the string between single quotations:
• mysite='likegeeks is a website that offers tech tutorials for geeks'
• And if we type echo $mysite
• If you forget the single quotation, the shell will assume that the
second word is another command and will show an error.
• The use of lower case characters for self defined variables (not
uppercase) is recommended NOT required, this helps you
distinguishing your environment variables from the system
environment variables.
• Once you have set your local variable, it will be visible in the currently
running shell scope and that means if you start another shell window
the variable will not be available in that new window.
• Declare Global Variables (Export variables)

• To declare a global environment variable, you have to declare a local


environment variable then use the export command like this:
myvar='I will do it likegeeks'
echo $myvar
export myvar
Filters
• When a program takes its input from another program, it performs
some operation on that input, and writes the result to the standard
output. It is referred to as a filter.
• Linux Filter commands accept input data from stdin (standard input)
and produce output on stdout (standard output). It transforms plain-
text data into a meaningful way and can be used with pipes to
perform higher operations.
• Example commands: Cat, cut, grep, comm, sed, tee, tr, uniq, wc, od,
sort, gzip
• Example: The grep command searches a file or files for lines that have
a certain pattern. The syntax is −
$grep pattern file(s)
The sort command arranges lines of text alphabetically or numerically
$sort filename
A long output can normally be zipped by you on the screen, but if you
run text through more or use the pg command as a filter; the display
stops once the screen is full of text.
Try: $ls -l | grep "Aug" | sort +4n | more
Quoting
• Left as an exercise for you…………..

You might also like