You are on page 1of 9

9/3/2019 OneNote

Basic Shell Skills


24 আগ 2019 09.55

The shell makes a difference between three kinds of commands:


█ Aliases
█ Internal commands
█ External commands
Some aliases are provided by default; type alias on the command line to get an
overview. To define an alias, use alias newcommand=’oldcommand’ , as in
the default alias ll=’ls -l --color=auto’ .

Aliases are executed before anything else.


An internal command is a command that is a part of the shell itself. It is available
when the shell is loaded and can be executed from memory without any lookup
from disk.
An external command is a command that exists as an executable file on disk of
the computer. Because it has to be read from disk, it is a bit slower. When a user
executes a command, the shell first looks to determine whether it is an internal
command; if it is not, it looks for an executable file with a name that matches the
command on disk.
To find out whether a command is a bash internal, or an executable file on disk,
you can use the type command.

To find out which exact command the shell will be using, you can use the which
command. For instance, type which ls to find out where the shell will get the
ls command from.

$PATH by including ./ in front of it. The dot stands for the current directory,
and by running it as ./ , you’ll tell bash to look for the command in the current
directory.

time ls
which time
echo $PATH
/bin/time ls

I/O Redirection

Stream Redirection
Linux includes redirection commands for each stream. These commands write standard
output to a file. If a non-existent file is targetted (either by a single-bracket or double-
bracket command), a new file with that name will be created prior to writing.
Commands with a single bracket overwrite the destination’s existing contents.
Overwrite
• > - standard output
https://onedrive.live.com/redir?resid=48C04F2D610DA740%211703&page=Edit&wd=target%28Untitled Section.one%7C918da4ff-65e5-4108-bb5b-80… 1/9
9/3/2019 OneNote

• < - standard input


• 2> - standard error
Commands with a double bracket do not overwrite the destination’s existing contents.
Append
• >> - standard output
• << - standard input
• 2>> - standard error
Let’s see an example:
cat > write_to_me.txt

ctrl-d

Here, cat is being used to write to a file, which is created as a result of the loop.
View the contents of writetome.txt using cat:
cat write_to_me.txt

It should have the following contents:


a
b
c

Redirect cat to writetome.txt again, and enter three numbers.


cat > write_to_me.txt

ctrl-d

When you use cat to view writetome.txt, you will see the following:
1
2
3

The prior contents are no longer there, as the file was overwritten by the single-bracket
command.
Do one more cat redirection, this time using double brackets:
cat >> write_to_me.txt

ctrl-d

Open writetome.txt again, and you will see this:


https://onedrive.live.com/redir?resid=48C04F2D610DA740%211703&page=Edit&wd=target%28Untitled Section.one%7C918da4ff-65e5-4108-bb5b-80… 2/9
9/3/2019 OneNote
1
2
3
a
b
c

The file now contains text from both uses of cat, as the second one did not override the
first one.

Redirector Explanation
> (same as 1>) Redirects STDOUT. If redirection is to a file, the current contents of
that
file are overwritten.
>> (same as 1>>) Redirects STDOUT. If output is written to a file, the output is
appended
to that file.
2> Redirects STDERR.
2>&1 Redirects STDERR to the same destination as STDOUT.
< (same as 0<) Redirects STDIN.

What is grep?
The grep command is for scanning through texts for a particular search term
(which can be in the form of a regex string) and return all lines that meets the
match. As a result grep is one of the most commonly used Linux commands.
Grep essentially acts as a filtering tool that's used for only outputting the lines
we are interested in.
To use grep, all you have to do is give it some content and tell it what search-
term/regular-expression to serach for. Grep will then output all lines where it
finds a match.
For example, let's say we have the following file:
$ cat testfile.txt

A list of fruits:
apples
bananas
oranges
more bananas

Now if we grep for bananas we get:


$ grep 'bananas' testfile.txt

bananas
more bananas

Or we can grep for the letter "e":


$ grep 'e' testfile.txt

apples
oranges
more bananas

https://onedrive.live.com/redir?resid=48C04F2D610DA740%211703&page=Edit&wd=target%28Untitled Section.one%7C918da4ff-65e5-4108-bb5b-80… 3/9


9/3/2019 OneNote

We can also pipe content into the grep command, here's how we do this when
grepping for "pp":
$ cat testfile.txt | grep 'pp'

apples

If grep can't find a match, then it won't return anything at all:


$ grep peaches testfile.txt

Useful grep options


There's a bunch of option that you can use to control grep's default
behaviour. You can view all of them using grep --help. Here are some useful
options:
-i, --ignore-case: find matches irrespective of upper/lower case
characters
-v, --invert-match: This does a reverse match.
-R, --dereference-recursive: This recursively search through all content.

Using Pipe
Piping is a technique that lets you use Linux commands as building blocks to
build your own custom commands.

Type ls -R | less . This shows the same result, but in the pager less , where you
can scroll up and down using the arrow keys on your keyboard.

$ ls -l
total 0

-rw-r--r--. 1 root root 0 Oct 20 19:22 file1

-rw-r--r--. 1 root root 0 Oct 20 19:22 file2

-rw-r--r--. 1 root root 0 Oct 20 19:22 file3

drwxr-xr-x. 2 root root 6 Oct 20 19:22 folder1

drwxr-xr-x. 2 root root 6 Oct 20 19:22 folder2

drwxr-xr-x. 2 root root 6 Oct 20 19:22 folder3


Now lets say you want to only list files and no directories. Then one way to do
this is by using piping:
$ ls -l | grep "^-"

