Categories
RHCE 7 Study Guide

Linux Shell and Command Types Explained

This tutorial explains how to view the current shell, list all available shells, change shell temporary or permanently along with Linux command types; internal, external, privileged and non-privileged in detail. Learn what the Linux command, option and argument are and how they work with examples.

This tutorial is the second part of the article \”Linux file system and shell explained with file redirectors\”. It explains following RHCSA/RHCE objective.

Access a shell prompt and issue commands with correct syntax

Other parts of this tutorial are following.

Linux file system and kernel version explained

This tutorial is first part of the article. It explains Linux file system structure (Linux directory structure) in detail along with naming convention used in kernel name.

STDIN, STDOUT, STDERR and File redirectors explained

This tutorial is the third part of the article. It explains what the STDIN, STDOUT, STDERR and File redirectors are and how they work in Linux with examples.

The shell is the default working environment in Linux. It accepts commands, interprets and processes those commands and returns the result on desired location. Different shells such as ksh, sh and bash are available in Linux.

Login process identifies the user and based on customization, it presents the configured shell just after the login.
If no customization is available for that particular user, it presents the default shell. In RHEL, default shell is the bash.

If user is logged in text environment, no further action is required to access the shell. Shell is ready to use just after the login. But if user is logged in GUI environment, in order to access the shell, he or she has to open a terminal box. Opening terminal box is simple and straightforward.

To open a terminal box, right click and select \”Open Terminal\” from right click context menu.

open terminal box in linux

Shell command prompt



Shell uses two types of command prompt:-

  • For regular user, it uses a command prompt that ends with $ character
  • For super user, it uses a command prompt that ends with # character

By default both prompts provide information about username, hostname and current working directory.

difference between regular user and root user command prompt

Shell practical examples for practice

Login from a regular use account and perform following task: –

  • View the current shell and list all available shells.
  • Change current shell temporary for running login session and verify the change.
  • Logout from current session and login again to verify that default shell has been restored.
  • Change shell again, but this time change it permanently.

Viewing the current shell and listing all available shells

To view the current shell, use echo $0 command. To list all available shells, either use cat /etc/shells or use chsh –l command.

how to view the current shell and list all available shell

Changing shell temporary

To change current shell temporary, use exec /bin/csh command. This command will change current shell to csh.
This change will be temporary. It will be lost with the current session. To verify this, terminate the current session with exit command.

Login again and view the current shell. It will be restored back to the default shell.

how to change shell temporary

Changing shell permanently

To change the current shell permanently, use chsh –s /bin/csh command. Change will take place at next login.

how to change shell permanently

Command Description
cat /etc/shells To list the all available shells
chsh –l To list the all available shells
echo $0 To view the current shell name
exec /bin/csh To change current shell temporary. At next login default shell will be restored.
chsh –s /bin/csh To change current shell permanently. Change will be applied at next login. Default shell will be used in current session.

Linux command types



A Linux command is basically a link of an executable file. Location of the file decides the type of command.
If executable file is available with in shell then the command is considered as an internal command.
If executable file is available outside the shell then it is considered as an external command.

If command is internal, shell will execute it immediately. If command is external, shell will search the
executable file in specified locations. If shell finds a match, it will run that file. Otherwise it will return with error message \”command not found\”.

  • To know whether a command is internal or external we can use type command with –t option.
  • If command is external, we can use which command to know the location of the executable file.
  • To know the path where shell will search for the executable files, we can use echo $PATH command.

linux command types internal and external

Like location, commands can also be categorized on the basis of privilege.

Every file in Linux is secured with permission levels. These permission levels define who are
allowed to access the file and how they are allowed to access it. When shell accesses an executable file,
it reads the permission properties of that file and decides whether the user who is trying to execute that file
has the necessary permission to do this or not. If user has the permission, shell executes the file otherwise
it asks user to provide the necessary credentials. If user fails to provide the necessary credentials, it does not execute the file.

linux command types privileged and non-privileged

Linux command types
Criteria Type Description
Location Internal These commands are integrated in shell. These commands are usually used for regular data manipulation tasks.
External These commands are not the part of the shell. These commands are either installed by Linux or third party software for specific services or tasks.
Privilege Non-privileged These commands are used for regular tasks and do not require any special privileges to run. These commands are available for both types of user account; regular and root.
Privileged These commands are available only for root (super user) account. These commands are intended for system management and require special privileges to run.

Linux command syntax

Basic syntax for a Linux command is following.

command option argument

Let’s understand each parameter in detail.

Command

Command is the link of an executable file. After entering any keyword at shell prompt when we hit the Enter key,
shell finds an executable file matching with that keyword. If file is available, shell executes that file otherwise
it returns an error message \”Command Not Found\”.

Option

Option is a single letter or a word, preceded by a hyphen sign that is used to change the behavior of command.
A command may use single option or multiple options or no option at all. Options can be used independently or jointly.
Regardless how they are used, they are interpreted separately. To group the options, remove space and hyphen from all options and
add a single preceding hyphen only before the first option.

option parameter in linux command

To use a whole word as option, usually commands use double hyphen. This convention removes unnecessary confusion between single
word option and grouped single letter options.

For example, usually commands use –help option to provide the basic information about them. If we use a single hyphen, shell
will interpret all letters individually (-h–e–l-p). If it fails to execute the command with interpreted options only then it
will execute the command by assuming all options as a single option (-help).

If a command supports single word option with single hyphen, it will execute. Otherwise it will return with an error message \”invalid option\”.
Again error message will be displayed for the first unmatched option.

option in linux command explained

Shell processes options based on hyphen signs. If there are two preceding hyphens, following letters will be treated as a single word.
If there is a single preceding hyphen, following letters will be treated as individual options.

how to use option with linux command

Argument

Argument is the keyword which specifies the target where command performs the action.
It can be any supportive information such as filename, directory, device or username. A command may accept single argument or
multiple arguments or no argument. Arguments are limited by the total allowed numbers of the character at command line.

Shell allows us to combine arguments with options to get more specific result. If argument is going to be used with a single
letter option, add a space between option and argument. If argument is going to be used with word option, add an equal (=) sign between them.

how to use argument in linux command

That’s all for this part. In next part, we will understand I/O (Input / Output)
redirection in detail with examples. If you like this tutorial, please don’t forget to share it from your favorite social platform.

Full Version EX300 Dumps

Try EX300 Dumps Demo

Categories
RHCE 7 Study Guide

STDIN, STDOUT, STDERR and File Redirectors Explained

This tutorial explains I/O (Input/Output) redirection with examples. Learn what the STDIN, STDOUT and STDERR are and how they work in Linux along with common file redirectors such
as >, >>, 2>&1, <, /dev/null, /dev/tty1 and /dev/sda in detail.

This tutorial is the last part of the article \”Linux file system and shell explained with file redirectors\”. It explains following RHCSA/RHCE objective.

Use input-output redirection (>, >>, |, 2>, etc.)

Other parts of this tutorial are following.

Linux file system and kernel version explained

This tutorial is first part of the article. It explains Linux file system structure (Linux directory structure) in detail along with naming convention used in kernel name.

Linux shell and command types explained

This tutorial is the second part of the article. It explains shell and its function in detail along with Linux command types such as internal commands, external commands, non-privilege commands and privilege commands.

Input / Output (I/O) Redirection



STDIN, STDOUT and STDERR represent the devices from which shell takes input command and displays result and error respectively. Let’s take a simple example.

STDIN & STDOUT: – User types a command from keyboard (STDIN) at shell prompt and hit the Enter key. If command exists, shell executes that command and displays the result on monitor (STDOUT).

stdin and stdout explained

STDIN & STDERR: – User types a command from keyboard (STDIN) at shell prompt and hit Enter key. If command does not exist, shell displays the error on monitor (STDERR).

stdin and stderr explained

