0% found this document useful (0 votes)
9 views

Unit-3 Group (Unix)

Unit-3 Group (Unix)

Uploaded by

ravi pandey
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Unit-3 Group (Unix)

Unit-3 Group (Unix)

Uploaded by

ravi pandey
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Unit:-3 Group

Group Operation:- In Linux, there can be multiple users (those who


use/operate the system), and groups are nothing but a collection of
users. Groups make it easy to manage users with the same security
and access privileges

Important Points:
 Groups command prints the names of the primary and any
supplementary groups for each given username, or the current
process if no names are given.
 If more than one name is given, the name of each user is printed
before the list of that user’s groups and the username is separated
from the group list by a colon.

Syntax of the `groups` command in Linux


groups [options] [username]
https://www.geeksforgeeks.org/groups-command-in-linux-with-
examples/

Searching for File Content:-

Grep command:- The grep command in Unix/Linux is a powerful tool


used for searching and manipulating text patterns within files. Its name
is derived from the ed (editor) command g/re/p (globally search for a
regular expression and print matching lines), which reflects its core
functionality. grep is widely used by programmers, system
administrators, and users alike for its efficiency and versatility in
handling text data.

https://www.geeksforgeeks.org/grep-command-in-unixlinux/

Syntax of grep Command in Unix/Linux


The basic syntax of the `grep` command is as follows:
grep [options] pattern [files]

Here,
[options]: These are command-line flags that modify the behavior
of grep.
[pattern]: This is the regular expression you want to search for.
[file]: This is the name of the file(s) you want to search within. You can
specify multiple files for simultaneous searching.

Options Available in grep Command


Options Description

This prints only a count of the


-c lines that match a pattern

Display the matched lines, but do


-h not display the filenames.

–i Ignores, case for matching

-l Displays list of a filenames only.

Display the matched lines and


-n their line numbers.

This prints out all the lines that do


-v not matches the pattern

Specifies expression with this


-e exp option. Can use multiple times.

Takes patterns from file, one per


-f file line.

Treats pattern as an extended


-E regular expression (ERE)

-w Match whole word

Print only the matched parts of a


matching line, with each such part
-o on a separate output line.

-A n Prints searched line and nlines


Options Description

after the result.

Prints searched line and n line


-B n before the result.

Prints searched line and n lines


-C n after before the result.
Sample Commands
Consider the below file as an input.
cat > geekfile.txt

unix is great os. unix was developed in Bell labs.


learn operating system.
Unix linux which one you choose.
uNix is easy to learn.unix is a multiuser os.Learn unix .unix is a
powerful.

Pratical Example of grep Command in Linux


1. Case insensitive search
The -i option enables to search for a string case insensitively in the
given file. It matches the words like “UNIX”, “Unix”, “unix”.
grep -i "UNix" geekfile.txt

Output:

Sed command:- https://www.geeksforgeeks.org/sed-command-in-


linux-unix-with-examples/
Scripts:- https://www.geeksforgeeks.org/introduction-linux-shell-shell-
scripting/

Shell is an interface of the operating system. It accepts commands from


users and interprets them to the operating system. If you want to run a
bunch of commands together, you can do so by creating a shell script.
Shell scripts are very useful if you need to do a task routinely, like taking a
backup. You can list those commands and execute them all with just a
single script. Let’s see how you can create a shell script and run it on
Linux.

Creating a Shell Script


Login to your Linux machine and open the terminal, navigate to the folder
where you want to store the shell script. Shell scripts end with the
extension “.sh”. Let’s create our first shell script. Type in
touch script.sh
Now, this script file is not executable by default, we have to give the
executable permission to this file. Type in
chmod +x script.sh
Now, we will add some commands to this shell script. Open this shell
script with any text editor of your choice (command-line based or GUI
based) and add some commands.

Variables in Shell Script


Yes, Shell scripts support the use of variables, and we need not define a
variable’s type during its declaration. There are two types of variables:
 System Defined variables
 User-Defined Variables.
 System-defined variables, also called environment variables, are
generally Capitalised. You can view all the current environment
variables using the printenv command. User-Defined variables are
set by the user, and they exist only during script execution. You can
define a variable by simply typing its name and assigning a value
with = sign and access a variable by adding a $ before the variable
name. Variables are demonstrated in the following example script.
 # Accessing an Environment Variable
 echo $USER

 # Creating and accessing User defined Variable
 variable_name="Geeksforgeeks"
 echo $variable_name