-rw-r--r--. 1 root root 0 Oct 20 19:22 file1

-rw-r--r--. 1 root root 0 Oct 20 19:22 file2

-rw-r--r--. 1 root root 0 Oct 20 19:22 file3

Here we used the pipe character, "|", to redirect the output of of "ls -l", into the
grep command.
https://onedrive.live.com/redir?resid=48C04F2D610DA740%211703&page=Edit&wd=target%28Untitled Section.one%7C918da4ff-65e5-4108-bb5b-80… 4/9
9/3/2019 OneNote

Due to the pipe, the standard output is instead redirected (i.e. piped) as the
input (aka standard input) for the next command (grep). The "^-" instructs
grep to only display lines that begins with "-". This means that it filters out any
lines starting with "d" (i.e. directories). The grep's standard output is then
displayed on the screen.

If instead you want to count the number of files there are (rather than a list of
the files), then you can do this by adding another pipe:
$ ls -l | grep "^-" | wc -l
3

Here, the standard output from the grep command is redirected into the
(w)ord(c)ount command. The -l option instructs wc to only do a line count.
Hence the resulting output is equal to the number of files.

Lets say we have a file1 which contains the following content:


$ cat file1

apple

102

cakes

drinks

bananas

500

301

Now we want to sort the lines in this file in alphabetical order, and then store
the ordered lines into a file called file2. To start with, lets first check that we
can order the content using the "sort" command:
$ sort file1

102

301

apple

bananas

cakes

drinks

Now we need to redirect the standard output to a file instead of outputting it


to the screen:
$ sort file1 > file2

https://onedrive.live.com/redir?resid=48C04F2D610DA740%211703&page=Edit&wd=target%28Untitled Section.one%7C918da4ff-65e5-4108-bb5b-80… 5/9


9/3/2019 OneNote

Here we have employed the > redirection operator to redirect the standard
output from "sort file1" to a new file called file2. Lets now look at the contents
of file2 to confirm that this has worked:
$ cat file2

102

301

500

apple

bananas

cakes

Drinks

Combining piping with redirection


There is a third way to write the above command, and that is by combining piping
and redirection, like this:
cat file1 | sort > file2

just to check that this has worked:


$ cat file2

102

301

500

apple

bananas

cakes

drinks

Combining piping and redirection in this way unleashes a whole new way of
working on the Linux command line, and is heavily used when writing shell
scripts.

History
1. Make sure that you have opened a shell as user user.
2. Type history to get an overview of commands that you have previously
used.
3. Type some commands, such as the following:
ls
pwd
cat /etc/hosts
ls -l.

https://onedrive.live.com/redir?resid=48C04F2D610DA740%211703&page=Edit&wd=target%28Untitled Section.one%7C918da4ff-65e5-4108-bb5b-80… 6/9


9/3/2019 OneNote

The goal is to fill the history a bit.


4. Open a second terminal on your server by right-clicking the graphical
desktop
and selecting the Open in Terminal menu option.
5. Type history from this second terminal window. Notice that you do not see
the
commands that you just typed in the other terminal. That is because the
history
file has not been updated yet.
6. From the first terminal session, type Ctrl+R . From the prompt that opens
now,
type ls . You’ll see the last ls command you used. Press Ctrl+R again.
You’ll
now see that you are looking backward and that the previous ssh command
is
highlighted. Press Enter to execute it.
7. Type history | grep cat . The grep command searches the history output
for any commands that contained the text cat . Remember the command
number of one of the cat commands you have previously used.
8. Type !nn , where nn is replaced by the number you remembered in Step 7.
You’ll see that the last cat command is repeated.
9. Close this terminal by typing exit .
10. From the remaining terminal window, type history -c . This wipes all
history
that is currently in memory. Close this terminal session as well.
11. Open a new terminal session and type history . It may be a bit unexpected,
but
you’ll see a list of commands anyway. That is because history -c clears
the inmemory history, but it does not remove the .bash_history file
in your home directory.

12. As an alternative to deleting the history file, you can also use
history -w
After using
history –c
Editing Files with vim

https://onedrive.live.com/redir?resid=48C04F2D610DA740%211703&page=Edit&wd=target%28Untitled Section.one%7C918da4ff-65e5-4108-bb5b-80… 7/9


9/3/2019 OneNote

Understanding the Shell Environment

env
Environment Configuration Files
When a user logs in, an environment is created for that user automatically. This
happens based on four different files where some script code can be specified
and
where variables can be defined for use by one specific user:

█ /etc/profile: This is the generic file that is processed by all users upon
login.

█ /etc/bashrc: This file is processed when subshells are started.

https://onedrive.live.com/redir?resid=48C04F2D610DA740%211703&page=Edit&wd=target%28Untitled Section.one%7C918da4ff-65e5-4108-bb5b-80… 8/9


9/3/2019 OneNote

█ ~/.bash_profile: In this file, user-specific login shell variables can be


defined.

█ ~/.bashrc: In this user-specific file, subshell variables can be


defined.

echo $LANG
ls –help
LANG=fr_FR.UTF-8
ls –help

Use vim .bashrc to open the .bashrc configuration file

In this file, add the line COLOR=red to set a variable with the name COLOR
and assign it the value red.
Verify that the variable COLOR has been set, by using echo $COLOR .

man –k
man –k partition
man -k partition | grep 8

Type mandb to update the man database.

https://onedrive.live.com/redir?resid=48C04F2D610DA740%211703&page=Edit&wd=target%28Untitled Section.one%7C918da4ff-65e5-4108-bb5b-80… 9/9

You might also like