Professional Documents
Culture Documents
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
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
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
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
bananas
more bananas
apples
oranges
more bananas
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
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
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.
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
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
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.
12. As an alternative to deleting the history file, you can also use
history -w
After using
history –c
Editing Files with vim
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.
echo $LANG
ls –help
LANG=fr_FR.UTF-8
ls –help
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