When we access a file, Linux creates an entry point in Kernel which is used to uniquely identify that file in running session.
This identifier is known as file descriptor. File descriptor is a non-negative integer value. First three file descriptors 0, 1 and 2 are reserved for STDIN, STDOUT and STDERR respectively.

File descriptors not only allow shell to accept input commands from any source but also allow shell to send the output or error of these commands on any destination. Unless we manually specify the device for STDIN, SDTOUT and STDERR, shell uses the default devices.

File Descriptor Name Data Flow direction Default Device
0 STDIN (Standard Input) < Keyboard
1 STDOUT (Standard Output) > Monitor
2 STDERR (Standard Error) > Monitor

If non-standard source (STDIN) or destination (STDOUT/STDERR) is used, we have to specify that manually.

Let’s take an example. A script automatically executes, when a user is logged in, and sends its output to the log server.
In this case, since shell is receiving commands from the script instead of a standard input device, it must have to know where it should
send the output generated from those commands.

stdin stdout and stderr explained

For input redirection < symbol is used while > symbol is used for output redirection. When we use input redirection symbol (<),
Linux replaces it with file descriptor and read the script and retrieves the commands as if they were typed from the keyboard.

I/O (Input/output) redirection practical examples



Access shell and run following commands

$ls
$ls > test
$cat test
$echo \"this text will overwrite existing content\"
$echo \"this text will overwrite existing content\" > test
$echo \"this text will append existing content\" >> test
$cat < test

Let’s understand each command in deail.

First command lists the content of current directory.

Second command also lists the content of current directory. But instead of displaying output on monitor (default standard output device), it sends that output in a file named test.

Third command displays the content of specified file on monitor.

Fourth command prints the specified string on monitor.

Fifth command also prints the specified string. But instead of printing it on standard output device (monitor),
it prints that in a file named test. Since the file test already contains data, its content will be overwritten.
We can verify this by running third command again.

Sixth command also prints the specified string in a file named test. But instead of overwriting, this command appends the file.

Seventh command takes its input from file instead of standard input device (keyboard).

Following figure illustrates above practice with output.

file redirector practical example

Common I/O file redirectors

Redirector Description
> Store output in specified file. If file exist, content will be overwritten. If file does not exist, a new file will be created with specified name and output will be stored.
>> Store output in specified file. If file exist, content will be appended. If file does not exist, a new file will be created with specified name and output will be stored.
2>&1 Send error messages and command output on same destination.
< Read command from file instead of keyboard.
/dev/null Send output to null. (Discard the output.)
/dev/tty1 Send output to terminal number one. (Require root permission)
/dev/sda Send output to first hard disk (sda). (Require root permission)

Pipes in Linux

Pipes make I/O redirection more flexible. It allows us to redirect the output of one command into other command as input.

how pipe works in linux at command prompt

Shell allows us to combine multiple commands using pipes. To combine commands use pipe (|) sign between them.

Let’s take an example. The cat command prints the contents of specified file on monitor. The wc command calculates the number of line,
word and characters from the specified file and prints the result on monitor. Both commands need source file to work.

Access shell and run following command.

$cat test | wc

This command redirects the output of first command (cat) in second command (wc) as input.

pipe example

When using pipes, only the output of last command will be displayed on standard output device (monitor).

That’s all for this tutorial. In next tutorial we will learn another RHCSA/RHCE tutorial in detail with examples. If you like this tutorial, please don’t forget to share it with friends.

Full Version EX300 Dumps

Try EX300 Dumps Demo

Categories
RHCE 7 Study Guide

grep options, regex, parameters and regular expressions

This tutorial explains grep command options, search patterns and regular expressions in detail
with practical examples. Learn how grep command search a specified string in a specified location along with grep syntax, and regex.

grep command syntax

The grep command uses following syntax.

#grep [option] [regex pattern] [path]

In this syntax

Parameter Description
option A controller that controls the search operation.
regex pattern A keyword or text string that we want to search.
path The location where we want to search the supplied pattern.

Let\’s understand each parameter in detail.

The grep command options

By default, grep searches the specified pattern line by line in the specified location.
If it finds any match in any line, it prints that line at the terminal. For example, following command
searches the keyword \”user\” in the file \”user-data\” and prints all the lines that contain the keyword \”user\”.

#grep user user-data

Options are used to modify the default behavior of a command. If options are not used, command works normally.
If options are used, it works according to the options. To understand it practically, let\’s take the above example again.

In above example, we searched the keyword \”user\” in file \”user-data\”. Since we did not specify any option,
command worked normally and printed all lines which contain the text string \”user\”.

Now suppose, we want to print all lines which do not contain the text string \”user\” from
the file \”user-data\”. For this, we can use following command.

#grep -v user user-data

Noticed, its the same command which we used above except the
option -v. The option –v instructs grep command to exclude all the lines
which contain the specified search pattern.

Following table lists some most frequently used options with grep command.

The grep command options

Option Description
-i To ignore the case. If this option is used, grep searches the specified pattern in both lower and upper cases. Without this option, grep searches the specified pattern only in the specified case.
-v To exclude the specified pattern. If this option is used, grep prints all the lines which don\’t contain the specified pattern.
-r To search recursively. If this option is used, grep searches the specified pattern not only in specified directory but also in all of its sub-directories.
-A To print additional lines after the matching string.
-B To print additional lines before the matching string.
-o To print only the matching contents.
-e To use multiple regex. To use more than one regex we have to use this option with each regex.
-E To use extended regex. If search pattern contains any meta extended characters, we have to use this option otherwise they will be treated as normal character.

grep regex (regular expression)



A regular expression is a search pattern that the grep command matches in specified file or in specified location.
A pattern can be any text string such as single character, multiple characters, single word, multiple words or a sentence.
It is also known as pattern search, pattern matching, regex or regexp.

To specify a search pattern, we can use two types of characters; literal characters and Meta characters.

A literal character is a regular character and has no special meaning.
A character is considered as a literal character when its actual meaning is used.
For example, we used a character \”A\” in search pattern and grep also treated this character as \”A\” then it
is considered as a literal character.

A Meta character is a character that has two meanings; regular meaning and special meaning.
As per requirement, we can instruct grep to use any one meaning from both meanings.
If regular meaning is used, it will be considered as a literal character while if special meaning is used, it will be considered as a Meta character.

Meta characters are caret (^), dollar ($), input redirect (<), output redirect (>), period (.),
asterisk (*), question mark (?), square brackets ([]), pipe (|), curly brackets ({}), parentheses (()), plus (+) and backslash (\\).

Meta characters are divided in two groups; basic and extend. Basic Meta characters are following

  ^ $ . [ ] *

Extended Meta characters are following.

( ) { } ? + |

We can use the special meaning of basic Meta characters without any option. To use the special meanings of extended Meta characters,
we have to use them with –E option. Unless extended Meta characters are used with –E option, grep treats them as regular characters.
For example, without –E option, plus (+) is a regular character while with –E option it is an extended Meta character.

Special meanings of Meta characters are tool or command specific. The grep command uses Meta characters to
customize the search pattern. Other command may use these for other purpose or for the same purpose.

Following table lists the special meanings of Meta characters.

Meta character Description
^ (Caret) Match expression at the starting of lines
$ (Question) Match expression at the ending of lines
\\ (Back Slash) Turn off the special meaning of the next character
[ ] (Brackets) Match any one of the enclosed characters
[-] Define a range to match
[^ ] Match any characters except which are enclosed in [ ]
| (Pipe) Match both strings
. (Period) Match a single character of any value, except end of the line
* (Asterisk) Match zero or more of the preceding character or expression
\\{x,y\\} Match x to y occurrences of the preceding
\\{x\\} Match exactly x occurrences of the preceding
\\{x,\\} Match x or more occurrences of the preceding

grep search location or path

The grep command can search in any source that contains text such as single text file, multiple files, single directory,
multiple directories, output of other command, an archive, a zip file, etc.

To search in files and directories, you can specify their absolute or relative path. For example,
a user rick wants to search the word jhon in the file userdata which is available in his home directory. He can use any one of following commands.