Comparison Operators
You can compare two variables in shell scripting. We do these
comparisons to make decisions, we will see how to do that later in this
article, but before that, here is a list of some comparison operators.

Integer comparison

Operator Description

-eq is equal to

-ne is not equal to

-gt is greater than

-ge is greater than or equal to

-lt is less than

-le is less than or equal to


String Comparison

Operator Description

== is equal to

!= is not equal to

\< is less than, in ASCII alphabetical order

\> is greater than, in ASCII alphabetical order

We add a \ before < and > because they need to be escaped when typed
in the [ ] construct. Now, let’s see where these are used.

Conditional statements
Conditional statements are used to execute a block of code only when
certain conditions are met. Shell scripts support the use of conditional
statements. We use comparison operators to check the conditions. Let’s
see a few conditional statements.

If statement

It checks the condition, and if it is conditioned true, it executes the


commands.
Syntax
if [ condition ]
then
#statements
fi
Let’s see an example.
#!/bin/sh
x=10
y=11
if [ $x -ne $y ]
then
echo "Not equal"
fi
https://www.geeksforgeeks.org/how-to-create-a-shell-script-in-
linux/

Group Command:-

Groups command prints the names of the primary and any


supplementary groups for each given username, or the current
process if no names are given. If more than one name is given, the
name of each user is printed before the list of that user's groups
and the username is separated from the group list by a colon.

Syntax of the `groups` command in Linux


groups [options] [username]
https://www.geeksforgeeks.org/groups-command-in-linux-with-
examples/

Users can be listed in different groups. Group allow us to set permission on the
group level instead of setting the permission on individual level.

Every Linux distribution have a graphical tool to manage groups. Groups can be
managed by graphical tools, command line tools and by vi or vigr depending upon
the user's experience

groupadd

The groupadd command creates or add a group in our system.

Syntax:

1. groupadd <groupName>

Example:

1. groupadd php
2. groupadd java
3. groupadd android
4. groupadd spring

Group File
The /etc/group file defines the group membership. A user can be a member of more
than one group.

Syntax:

1. /etc/group

Groups
The group command tells about the group where current user belongs to.

Syntax:

1. groups

Awk commands:-
Awk is a scripting language used for manipulating data and generating
reports. The awk command programming language requires no compiling
and allows the user to use variables, numeric functions, string functions, and
logical operators.
Awk is a utility that enables a programmer to write tiny but effective programs
in the form of statements that define text patterns that are to be searched for
in each line of a document and the action that is to be taken when a match is
found within a line. Awk is mostly used for pattern scanning and processing.
It searches one or more files to see if they contain lines that matches with
the specified patterns and then perform the associated actions.
AWK Operations:
(a) Scans a file line by line
(b) Splits each input line into fields
(c) Compares input line/fields to pattern
(d) Performs action(s) on matched lines
2. Useful For:
(a) Transform data files
(b) Produce formatted reports
3. Programming Constructs:
(a) Format output lines
(b) Arithmetic and string operations
(c) Conditionals and loops
Syntax:
awk options 'selection _criteria {action }' input-file > output-
file

Sample Commands
Example:
Consider the following text file as the input file for all cases below:
$cat > employee.txt
ajay manager account 45000
sunil clerk account 25000
varun manager sales 50000
amit manager account 47000
tarun peon sales 15000
deepak clerk sales 23000
sunil peon sales 13000
satvik director purchase 80000
1. Default behavior of Awk: By default Awk prints every line of data from
the specified file.
$ awk '{print}' employee.txt
Output:
ajay manager account 45000
sunil clerk account 25000
varun manager sales 50000
amit manager account 47000
tarun peon sales 15000
deepak clerk sales 23000
sunil peon sales 13000
satvik director purchase 80000
In the above example, no pattern is given. So the actions are applicable to all
the lines. Action print without any argument prints the whole line by default,
so it prints all the lines of the file without failure.

Records and fields


Awk views its input data as a series of records, which are usually newline-
delimited lines. In other words, awk generally sees each line in a text file as
a new record. Each record contains a series of fields. A field is a
component of a record delimited by a field separator.

By default, awk sees whitespace, such as spaces, tabs, and newlines, as


indicators of a new field. Specifically, awk treats multiple space separators
as one, so this line contains two fields

You might also like