Linux Cmds
Linux Cmds
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
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
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
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]
$
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
Letters Definition
Operators Definition
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
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.