#grep jhon userdata
#grep jhon /home/rick/userdata

To search in the output of other command, redirect the output of that command in grep command as input.
At shell prompt, pipe sign (|) is used to redirect the output of the first command in to the second command as input.

For example, a user wants to know whether a file named salary exists or not in a directory named database.
The database directory contains more than 1000 files. In this case, he can use the ls command with grep command as following.

#ls database | grep salary

Practice lab for grep command

In order to understand the grep command options and regular expressions practically,
let\’s create a practice lab with some dummy files and directories. This lab is optional.
If you are familiar with grep command, you can skip this lab. The grep command does not change anything itself. It only prints the search result. You can safely use it with existing file system.

If possible, create and use this lab. It not only allows you to practice with grep command in test environment but also helps you in understanding how to use the regular expressions with grep command practically.

Create a directory named \”rhcelab\”. In this directory, create a file named userdata with some dummy user records.

practice lab for grep create directory

Create a directory named \”scriptdir\” and move in it. Create a simple script file named userprofile by redirecting the
output of .bash_profile and .bashrc files. Both files are available in user\’s home directory.

grep practice lab crate script directory

Create a directory named \”html\” and move in it. Copy the /usr/share/doc/HTML/en-us/index.html file in it.

grep option practice directory html

Create another directory named \”custom\” and move in it. Create a file named \”custom-file\” with some dummy contents.

grep practice dirctory custom

That\’s all setup we need for practice. Now let\’s take some examples to know how grep command searches the specified string.

Search exact word or exact match

By default, grep searches for the specified pattern exactly. To print all the lines from a file which contain
the specified pattern, use grep command without any options as following.

#grep pattern file-path

For example, to print all lines which contain the word Sanjay from the file userdata, you can use following command.

#grep Sanjay userdata

grep search text in file

Check whether a specific group exists in system or not

Group database is stored in the file /etc/group. To figure out whether a particular group exist or not, we can use following command

#grep [name of group] /etc/group

If grep displays group\’s record, it exists otherwise it doesn\’t exist. Let\’s take an example.
Check whether the group nfsuser and the group sambauser exist in system or not.

#grep nfsuser /etc/group
#grep sambauser /etc/group

grep check whether group exist or not

As we can see in above output, group nfsuser exists and group sambauser does not exist.

Check whether a particular user exists in system or not

Just like group, we can also check whether a user exist in system or not with grep command.
User database is stored in /etc/passwd file. To figure out whether a particular user exist or not, we can use following command

#grep [name of user] /etc/passwd

If grep displays user\’s record, it exists otherwise it doesn\’t exist. Let\’s figure out, whether the user sanjay and john exist or not.

#grep sanjay /etc/passwd
#grep john /etc/passwd

grep check whether user exist or not

As we can see in above output, user sanjay exists while user john does not exist.

This tutorial is the first part of the article \”grep command in Linux explained with options and regular expressions\”. Other parts of this article are following.

grep command in linux explained with practical examples

This tutorial is the second part of the article. It explains how to use options with grep command in detail with practical examples.

grep regex practical examples of regular expressions

This tutorial is the third part of the article. It explains how to use regular expressions with grep command in detail with practical examples.

use extended regular expressions with grep command

This tutorial is the fourth part of the article. It explains how to use extended regular expressions with grep command in detail with practical examples.

That\’s all for this introductory part. In next part, I will explain how to use options with grep command. If you like this tutorial, please don\’t forget to share it with friends through your favorite social channel.

Full Version EX300 Dumps

Try EX300 Dumps Demo

Categories
RHCE 7 Study Guide

Grep Command in Linux Explained with Practical Examples

This tutorial explains how to use grep command in Linux with practical examples such as performing case insensitive search, printing lines numbers and displaying number of lines before and after every match in grep output.

The grep command allows us to extract any information from any file. As Linux stores settings and configurations in text files, knowing how to use the grep command not only helps in exams but also boosts confidence on job.

This tutorial is the second part of the article \”grep command in Linux explained with options and regular expressions\”. Other parts of this article are following.

grep options, regex, parameters and regular expressions

This tutorial is the first part of the article. It explains grep command options and regular expressions with the special meanings of Meta characters.

grep regex practical examples of regular expressions

This tutorial is the third part of the article. It explains how to use regular expressions with grep command in detail with practical examples.

use extended regular expressions with grep command

This tutorial is the fourth part of the article. It explains how to use extended regular expressions with grep command in detail with practical examples.

For demonstration purpose, I will use the practice lab which I created in the first part of this article.

How to search a sentence or a search pattern that contains white space

To search a sentence or multiple words which contain white space, we have to enclose them with quotes.
If we do not enclose the white space then grep searches only the first word from the specified sentence.
Remaining words are treated as files where grep searches for the specified pattern.
To see it practically, let\’s search a user name Sanjay Gupta in file userdata without quote and with quote.

#grep Sanjay Gupta userdata
#grep \"Sanjay Gupta\" userdata

Following figure shows both commands with output.

grep search multiple words or sentence

As we can see in above output, when we did not enclose the string Sanjay Gupta with quotes,
only the word Sanjay was searched and the word Gupta was taken as file path.
But when we enclosed the string Sanjay Gupta with quotes, it was considered as a search string.

How to perform a case insensitive search with grep



By default, grep matches case. It means, for grep the word sanjay and the word Sanjay are two different words.
If you search the word sanjay, it will not display the lines which contain the word Sanjay.
Vice versa if you search the word Sanjay, it will not display the lines which contain the word sanjay. To see it in action,
let\’s search both words in our practice file userdata.

#grep Sanjay userdata
#grep sanjay userdata

To perform a case insensitive search with grep command, use –i option. The –i option instructs grep command to ignore the
case while matching the pattern. Let\’s perform one more search for the same word sanjay, but this time use –i option with grep command.

#grep -i sanjay userdata

Following figure illustrates above commands with output.

grep case insensitive search

As we can see in above output, without –i option, grep matched exact word but with this option it ignored the case.

How to print line numbers in grep command output

By default, grep does not print line numbers in output. But if require, you can ask it to print the line numbers along with lines.
To print number of lines, use -n option. For example, following command search a keyword Host in file /etc/ssh/ssh_config and print
the output with line numbers.

#grep –n Host /etc/ssh/ssh_config

grep print line numbers

How to search all files and directories recursively with grep command

The grep command supports recursive search. It can search in all files and directories recursively.
To perform a recursive search, use -r option with grep command.

If this option is used, grep first searches all files of the specified directory. If specified directory contains another directory,
it also searches that directory and all of its subdirectories as well.

In our lab, we created a multilevel directory structure. We also created a file named custom_file in the last
directory of this structure. This file contains some dummy text string. Let\’s find that string from the root directory.

#grep -r xxx123 rhcelab

In above command, xxx123 is the search pattern and rhcelab is the main or parent directory.

grep recursive search

If you want to search only in few files, instead of searching in all file recursively you can search only in desired files. The grep allows us to specify multiple files name as arguments.
For example, to search the text string \”test\” in file1, file2, file3 and file4, you can use following command.

#grep test file1 file2 file3 file4

How to print file names which contains the specified pattern with grep command



If you don\’t know which file contains the specified pattern, you can perform a file name search with grep command.
For example, suppose you don\’t know which files in /etc/ directory contain IP configuration or IP related settings.
In this case, you can ask grep to search all files in /etc/ directory for keyword \”IP address\” and print their names at command
prompt with following command.

#grep –r –l \"ip address\" /etc/

grep print file name

The -l option asks grep to print the name of file which contains the specified pattern instead of the lines of that file which contain the pattern.

How to show number of lines before every match

The grep allows us to print the number of lines before every match.
To show the number of lines before every match, use –B option. For example,
following command shows 2 lines before every match.

#grep –B 2 \"DocumentRoot\" /etc/httpd/conf/httpd.conf

