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

Linux Cmds

The document provides an overview of various Unix/Linux commands such as grep, find, awk, comm, and their functionalities for text searching, file manipulation, and data processing. It also covers concepts of input/output redirection, pipe operators, and file permissions, explaining how to manage access and modify permissions using commands like chmod. Additionally, it highlights the importance of these commands and permissions in ensuring efficient data handling and security in Linux environments.

Uploaded by

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

Linux Cmds

The document provides an overview of various Unix/Linux commands such as grep, find, awk, comm, and their functionalities for text searching, file manipulation, and data processing. It also covers concepts of input/output redirection, pipe operators, and file permissions, explaining how to manage access and modify permissions using commands like chmod. Additionally, it highlights the importance of these commands and permissions in ensuring efficient data handling and security in Linux environments.

Uploaded by

naaperu425
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

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.
grep [options] pattern [files]
 [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


Options Description

Print only the matched parts of a


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

Prints searched line and nlines


-A n after the result.

Prints searched line and n line


-B n before the result.

-C n Prints searched line and n lines aft

The find command in Linux is a dynamic utility designed for comprehensive file and directory
searches within a hierarchical structure. Its adaptability allows users to search by name, size,
modification time, or content, providing a flexible and potent solution.
find [path] [options] [expression]
 Path: Where to start searching (e.g., ~/Documents).
 Options: Refine your search (e.g., -type for files/directories).
 Expression: Criteria like filenames or sizes.

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 options 'selection _criteria {action }' input-file > output-file
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

The ‘comm’ command is used for line-by-line comparison of two sorted files. It reads two
files as input and generates a three-column output by default:
 Column 1: Lines unique to the first file.
 Column 2: Lines unique to the second file.
 Column 3: Lines common to both files.
$comm [OPTION]... FILE1 FILE2

 ‘FILE1’ and ‘FILE2′: The sorted files to compare.


 ‘[OPTION]’: Flags to modify the command’s output.
Key Options for the ‘comm’ command
The ‘comm’ command offers several options to customize its
output. Here are the most useful ones:
1. ‘-1’ (Suppress First Column)
Suppresses lines that are unique to the first file, displaying only
lines from the second file and common lines.
2. ‘-2’ (Suppress Second Column)
Suppresses lines that are unique to the second file, displaying only
lines from the first file and common lines.
3. ‘-3’ (Suppress Third Column)
Suppresses lines that are common to both files.
4. ‘–check-order’
Check that the input is correctly sorted, even if all input lines are
pairable.

Input/Output Redirection
Unix provides the capability to change where standard input comes from, or where output
goes using a concept called Input/Output (I/O) redirection. I/O redirection is accomplished
using a redirection operator which allows the user to specify the input or output data be
redirected to (or from) a file. Note that redirection always results in the data stream going to
or coming from a file (the terminal is also considered a file).
The simplest case to demonstrate this is basic output redirection. The output redirection
operator is the > (greater than) symbol, and the general syntax looks as follows:
command > output_file_spec
Spaces around the redirection operator are not mandatory, but do add readability to the
command. Thus in our ls example from above, we can observe the following use of output
redirection:
$ ls > my_files [Enter]
$
Notice there is no output appearing after the command, only the return of the prompt. Why
is this, you ask? This is because all output from this command was redirected to the file
my_files. Observe in the following diagram, no data goes to the terminal screen, but to the
file instead.

Examining the file as follows results in the contents of the my_files being displayed:
$ cat my_files [Enter]
foo
bar
fred
barney
dino
$
In this example, if the file my_files does not exist, the redirection operator causes its
creation, and if it does exist, the contents are overwritten. Consider the example below:
$ echo "Hello World!" > my_files [Enter]
$ cat my_files [Enter]
Hello World!
Notice here that the previous contents of the my_files file are gone, and are replaced with
the string "Hello World!" Note also that when using redirection, the output file is created
first, then the command left of the redirection operator is executed. Observe the following:
$ cat my_files [Enter]
Hello World!
$ cat my_files > my_files [Enter]
$ cat my_files [Enter]
$
Often we wish to add data to an existing file, so the shell provides us with the capability to
append output to files. The append operator is the >>. Thus we can do the following:
$ ls > my_files [Enter]
$ echo "Hello World!" >> my_files [Enter]
$ cat my_files [Enter]
foo
bar
fred
barney
dino
Hello World!
The first output redirection creates the file if it does not exist, or overwrites its contents if it
does, and the second redirection appends the string "Hello World!" to the end of the file.
When using the append redirection operator, if the file does not exist, >> will cause its
creation and append the output (to the empty file).
The ability also exists to redirect the standard input using the input redirection operator, the
< (less than) symbol. Note the point of the operator implies the direction. The general syntax
of input redirection looks as follows:
command < input_file_spec
Looking in more detail at this, we will use the wc (word count) command. The wc command
counts the number of lines, words and bytes in a file. Thus if we do the following using the
file created above, we see:
$ wc my_files [Enter]
6 7 39 my_files
where the output indicates 6 lines, 7 words and 39 bytes, followed by the name of the file
wc opened.
We can also use wc in conjunction with input redirection as follows:
$ wc < my_files [Enter]
6 7 39
Note here that the numeric values are as in the example above, but with input redirection,
the file name is not listed. This is because the wc command does not know the name of the
file, only that it received a stream of bytes to count.
Someone will certainly ask if input redirection and output redirection can be combined, and
the answer is most definitely yes. They can be combined as follows:
$ wc < my_files > wc_output [Enter]
$
There is no output sent to the terminal screen since all output was sent to the file
wc_output. If we then looked at the contents of wc_output, it would contain the same data
as above.
To this point, we have discussed the standard input stream (descriptor 0) and the standard
output stream (descriptor 1). There is another output stream called standard error (stderr)
which has file descriptor 2. Typically when programs return errors, they return these using
the standard error channel. Both stdout and stderr direct output to the terminal by default,
so distinguishing between the two may be difficult. However each of these output channels
can be redirected independently. Refer to the diagram below:

The standard error redirection operator is similar to the stdout redirection operator and is
the 2> (two followed by the greater than, with no spaces) symbol, and the general syntax
looks as follows:
command 2> output_file_spec
Thus to show an example, we observe the following:
$ ls foo bar 2> error_file [Enter]
foo

$ cat error_file [Enter]


ls: bar: No such file or directory

Note here that only the standard output appears once the standard error stream is
redirected into the file named error_file, and when we display the contents of error_file, it
contains what was previously displayed on the termimal. To show another example:
$ ls foo bar > foo_file 2> error_file [Enter]
$

$ cat foo_file [Enter]


foo

$ cat error_file [Enter]


ls: bar: No such file or directory
In this case both stdout and stderr were redirected to file, thus no output was sent to the
terminal. The contents of each output file was what was previously displayed on the screen.
Note there are numerous ways to combine input, output and error redirection..
One final miscellaneous item is the technique of combining the two output streams into a
single file. This is typically done with the 2>&1 command, as follows:
$ command > /dev/null 2>&1 [Enter]
$
Here the leftmost redirection operator (>) sends stdout to /dev/null and the 2>&1 indicates
that channel 2 should be redirected to the same location as channel 1, thus no output is
returned to the terminal.
Redirection Summary

Redirection Operator Resulting Operation

command > file stdout written to file, overwriting if file exists

command >> file stdout written to file, appending if file exists

command < file input read from file

command 2> file stderr written to file, overwriting if file exsits

command 2>> file stderr written to file, appending if file exists

command > file 2>&1 stdout written to file, stderr written to same file descriptor

Pipe Operator
A concept closely related to I/O redirection is the concept of piping and the pipe operator.
The pipe operator is the | character (typically located above the enter key). This operator
serves to join the standard output stream from one process to the standard input stream of
another process in the following manner:
We can look at an example of pipes using the who and the wc commands. Recall that the
who command will list each user logged into a machine, one per line as follows:
$ who [Enter]
mthomas pts/2 Oct 1 13:07
fflintstone pts/12 Oct 1 12:07
wflintstone pts/4 Oct 1 13:37
brubble pts/6 Oct 1 13:03
Also recall that the wc command counts characters, words and lines. Thus if we connect the
standard output from the who command to the standard input of the wc (using the -l (ell)
option), we can count the number of users on the system:
$ who | wc -l [Enter]
4
In the first part of this example, each of the four lines from the who command will be
"piped" into the wc command, where the -l (ell) option will enable the wc command to
count the number of lines.

Command Summary
 cut - remove sections from each line in a file
 grep - (search for and) print lines matching a pattern
 sed - command to modify a text stream
 uniq - remove duplicate lines from a sorted file
 wc - print the number of bytes, words, and lines in files
 pipe operator - character ( | ) used to link the output of one command to the input of
another
 redirection - changing the flow of an input, output or error stream

What are File Permissions in Linux


In Linux, file permissions are rules that determine who can access, modify, or execute files
and directories. They are foundational to Linux security, ensuring that only authorized users
or processes can interact with your data. Here’s a breakdown:
1. The Three Basic Permissions
Every file or directory has three types of permissions:
 Read (r): View the file’s contents or list a directory’s files.
 Write (w): Modify a file or add/delete files in a directory.
 Execute (x): Run a file as a program/script or enter a directory.

Letters Definition

‘r’ “read” the file’s contents.

‘w’ “write”, or modify, the file’s contents.

“execute” the file. This permission is given


‘x’ only if the file is a program.

2. Ownership and Permission Groups


Permissions are assigned to three categories of users:
 User (Owner): The person who created the file.
 Group: Users belonging to a shared group (e.g., “developers” or “admins”).
 Others: Everyone else on the system.
File Permission: Operation Chart

Operators Definition

`+` Add permissions

`-` Remove permissions

`=` Set the permissions to the specified values

Note: All these permissions are being granted at three different levels based on their group.
What are Permission Groups in Linux
First, you must think of those nine characters as three sets of three characters (see the box
at the bottom). Each of the three “rwx” characters refers to a different operation you can
perform on the file.
1. Owners: These permissions apply exclusively to the individuals who own the files or
directories.
2. Groups: Permissions can be assigned to a specific group of users, impacting only
those within that particular group.
3. All Users: These permissions apply universally to all users on the system, presenting
the highest security risk. Assigning permissions to all users should be done cautiously
to prevent potential security vulnerabilities.
--- --- ---
rwx rwx rwx
user group other
User, Group, and others Option in Linux File Permission

Reference Class Description

The user permissions apply


only to the owner of the file
or directory, they will not
impact the actions of other
`u` user users.

The group permissions


apply only to the group that
has been assigned to the
file or directory, they will
not affect the actions of
`g` group other users.

The other permissions apply


to all other users on the
system, this is the
permission group that you
`o` others want to watch the most.

All three (owner, groups,


`a` All three others)
How to Change Permissions in Linux
The command you use to change the security permissions on files is called “chmod“, which
stands for “change mode” because the nine security characters are collectively called the
security “mode” of the file. You can modify permissions using symbolic notation or octal
notation.
1. Symbolic Notation
Symbolic notation allows you to add, remove, or set permissions for specific users. Let’s
understand this using different example below:
Example 1: To Change File Permission in Linux
If you want to give “execute” permission to the world (“other”) for file “xyz.txt”, you will
start by typing.
chmod o
Now you would type a ‘+’ to say that you are “adding” permission.
chmod o+
Then you would type an ‘x’ to say that you are adding “execute” permission.
chmod o+x
Finally, specify which file you are changing.
chmod o+x xyz.txt
Octal Notations Permissions in Linux
The octal notation is used to represent file permission in Linux by using three user group by
denoting 3 digits i.e.
 user
 group
 other users
Here’s how to permissions are mapped:
 Read (r) = 4
 Write (w) = 2
 Execute (x) = 1
Permissions for owner, group, and others are represented by a three-digit octal value. The
sum of permissions for each group gives the corresponding number.
How to Set File Permissions for a Specific User
To set permissions for a specific user or group:
1. By using chown
Use chown to change file ownsership:
chown user:group file.txt
2. By using chmod
Use chmod to modify permissions:
chmod 755 file.txt

The wc command in Linux is used to count the number of lines, words, and characters in a
file or input stream. It's a versatile tool for quickly getting statistics about text data.
Here's a breakdown of how it works and its common options:
Basic Usage:
 wc filename: Counts lines, words, and characters in the specified file.
 wc < input_file: Counts lines, words, and characters from standard input.
Common Options:
 -l: Counts only the number of lines.
 -w: Counts only the number of words.
 -c: Counts only the number of characters.
 -m: Counts only the number of characters (like -c but might be affected by encoding).
 -C: Counts the number of bytes.

You might also like