grep show line before match

How to print next lines after every match

Just like the previous lines, you can also print next lines after every match.
To print the next lines after every match, use –A option with grep command.
For example, following command print next 2 lines after every match.

#grep –A 2 \"DocumentRoot\" /etc/httpd/conf/httpd.conf

grep show line after match

How to show line before and after every match

Since grep allows us to specify multiple options, we can combine both –A and –B options to print the number
of lines before and after every match. For example, following command prints 1 line before and after every match.

#grep –B 1 –A 1 \"DocumentRoot\" /etc/httpd/conf/httpd.conf

grep show lines before and after match

That\’s all for this part. In next part of this tutorial, I will explain how to use regular expressions with grep command in detail.
If you like this tutorial, please share it with friends through your favorite social platform.

Full Version EX300 Dumps

Try EX300 Dumps Demo

Categories
RHCE 7 Study Guide

grep regex Practical Examples of Regular Expressions

This tutorial provides 10+ practical examples of regular expressions or regex such as search lines which start or end with a specific word or pattern, remove blank or empty lines, use multiple regex, search multiple words, etc.

This tutorial is the third part of the article \”grep command in Linux explained with options and regular expressions\”. It explains following RHCSA/RHCE exam objective.

Use grep and regular expressions to analyze text

Other parts of this article are available here.

grep options, regex, parameters and regular expressions

This tutorial is the first part of the article. It explains grep command options and regular expressions with the special meanings of Meta characters.

use extended regular expressions with grep command

This tutorial is the second part of the article. It explains how to use options with the grep command in detail with practical examples.

use extended regular expressions with grep command

This tutorial is the fourth part of the article. It explains how to use extended regular expressions with grep command in detail with practical examples.

grep regex (search lines which start with a specific word or pattern)

By default, grep searches the specified pattern or regular expression in entire line.
To instruct it to search the specified pattern only at the starting of the lines,
a Meta character ^ is used with the pattern. Let\’s take an example.
Search all lines which start with # sign from a file named user_profile.

#grep ^# user_profile

grep search lines start with specific pattern

This type of regular expression is commonly used when working with configuration files or shell scripts.
Let\’s take two more examples which use this regex.

grep regex (view or print only comments lines from a configuration or script file)



In a well written configuration or script file, comments provide detailed information about that file.
A line which starts with # is treated as comment line in these types of files.
While processing a script or configuration file, Linux ignores all the lines which start with # sign.

As explained above, we can use a regular expression ^# to print all lines which start with # sign.
Since a line which starts with # is treated as comment line, this regular expression is used to view or
print all comments line form a configuration or script file.

Let\’s print the comment only from the configuration file /etc/yum.conf.

#grep ^# /etc/yum.conf

grep show only comments from a file

grep regex (view or print only configurations lines from a configuration or script file)

Unlike above example, if we are interested only in configuration values, we can instruct grep
command to perform search operation in reverse. To perform search operation in reverse,
the option -v is used with grep command.

For example, the search pattern ^# asks grep command to print all lines which
start with # sign. If we use this pattern with -v option, grep will print all lines except which start with # sign.

In above example, we printed all comment lines from the file /etc/yum.conf. Now let\’s print all lines except comment lines.

#grep -v ^# /etc/yum.conf

grep exclude search pattern

Grep regex (Search the lines which end with a specific word or pattern)



To instruct grep command to look the specified pattern only in the end of the line,
a Meta character $ is used with the regular expression. Let\’s take an example.
Search users who work in sales department from the file userdata.

#grep sales$ userdata

You can also use this regex with –i option, to perform search in both cases; upper case and lower case.

#grep -i sales$ userdata

grep search end with specific pattern

Let\’s take one more example of this search pattern.

grep regex (print the list of users who use bash shell)

The /etc/passwd file stores users\’ information. Each line in this file represents an
individual user account and contains information only about that account in colon separated columns.
Shell information is stored in the last column of the line.

Since shell information is stored in the end of the line, we can instruct grep command to
look for the specified search pattern only in the end of line. The Meta character $ is used to
force the grep command to search the specified pattern only in the end of the lines.

To print the list of users who use the bash shell, you can use the regular expression bash$ as following.

#grep bash$ /etc/passwd

grep search users who uses bash shell

Grep pattern (Remove blank lines or empty lines from a file)

To remove blank lines or empty lines, two Meta characters the ^ and the $ are used
together in a regular expression. The Meta character ^ stands for starting of the line while the Meta
character $ stands for the ending of the line.

If we use ^ [starting point] with $[ending point] as ^$ in a
regular expression, it says there should be nothing in the line. Since there is nothing in the line,
it will be a blank line. For example, following grep command prints all blank lines from the file user_profile.

#grep ^$ user_profile

Once you know how to print the empty lines, you can easily reverse it with -v option.
The -v option instructs the grep command to do the opposite of what search pattern says.
The search pattern ^$ says print all blank lines. But if you use it with -v option, it will say print all line except the blank lines.

#grep -v ^$ user_profile

Left pane of the following figure shows a default script file.
Right pane shows how grep command searches the pattern ^$, with and without -v option.

grep remove blank lines

This command may not work if file is created on Windows system. To remove blank lines from a
file which is created on other systems except *nix, use \’^[[:space:]]*$\’ regular expression.

How to use multiple regular expressions with grep command

We can use a single regular expression without any option.
To use multiple regular expressions, we have to use -e option with each regular expression.
We used ^$ regex (regular expression) to filter the blank line. We also used ^# to filter the comment line.
Let\’s combine both regex to filter the blank lines and comment lines in one go with –v option.

#grep –v –e ^$ -e ^# user_profile

grep use multiple regular expressions

How to search in the output of the other command with grep command

Usually grep is used to search in files. But it can also be used to search in the output of other command.

By default, shell displays the output of a command at command prompt.
Instead of displaying it at command prompt, we can instruct shell to redirect it to other command as input.
For this, we have to connect both commands with a pipe sign. A pipe sign asks shell to redirect the output of
first command (left side command) to the second command (right side) as input.

Let\’s take an example. Figure out whether a particular rpm package is installed in system or not.

The rpm –qa command lists all installed packages in system. We can redirect the output of this
command to the grep command. Later, we can instruct grep command to search the specified package in the output.

Let\’s check, whether the package vsftpd is installed or not by following this approach.

#rpm -qa | grep vsfptd

grep check rpm is installed or not

That\’s all for this part. In next part, I will explain how to use extended regular
expressions in detail with practical examples. If you like this tutorial, please don\’t forget to share it from your favorite social channel.

Full Version EX300 Dumps

Try EX300 Dumps Demo

Categories
RHCE 7 Study Guide

Use Extended Regular Expressions with grep command

This tutorial explains how to use extended regular expressions with grep command in detail. Learn what extended regular expressions are and how they work with grep command through a practical example that extracts all links form a html file.

Extended regular expressions

A regular expression is a search pattern that grep command matches in specified file or in provided text. In order to allow a user to express the regular expression in more customized way, grep assigns special meanings to few characters. These characters are known as Meta characters. Initially, grep assigned the characters ^ $ . [ ] and * as Meta characters. Later few more characters were added in this list. These were ( ) { } ? + and |.

Based on the use of Meta characters, a regular expression can be divided in two categories; BRE (Basic Regular Expression) and ERE (Extended Regular Expression).

Basic Regular Expression: – An expression which uses the default Meta characters.

Extended Regular Expression: – An expression which uses the later added Meta characters.

This tutorial is the last part of the article \”grep command in Linux explained with options and regular expressions\”. Other parts of this article are following.

grep options, regex, parameters and regular expressions

This tutorial is the first part of the article. It explains grep command options and regular expressions with special meanings of Meta characters.

grep command in linux explained with practical examples

This tutorial is the second part of the article. It explains how to use options with grep command in detail with practical examples.

grep regex practical examples of regular expressions

This tutorial is the third part of the article. It explains how to use regular expressions with grep command in detail with practical examples.

How to use extended regular expression



Extended regular expression uses the Meta characters which were added later.
Since later added characters are not defined in original implementation, grep treats them as regular
characters unless we ask it to use them as Meta characters.

To instruct grep command to use later added characters as Meta characters, an option –E is used.
Let\’s take an example. In original implementation, the pipe sign (|) is defined as regular character
while in new implementation, it is defined as a Meta character.

If we use pipe sign without –E option, grep will treat it as a regular character.
But if we use it with –E option, grep will treat it as a Meta character.
As a Meta character, it is used to search multiple words. Let\’s search two users\’ information in file /etc/passwd with and without –E option.

#grep \"sanjay|rick\" /etc/passwd
#grep –E \"sanjay|rick\" /etc/passwd

how to use extended regular expression

Without –E option, grep searched the pattern as a single word
sanjay|rick in the file /etc/passwd. While with –E option,
it separated the pattern in two words sanjay and rick and searched them individually.

grep extended regex (search multiple words)



The pipe sign (|) is used to search multiple words with grep command.
To search multiple words with grep command, connect all of them with pipe sign
and surround by quote signs. For example to search words abc, fgh, xyz, mno and jkl, use the search pattern \”abc|fgh|xyz|mno|jkl\”.

 #grep –E \"abc|fgh|xyz|mno|jkl\" dummy-file

grep extended regex (search all links with linked text from an html file)

To extract all links from an html file named html_file, use following command.

#grep –Eoi \'<a[^>]+>.*</a>\' html_file

Let\’s understand above command in detail.

grep command options

-E: – This option instructs grep command that search pattern contains the Meta characters which were added later.

o:- By default, grep prints entire line which contains the search pattern. This option instructs grep command to
print only the matching words instead of entire line.

i:- This option ask grep command to ignore the case while matching the pattern.

Extended regular expression

<a :- Starting point of anchor tag.

[^ >] :- Match everything except >.

+ :- Match preceding one or more time.

> :- Ending point of anchor tag.

So far this search string says, search for the text which starts with <a and contains anything after it except >
sing (because this sign is used to end the tag and we need some values before end otherwise it will become <a >
which is not a valid anchor tag
) and ends with > sign.

This string is followed by a Meta character + which instructs grep command to match it one or more times.

Meta character dot (.) represents any single character and star (*) represents any number of characters.
We used both together to search for any characters between starting and closing anchor tag.

</a> :- This is the closing point of anchor tag.

Collectively above search patterns says search a text string which

starts with <a
and contains some value
and ends with >
and again contains any value
and ends with </a>

In more simple language <a some value > any value </a>.

Following figure illustrates the use of above regex

search all anchor tag in html file

grep regex (print only anchor tag)

If you are interested only in anchor tags, you can exclude the expression which prints the linked text as following.

#grep –Eoi \'<a[^>]+>\' html_file

grep show only href tag

grep regex ( extract all links or URLs from an html file and save them in a text file)

To extract all links or URLs from an html file and save them in a text file, we have to combine three commands. These commands are: –

#grep –Eoi \'<a[^>]+>\' html_file
#grep -Eo \'href= \"[^\\\"]+\" \'
#grep –Eo \'(http|https)://[^/\"]+1 > link-only

We have to combine these commands in following way.

#grep –Eoi \'<a[^>]+>\' html_file | grep -Eo \'href= \"[^\\\"]+\" \' | grep –Eo \'(http|https)://[^/\"]+1 > link-only

In above commands,

  • First command receives its input from file named html_file, second command receives its input form first
    command and third command receives its input from second command.
  • First command extracts all anchor attributes from html file and sends output to the second command instead of printing it at command prompt.
  • Second command extracts all href tags from the output of the first command and sends output to the third command.
  • Third command extracts all links from the output of the second command and save output to a text file named link-only.

Following figures explains above commands with output.

grep save all links from a html file

That\’s all for this tutorial. If you like this tutorial, please don\’t forget to share with friends through your favorite social site.

Full Version EX300 Dumps

Try EX300 Dumps Demo

Categories
RHCE 7 Study Guide

Compress and Decompress Files in Linux with gzip and bzip2

This tutorial explains how to compress and decompress files in Linux along with the similarities and differences between gzip and bzip2 commands. Learn how to use the gzip, bzip2, gunzip and bunzip2 commands in Linux with practical examples.

A compressed file not only uses less disk space but also consumes less memory and network bandwidth when moved to another location. Linux contains several compression utilities. Among those, this tutorial discusses two most popular utilities; gzip and bzip2.

Similarities between gzip and bzip2

Both commands not only works in similar fashion but also use similar syntax and options to compress and decompress the files. For example, the gzip command uses following syntax.

#gzip [option] [file]

Just like the above syntax, the bzip2 command uses following syntax.

#bzip2 [option] [file]

Once compression is done, both commands replace the supplied source file with the compressed file. To decompress the compressed file, both commands also offer individual commands. These commands are gunzip and bunzip2 for gzip and bzip2 respectively. To decompress the compressed file, we can use the corresponding command or can use command’s inbuilt functionality.



Following table lists supported options and their descriptions.

Short option Long option Supported command Description
-h –help Both List all supported options
-d –decompress Both Decompress the compressed file
-f –force Both Overwrite existing output file
-t –test Both Test compressed file integrity
-c –stdout Both Write output to standard output device
-q –quiet Both Don’t display noncritical errors and warnings
-v –verbose Both Display verbose messages
-L –license Both The bzip2 displays both software version and license information. The gzip displays License information only.
-V –version Both The bzip2 displays both software version and license information. The gzip shows version information only.
-1 –fast Both The bzip2 sets block size to 100k. The gzip compresses faster
-9 –best Both The bzip2 sets block size to 900k. The gzip compresses better.
-z –compress bzip2 only Force compression
-k –keep bzip2 only Keep original file.
-s –small bzip2 only Use less memory
-l –list gzip only Display compressed and decompressed size
-n –no-name gzip only Do not save or restore original name and time stamp
-N –name gzip only Save or restore original name and time stamp
-r –recursive gzip only Operate recursively on directories
-S –suffix=SUF gzip only Use suffix SUF on compressed files

As we can see in above table: –

  • The options -h, -d, -f, -t, -c, -q and -v similarly work in both commands.
  • The options -1, -9 -L and -V work slightly different in both commands.
  • The options -z, -k and -s work only in the bzip2 command.
  • The options -l, -n, -S, -N and -r work only in the gzip command.

Besides command line options, there are few more differences between both commands. Following table lists those differences.

Differences between gzip command and bzip2 command

The gzip command The bzip2 command
It uses the DEFLATE algorithm. It uses the Burrows-Wheeler block sorting algorithm.
To denote the compressed file, it uses the extension .gz. To denote the compressed file, it uses the extension .bz2.
It compresses files at higher speed in comparison with the bzip2 command. It provides higher compression ratio in comparison with the gzip command.
It doesn’t provide any inbuilt functionality or associate program to recover the damaged .gz files. It provides an additional program bzip2recover that can recover the damaged .bz2 files.
For decompression, it provides the utility gunzip. For decompression, it provides the utility bunzip2.
It supports recursive compression. It doesn’t support recursive compression.

This tutorial is the first part of the article \”Compressing and archiving explained in Linux\”. This tutorial explains following RHCSA/RHCE topic.

Archive, compress, unpack, and uncompress files using tar, star, gzip, and bzip2

Other parts of this article are following.

Tar command and Syntax Explained with Examples

This tutorial is the second part of the article. It explains basic usages of tar command with syntax and options.

Tar Command Examples in Linux

This tutorial is the last part of the article. It explains how to use the tar command in Linux with practical examples.

gzip, bzip2, gunzip and bunzip practical examples



Although both gzip and bzip2 commands use fairly simple and straightforward options,
still if you forget any option or have any confusion about any option, you can list all supported options with the -h option.

To list all supported options of gzip command, use following command

#gzip –h

gzip command help

To list all supported options of gzip command, use following command

#bzip2 -h

bzip2 command help

Compressing and decompressing files

Compressing and decompressing files with gzip and bzip are relatively simple.
To compress a file, simply specify its name (if file is located in same directory) or
full path (if file is located in other directory) with these commands. For example to compress
a file named file_a, we can use any one command from following commands.

#gzip file_a
#bzip2 file_a

As explained earlier, both commands replace the supplied file with the compressed file.
So if we use gzip and bzip2 for compression, the supplied file file_a will be replaced with
the compressed file file_a.gz and file_a.bz2 respectively.

To decompress the compressed file, we can use -d option with both commands
or can use gunzip command and bunzip command if file is compressed with gzip and bzip2 respectively.
For example, to decompress the file file_a.bz2, we can use any one command from following commands

#bzip2 -d file_a.bz2
#bunzip file_a.bz2

To decompress the file file_a.gz, we can use any one command from following commands.

#gzip -d file_a.gz
#gunzip file_a.gz

Following figure shows compression and decompression with gzip and gunzip commands.

compression and decompression with gzip command

Following figure shows compression and decompression with bzip2 and bunzip commands.

compression and decompression with bzip2

gzip vs bzip2 which provides higher compression ratio

The bzip2 provides higher compression ratio but take more time in compression.
To verify it practically, let’s compress a file with both commands and compare the file size of compressed file.

comprare gzip with bzip2

As we can see in above figure, the file compressed with gzip is larger in size than the file compressed with bzip2.

It clearly shows that bzip2 provides more compression ratio than the gzip. If you need more proof, you can perform the same compression with -v option.

gzip vs bzip2

As we can in above figure, when we compressed the file file_a with bzip2, compression ratio was 62.58%.
While when we compressed the same file with gzip, the compression ratio was 61.6%.

Redirecting output to a device or file

As we have seen above, by default both commands store output to a new compressed file.
And once compression is done, both commands replace the supplied file with the compressed file.

If require, we can store output to any device, file or custom location.
To send the output at custom location, the option –c is used. The option -c forces command to
send output at standard output device (console) and keeps the original file intact.

Following figure shows an example. In this example, a small file is created and gzip command with option -c is used to compress it.

save gzip output to custom location

As we can see in above figure, if option -c is used, command writes output to the console.

We can use shell redirector (>) to store output in custom location.
For example, following command compresses two files; small-file and small-file-2 in supplied sequence and
writes the output to a new file small.gz.

#gzip -c small-file small-file-2 > small.gz

redirect output of gzip command

You can also use this feature to create a single compressed file from multiple files.

Getting information from a compressed file

The gzip command, if used with -l option, scans the supplied compressed file and lists following information about that file.

Compressed size, uncompressed size, compression ratio and uncompressed name

display compressed and decompressed size of file

This option only work with gzip command. The bzip2 doesn’t support this option.

Compressing files recursively

Use -r option with gzip command, to scan and compress all files from a directory and all
of its sub-directories. For example, following command not only compresses all files
of the directory named a_dir and but also recursively scans all of its sub-directories.
If it finds any file in any sub-directory, it will also compress that file.

#gzip -r a_dir

We can also use this option with gunzip command to decompress all files form a directory and all of its sub-directories recursively.

#gunzip -r a_dir

compress directory with gzip

The bzip2 command neither supports this option nor provides any other option for recursive operation.

Keeping original file intact

By default, the bzip2 command replaces supplied input file with compressed output file.
To keep input file intact, use -k option with bzip2 command.
For example, following command keeps supplied file file_a along with the compressed output file.

#bzip2 -k file_a

keep original file after zip

This option only works with bzip2 command. The gzip command does not support this option.

Recovering damage compressed file

To recover the damage compressed file, bzip2 provides a separate tool known as bzip2recover.
This tool scans damage file, skips corrupt data blocks and copy correct data blocks in a new file.
To understand how this tool works, let’s take an example.

Create a compressed file with bzip2 and open it with a text editor. Add an extra line and save it.
Now as, it contains text in both formats; compressed and decompressed, bzip2 treats it as a corrupt compressed file.

To repair this file, we can use bzip2recover tool. Once file is repaired, it can be decompressed with the bzip2.

Following figure shows this exercise.

recovering corrupt zip file

The bzip2recover tool works only with bzip2 compression. A file that is compressed with any other utility or tool can’t be repaired with it.

Adjusting speed and compression ratio

We can adjust the speed and the compression ratio in both commands.
Both commands supports a scale of 1 to 9 where number 1 provides the highest speed but the lowest
compression ratio while number 9 provides the highest compression ratio but the lowest speed. Compression ratio works inverse of the speed.

Default value is 6. To use any other value, specify that value as option.

Following figure shows, how changing this value can impact the compression ratio.

adjust compress ratio and speed

Compressing an already compressed file

When we compress a file, the information that is required to decompress it, is also stored with compressed data. If we compress it again, this information will be added again. Since data has already compressed in first time, it will remain unchanged in second time. So if we compress an already compressed file, we end up with a large file.

Although it’s a waste of time and space, but if require, you can compress an already compressed file again with -f option.

Let’s take an example. Compress a file with gzip and note down its size. Now compress it again. Since file has already compressed, gzip will not compress it again. Use option -f, to force it. Once file is compressed again, compare its size with noted size.

Following figure shows this exercise.

compressing an already compressed file

A file compressed two times, also need to be decompressed two times.

That’s all for this tutorial. If you like this tutorial, please don’t forget to share it with friends through your favorite social channel.

Full Version EX300 Dumps

Try EX300 Dumps Demo

Categories
RHCE 7 Study Guide

Tar Command Examples in Linux

Learn how to use the tar command in Linux with practical examples such as adding or updating files in
existing archive, removing and extracting a single file from the archive and preserving SELinux contexts in archiving process.

Lab setup for practice

Create a new directory and create some files and few sub-directories with files in this directory.

lab setup for tar command practice

Create an archive data.tar with few files.

create tar archive

That’s all setup we need for the practice.

This tutorial is the last part of the article \”Compressing and archiving explained in Linux\”. This tutorial explains following RHCSA/RHCE topic.

Archive, compress, unpack, and uncompress files using tar, star, gzip, and bzip2

Other parts of this article are following.

Compress and Decompress Files in Linux with gzip and bzip2

This tutorial is the first part of the article. It explains how to compress and decompress files in Linux with gzip and bzip2.

Tar command and Syntax Explained with Examples

This tutorial is the second part of the article. It explains basic usages of tar command with syntax and options.

Adding new file in exiting archive file



To add an additional file in existing archive, use the options -rvf and specify the new file name as argument.
For example, following command adds the file c in the existing archive data.tar.

#tar -rvf data.tar c

New file will be added in the end of the archive.

adding new file in existing tar archive

Removing or deleting files from an archive

To delete a file from archive, use the option –delete. For example,
to delete a file named b from the archive data.tar, use the following command.

#tar --delete b -vf data.tar

removing a file from archive tar example

Updating files in existing archive



To add only those files which are modified or created since the archive was created, use the option u.

This option compares the supplied files and directories as arguments with the files
and directories stored in the supplied archive. If it detects any change in any file or finds any new
file and directory, it appends that file and directory in the end of the archive.

Since this option, instead of replacing the existing version of the file, adds the
updated version of file as a separate file in end of the archive, the original version of file also remains in the archive.

This feature allows us to store multiple copies of the same file where first copy
contains the original data and all later incremental copies contains the original data
plus the data which was added since the last incremental copy was created.

To view how many incremental copies of a file are stored in an archive,
we can use option t. The option t lists the name of files and directories stored in the
archive. If a file is listed two or more times, it simply means that the file has been changed since the archive was created.

In extraction process, files are extracted in the same sequence in which they were added in the
archive. It means, original copy extracts first and incremental copies extract later in their respective sequence.

By default, extraction process overwrites existing files. Due this feature, the original file which extracts
first is overwritten with the updated file which extracts later.

To understand this process practically, create an archive file from a directory which contains sub-directories
with files. Once archive is created, run update process immediately.

Since nothing has changed since the archive was created, update process adds nothing in archive.

Now change a file and run the update process again. This time, since a file has changed,
the update process adds that modified file in the end of the archive.

List the contents of archive with options -tvf, to verify that updated file has added in the end of the archive.

Remove the original directory. It helps us in understanding how the extraction process works with an archive that contains incremental copies.

Now extract the archive and list the directory which contains modified file. It should contain only the updated version of the file.

Following figure illustrates this example step by step.

updating archive file

Compressing and decompressing archive

By default, the tar command doesn’t perform compression on archived files.
But if require, it can use the third party compression utility such as gzip and bzip2 to compress and decompress the archived file.

The gzip compress faster but provides low compression ratio. To create and compress the archive file with gzip utility,
use the option g with options cvf as cgvf and use the file extension .gz with the archive file name.

The bzip2 compress slower but provides high compression ratio.
To create and compress the archive file with bzip2 utility, use the option j with options cvf as cjvf and
use the file extension .bz2 with the archive file name.

Following commands create the archive home.tar from the /home directory without compression,
with gzip compression and with bzip2 compression.

#tar -cvf /tmp/home.tar /home
#tar -czvf /tmp/home.tar.gz /home
#tar -cjvf /tmp/home.tar.bz2 /home

To view the size of compressed archive file, once compression is done, use du command with the option h.

compressing archive file with gzip and bzip2

Once an archived file is compressed, advance operations such as append, update and individual delete can’t be performed with it.

no advance options can be perform with compressed file

To extract a compressed archive, use the same option which you used to create it.
For example, if archive is compressed with gzip, use the option z with the options xvf as xzvf.
And if archive is compressed with bzip2, use the option j with the options xvf as xjvf.

Alternatively, you can skip the compression related option (such as g and j)
and let the tar to automatically detect and use the correct compression utility to decompress the compressed archive.

For example to extract the compressed archive data.tar.gz, you can use any one command from following commands.

#tar -xvf data.tar.gz
#tar -xzvf data.tar.gz

If you are not sure which compression utility was used to compress the archive file,
don’t specify the compression related option. If compression related option is specified,
the tar command uses the utility that is associated with that option. And if that utility is incorrect, archived file is not decompressed.

For example, following command will fail, as it instructs tar to use the bzip2
utility to decompress the archived file that is compressed with the gzip utility.

#tar -xjvf data.tar.gz

extracting a compressed file

Preserving SELinux context in archive

File attributes are used to store additional information about the file.
There are two types of attributes; regular attributes and extended attributes.
Regular attributes contain the essential information such as owner info, access permission, created date etc. Extended attributes contain the metadata information such as access control list, SELinux etc.

By default, the tar command doesn’t retrain the extended attributes.
If we want to preserve the extended attributes, we have to instruct the tar command to do the same.
For example, if you want to preserve the SELinux contexts, use the option –selinux.

Let’s take an example, to understand how it works practically.

  • Create two directories in our practice directory.
  • Switch to directory /var/www/html and create few files.
  • Create two archive files with the files which created in above step.
    While creating second archive file, use the option –selinux before the options -cvf.
  • Copy both archive files in the directories created in first step respectively.
  • Switch in both directories and extract the copied archive files.
  • While extracting the archived file which was created with the
    option –selinux, use the same option –selinux again before the options -xvf.

Following figure illustrates above process step by step.

preserve selinux contexts in tar archive

Now compare the SELinux contexts of extracted files with the SELinux contexts of the original files.

tar command selinux example

As we can see in above figure, an archive created and extracted with –selinux option preserves the SELinux contexts.

The tar command v/s star command

The original version of tar does not support the extended attributes. To support the extended attributes, an enhanced version of tar, known as star, was designed. Besides the options which require for extended attributes, it uses the same options for creating, listing, appending, updating and extracting the archive. Later, the support for extended attributes was also added in the original version of tar. After this update, there is no significant difference between both commands.

You can perform almost all tasks, which you can perform with the star command, with the tar command also. To support the tar command, star command is not installed by default. In exam, unless you are explicitly asked to use the star command, there is no need to install and use the star command. You can use the tar command to perform all archive related tasks.

That’s all for this article. If you have any suggestion, feedback or comment about this article, let me know. If you like this article, please don’t forget to share it with friends through your favorite social network.

Full Version EX300 Dumps

Try EX300 Dumps Demo

Categories
RHCE 7 Study Guide

Differences between Absolute path and Relative path in Linux

This tutorial explains what the absolute path and the relative path names are in Linux with examples. Learn the differences between the absolute path and the relative path along with the meaning of single dot and double dots in pathname.

Linux file system is built from files and directories. Files are used to store the data. Directories are used to organize the files systematically. The directory root (/) is the main directory in Linux. All directories and files are created and managed under this directory. The location of a file or directory from this directory is known as the path of that file or directory. A path, based on how it is written, can be categorized in two types; absolute path and relative path.

Absolute Path

Absolute path starts from the directory root (/) and goes up to the actual object (file or directory). It contains the names of all directories that come in the middle of the directory root and the actual object. In this, name of the parent directory is written in the left.

Let’s take an example, suppose a user named sanjay creates a directory named test in his home directory. What will the absolute path of this directory?

To write the absolute path of this directory, we have to start writing the path from the directory root. The directory root is written as / (forward slash).

After / (root directory), we have to write the name of the directory in which user’s home directory is located.
By default, Linux places user’s home directory in the directory named home. Usually, this directory is created just under the directory root (/).

If we write the name of the home directory just after the root directory, we get the absolute path of the home directory.

If we write the name of the user’s home directory just after the absolute path of the home directory, we get the absolute path of the user’s home directory.
By default, Linux uses user’s username to create user’s home directory. In this example, username is sanjay, hence the name of its home directory is also sanjay.

Following the same track, if we write the name of the directory or file which is created in user’s home directory just after the
absolute path of the user’s home directory, we get the absolute path of that directory or file.

In this example, absolute path of the directory test will be; /root/home/sanjay/test.

example of absolute path

Key points
  • First forward slash (/) in the absolute path represents the directory root. Besides this, all slashes in the path represent the directory separator.
  • Besides the last name, all names in the absolute path belong to directories. Last name can belong to file or directory.
  • In the absolute path, directories names are written in their hierarchy order. Parent directory’s name is written in the left side.
  • Absolute path does not change when we change the current directory.
  • To know to the absolute path of the current directory, we can use the command pwd.

Relative path



Relative path starts from the current directory and goes up to the actual object. Relative path depends on the current directory. When we change the directory, relative path also changes. Just like the absolute path, the name of the parent directory is written in the left side. Unlike the absolute path, all slashes in the relative path represent the directory separator.

Before we take the examples of the relative path, let’s understand the special meanings of single dot and double dots used in the relative path.

Single dot (.) and double dots (..) in Linux

In Linux every directory contains two dots; single dot and double dots. When a directory is created,
both the single dot and the double dots are also automatically created in it. By default these dots are hidden and do not show in the output of the command ls. To view these dots, we have to use the option a with the command ls.

The single dot refers to the directory itself and the double dots refers to its parent directory or the directory
that contains it. Shell allows us to access the current directory and the parent directory by using the single dot and the double dots respectively.

Relative path also uses these dots to represent the current directory and the parent directory respectively.
With the use of these dots, we can build the relative path of any file or directory from the current directory.

Following figure shows the relative path of the directories used in previous example.

example of relative path

Let’s take another example. Suppose a user want to access a file that is available two directories above in hierarchy from his
current directory. To access this file, he can use the following relative path.

../../file

Just like it, if the file is available in three directories above in the hierarchy, he can use the following path.

../../../file

Relative path of the file or directory that is below in hierarchy always starts with a single dot followed by a forward slash as ./.
The ./ represent the current directory.

Unlike the parent directory, no symbol (dot or dots) is used for the child directory. If the file or directory is available in the directory that
is below in the hierarchy, we must have to use the actual names of child directories in path.

For example, a file named abc is available in the directory named dir1 and the directory dir1 is available in the current directory, relative path of this file will be following.

./dir1/abc

To execute a command, Shell uses current directory as the default directory.
Because of this, if the target object is available in the child directory of the current directory, we can omit the leading ./ from the relative path.
For example, to access the above file we can also use the following path.

dir1/abc

In some situations, skipping current directory from the relative path makes it ambiguous.
In such situations, we must have to use the full relative name including the current directory. For example, to run a
script from the current directory, we must have to use the full relative name.



Let’s take few more examples to understand absolute path, relative path and the use of dots in the relative path practically.

Access Shell prompt and create a directory named dir1. Create a file named abc in this directory.
Also create a simple script named simple.sh in the current directory.

Now run the commands listed in the following table.

Command Description Path
cat ./dir1/abc Print the contents of the file abc. Use relative path. Include current directory
cat dir1/abc Print the contents of the file abc. Use relative path. Skip current directory
cd ./dir1 Change current directory to dir1 Use relative path.
cd .. Change current directory to parent directory Use relative path.
cd /home/sanjay/dir1 Change current directory to dir1 Use absolute path.
cp ./dir1/abc . Copy the file abc in current directory Use relative path.
./simple.sh Run script from current directory Use relative path

Following figure illustrates above practice.

absolute path and relative path examples

That’s all for this tutorial. If you like this tutorial, please don’t forget to share it with friends through your favorite social network.

Full Version EX300 Dumps

Try EX300 Dumps Demo

Categories
RHCE 7 Study Guide

How to use the vi and vim Editors in Linux

This tutorial explains how to perform essential editing tasks in vi and vim editors.
Learn how to copy, paste, search, replace, insert, append, delete, undo and redo in vi and vim.

This tutorial is the second and last part of the article \”How to use vi and vim commands in Linux\”.
For this tutorial I assume that you know what the vi and vim editors are and how they work in Linux. To learn about these editors,
see the first part of this tutorial. First part of this tutorial is available here.

Vi and Vim Editor Explained with Examples

Inserting and appending text

The vi (vim) editor uses cursor position as the base point to calculate the editing location in file.
With proper command you can insert and append text exactly where you want from the cursor position.

Following are the some common positions, along with the required commands, where you can insert or append the text.

  • To insert the text before the cursor, use the Escape + i keys combination.
  • To append the text after the cursor, use the Escape + a keys combination.
  • To insert the text in the beginning of line, use the Escape + I keys combination.
  • To insert the text in the end of line, use the Escape + A keys combination.

Following image shows how to insert and append text in vi (vim) editor with practical examples.

how to insert and append in vi

Inserting new blank line



  • To insert a new blank line above the line, use the Escape + O keys combination.
  • To insert a new blank line below the line, use the Escape + o keys combination.

By default only one line will be inserted. To insert multiple lines, we have to specify the number of
lines before O/o key. For example to insert 3 blank lines above the cursor line, we have to use the Escape + 3 + O keys combination.

Following image shows practical examples of inserting new lines in vi (vim) editor.

how insert new lines in vi

Performing cut and paste

To cut a line, use the dd command. This command cuts the line in which cursor remains and puts that line in memory buffer.
To paste the copied line from buffer, use the p command. This command pastes the copied line in the current line of cursor.

By default, both commands perform their action only one time. It means, the dd command cuts a single line and the p command pastes
the copied line one time only.

To cut the multiple lines or to paste the contents from memory buffer multiple times, we have to specify the desired numbers before these commands.

For example to cut three lines, use the Escape + 3 + d + d keys combination. To paste the contents four times, use the Escape + 4 + p keys combination.

Following image illustrates the cut and paste operation practically.

cut and paste in vi

Performing copy and paste



Copy and paste operation also works similar to the cut and paste operation.

To copy a line, use the yy command. This command copies current line in the memory buffer. To copy multiply lines, specify the
number of lines before the yy command. For example to copy 4 lines, use the Escape + 4 + y + y keys combination.

Just like line, you can also copy a single or multiple characters and words. To copy a single character from the cursor, use the
yl command. To copy a single word from the cursor, use the yw command.

To paste the copied contents, use the same p command.

Following image illustrates the copy and paste operation with example.

copy and paste in vi

Finding text

To find a text string in forward direction, the Escape + / + [String] command is used. To find a text string in backward direction,
the Escape + ? + [String] command is used.

To navigate between search results, the n key is used with the Escape key. To move in backward direction, the Escape + N key is
used while to move in the forward direction, the Escape + n key is used.

Following image illustrates the search operation practically.

how to search text in vi

Finding and replacing text

To replace the first occurrence of text, use the following keys combination.

Escape + : + % + s + / + [old text] + / [new text]

Here old text is the text which you want to replace and new text is text from which you want to replace the old text.

To replace all occurrences of text, use the following keys combination.

Escape + : + % + s + / + [old text] + / [new text] + / +g

Following image illustrates the find and replace operation with example.

find and replace in vi

Deleting text

To delete a single character from the cursor in forward direction, use the Escape + x keys combination.
To delete a single character from the cursor in backward direction, use the Escape + X keys combination.

Following image shows the delete operation practically.

delete text in vi

  • To remove a single word after the cursor, use the Escape + d + w keys combination.
  • To remove a single word before the cursor, use the Escape + d + W keys combination.
  • To delete a line, use the Escape + d + d keys combination.
  • To delete multiple lines, specify the number of lines before the dd command.

delete word

Reverting (undo / redo) last command or action

To undo last action or command, use the Escape + u keys combination. To undo all actions performed in current line,
use the Escape + U keys combination. To redo last command, use the Escape + Ctrl + r keys combination.

Following image illustrates the undo / redo operation practically.

undo redo in vi

I used dd command to perform the action for demonstration.

Setting line numbers

By default line numbers are not displayed. But if required, we can display them with the Escape + : +set +nu command.

Following image show how to set the line numbers in vi (vim) editor practically.

setting line numbers

Save and exit

To save and exit, use the Escape + : + w + q keys combination. To exit without saving, use the Escape + : + q + ! keys combination.
If file is not changed, you can also use Escape + : + q keys combination to exit.

Following image shows how to save and exit from vi editor practically.

save and exit in vi

Vi / Vim Editor commands cheat sheet

Command Description
I Insert text before the current cursor position.
I Insert text in the beginning of the current line.
A Append text after the current cursor position.
A Append text at the end of the current line
o Open new line below the current line and switch in input mode
O Open new line above the current line and switch in input mode
/string Search for string in forward direction
?string Search for string in backward direction
n Select next search result
N Select previous search result
:%s/old/new Replace string at first occurrence in forward direction
:%s/old/new/g Replace string globally
yy Copy single line and put it in memory buffer
2yy Copy two lines and put them in memory buffer
yl Copy single character from cursor point and put it in memory buffer
yw Copy single word from cursor point and put it in memory buffer
p Paste contains from memory buffer one time
3p Paste contains from memory buffer three time
x Delete single character after cursor
X Delete single character before cursor
dw Delete single word from cursor (in forward direction)
dW Delete single word from cursor (in backward direction)
dd Delete single line from cursor
4dd Delete four lines from cursor
q Quit from file if it is not changed.
q! Quit from file without save if it is changed
wq Save and quit form file
u Undo last command
U Undo all change in current line
Ctrl+r Redo last command

Always press Escape key before typing any command otherwise whatever you typed will be inserted in file as a regular text.

That’s all for this tutorial. If you like this tutorial, please don’t forget to share it with friends through your favorite social channel.

Full Version EX300 Dumps

Try EX300 Dumps Demo