0% found this document useful (0 votes)
20 views21 pages

Revision Notes 230824 155813

The document provides a comprehensive guide on Linux commands related to file permissions, viewing, and searching. It covers concepts like file ownership, permission modification using commands like chmod and chown, and tools for viewing file content (cat, less, head, tail). Additionally, it explains searching for patterns in files using grep and performing text transformations with sed, along with practical examples for each command.

Uploaded by

versehello19
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views21 pages

Revision Notes 230824 155813

The document provides a comprehensive guide on Linux commands related to file permissions, viewing, and searching. It covers concepts like file ownership, permission modification using commands like chmod and chown, and tools for viewing file content (cat, less, head, tail). Additionally, it explains searching for patterns in files using grep and performing text transformations with sed, along with practical examples for each command.

Uploaded by

versehello19
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

Linux Commands: Permissions, Files & Search

Command Line Usage


1. Explanation of file permissions (read, write, execute) and ownership (user, group).
2. Modifying file permissions and ownership (chmod, chown, chgrp).
3. Viewing file content (cat, less, more, head, tail).
4. Searching for files and directories (find, locate).
5. Basic file editing using a command line editor (nano)

Permissions in Linux :

Introduction :
Imagine a house with rooms (directories) that contain different kinds of boxes (files). Every room
has a label indicating who can:

● Enter and look inside (read).


● Change the contents inside or rearrange the boxes (write).
● Execute or use the items (execute).

In this house, three types of people can interact with rooms and boxes:

● Owner: The person who owns the house (usually the one who created the file or
directory).
● Group: A selected group of friends whom the owner trusts.
● Others: Everyone else who might visit the house.

Types of Permissions:

● Read (r): Like reading a book. You can view the contents but can't alter it.
● Write (w): Like having a pen while reading. You can change the contents.
● Execute (x): For files, it's like a toy – you can 'play' (run) it. For directories, it allows you
to enter and access its content.
Viewing Permissions: The "ls -l" Command :-

If we run the command ls -l in a directory, it might show something like:

drwxr-xr-x 2 user group 4096 Apr 5 12:34 Documents


-rw-r--r-- 1 user group 25 Apr 5 12:33 notes.txt

Breakdown:

● The first character: d indicates a directory. - indicates a file.


● Next three characters: Permissions for the owner.
● Middle three characters: Permissions for the group.
● Last three: Permissions for others.

In our example:

● Documents is a directory. The owner can read, write, and execute (enter it). The group
can read and execute (enter it). Others can also read and execute.
● notes.txt is a file. The owner can read and write. Both the group and others can only
read.

Changing Permissions: The "chmod" Command :

To change the permissions of a file or directory, use chmod.

Symbolic Mode :

● Granting Permissions using Symbolic Modes :

Examples :

1. Give read permission to the owner of the file: chmod u+r filename.txt
2. Give read and write permissions to the owner and group of the file:
chmod u+rw,g+rw filename.txt
3. Give execute permissions to everyone: chmod a+x script.sh
4. Remove read and execute permissions from others: chmod o-rx filename.txt
5. Set the owner to have read and write permissions, but no execute permission, and give
only read permissions to group and others:
chmod u=rw,g=r,o=r filename.txt
● Numeric (Octal) Mode:

Each permission can be represented as a number:


● 4 represents Read (r)
● 2 represents Write (w)
● 1 represents Execute (x)

Examples :

● Give read, write, and execute permissions to the owner, and read and execute
permissions to the group and others :

chmod 755 filename.txt

● Give read and write permissions to the owner, and only read permissions to the group
and others:

chmod 644 filename.txt

● Give full permissions to the owner, write and execute permissions to the group, and no
permissions to others:

chmod 761 filename.txt

● Recursively Change Permissions on Directories:


○ Give read, write, and execute permissions to the owner of all files and directories,
recursively:

chmod -R 700 /path/to/directory

● Remove All Permissions from a File:

chmod 000 filename.txt


CHOWN command :

The chown command in Linux allows us to change the owner and/or group of a file or directory.
When focusing on changing just the group, here's how it works:

● If we want to change the group ownership of a file named example.txt to the group
developers :

chown :developers example.txt

● If we want to change the group ownership of a directory named project_dir to the group
developers :

chown :developers project_dir

● To recursively change the group ownership for a directory and all its contents
(subdirectories and files), you can use the -R option.

For example, to change the group ownership of the directory project_dir and all its
contents to the group developers:

chown -R :developers project_dir

● To change the owner of example.txt to alice and the group to developers:

chown alice:developers example.txt

Introduction to the cat Command :-

The cat (short for "concatenate") command in Linux is one of the most frequently used
commands. It's used to:

● Display the content of files


● Concatenate files and redirect output in terminal or files
● Append content of a file to another file
● Create a new file

Examples :
● To view the contents of a file, simply type cat followed by the filename :

cat filename.txt

● We can also use cat to view the content of multiple files at once :

cat file1.txt file2.txt

● We can create a new file using cat :

cat > newfile.txt

After running this command, you can start typing the contents of the file on the terminal. When
you're done, press Ctrl + D to save the file and exit.

● We can append the contents of one file to another

cat file1.txt >> file2.txt

● If we want to display the contents of a file with line numbers, use the -n option:

cat -n file1.txt

Introduction to less Command :-


The less command in Linux is used to view the content of a file one screen at a time. It's similar
to the more command but provides more advanced features. One of the most significant
benefits of less over more is that less allows both forward and backward navigation through the
file.

less filename.txt

This opens filename.txt in less for viewing. You can scroll through the file using the arrow keys.

Searching Within less


While viewing a file in less, you can search for a pattern:

Forward search: Press / followed by the pattern and press Enter. For example, /pattern
searches for "pattern".

Backward search: Press ? followed by the pattern.

After searching, press n to find the next occurrence or N for the previous occurrence.

Go to a Specific Line
While inside less:
42g

This takes you to the 42nd line.

Jump to End or Beginning of a File


While inside less:

G will take you to the end of the file.


g will take you to the beginning.

Exiting less : Press q to quit less.

HEAD and TAIL Command :

head and tail commands are useful for quickly viewing parts of files. As their names suggest,
head shows the beginning (or head) of files, while tail displays the end (or tail) of files.

Examples of head command :-

1. Display the first 10 lines of a file named sample.txt:

head sample.txt

2. Display the first 5 lines of sample.txt:

head -n 5 sample.txt

The -n option allows you to specify the number of lines you wish to view.
Examples of tail command :-

1. Display the last 10 lines of a file named sample.txt:

tail sample.txt

2. Display the last 5 lines of sample.txt:

tail -n 5 sample.txt

GREP COMMAND :

Introduction : grep stands for "Global Regular Expression Print". It is a powerful tool for
searching specific patterns in files.

Usage: Describe the basic use-case. It's commonly used to find specific strings or patterns in
files.

Simple String Search:

grep "pattern" filename

Use an example with a small file and show how grep highlights the matching pattern.

Case-insensitive search: -i option.

grep -i "pattern" filename

Search across multiple files:

grep "pattern" file1 file2 file3

Using Flags with grep :


● Line numbers with matches:

grep -n "pattern" filename

● Display only count of matching lines:


grep -c "pattern" filename

● Inverse the match : Give the lines which don’t contain the pattern.

grep -v "pattern" filename

● Search Recursively: Use -r or -R to search for a word recursively in directories:


grep -r "word" directory/

● Match whole word :


clear
grep -w "good" example.txt

Using Regular Expressions :


● Search for lines that start with a particular word:

grep "^word" filename.txt

● lines that end with a particular word:

grep "word$" filename.txt

● Wildcard Character:

. [DOT] can be used to represent any single character:

grep "w.rd" filename.txt

This will match "word", "ward", "w3rd", etc.

WC Command :

wc stands for "word count." It allows users to get the number of lines, words, and bytes or
characters from a text source, whether it be a file or an output from another command.

wc filename.txt

The output will be in the format of:

[lines] [words] [bytes] filename.txt


Count Lines:

Use the -l option to count just the lines:

wc -l filename.txt

Count Words:

Use the -w option to count only words:

wc -w filename.txt

Count Characters:

The -c option allows you to count characters (bytes):

wc -c filename.txt

Real-world Usage & Combining with Other Commands:

Piping with echo:

Students can understand how wc works with piped input:

echo "This is a test." | wc -w

This should return 4 because there are four words.

Piping in Linux:

A pipe is a form of redirection (transfer of standard output to some other destination) that is
used in Linux and other Unix-like operating systems to send the output of one
command/program/process to another command/program/process for further processing. The
Unix/Linux systems allow the stdout of a command to be connected to the stdin of another
command. You can make it do so by using the pipe character ‘|’.
Piping with cat:

Count words across multiple files:

cat file1.txt file2.txt | wc -w

Piping with grep:

-o option with grep : Print only the matched parts of a matching line, with each such part on a
separate output line.

By default, grep displays the entire line which has the matched string. We can make the grep to
display only the matched string by using the -o option.

grep -o “good” example.txt

Count the total number of occurrences of a word in a file:

grep -o "word" input | wc -l

Q: Print line numbers from 5 to 10 from a file.

To print lines from x to y (e.g., from 5 to 10):

head -10 filename | tail -6

Explanation : head -10 filename prints the first 10 lines. From those, tail -6 then prints the last 6
lines, which correspond to lines 5 through 10.

Search apple or banana in a file :

grep -E "apple|banana" filename

Lines that start with "apple"

grep -E "^apple" filename

Lines that end with "apple"


grep -E "apple$" filename

SED and AWK Commands

sed (Stream Editor):

sed is a stream editor that is used to perform basic text transformations on an input stream (a
file or input from a pipeline).

Basic Usage:
● Replace first occurrence of a string in a line: sed 's/find/replace/' filename

echo "apple apple apple" | sed 's/apple/orange/'

-> This will output "orange apple apple". It replaced only the first
occurrence. To replace all occurrences on a line, use the g option.

● Replace all occurrences of a string in a line: sed 's/find/replace/g' filename

echo "apple apple apple" | sed 's/apple/orange/g'

-> This will output "orange orange orange".

● Replace on a specific line number:

sed '3 s/find/replace/' filename

Intermediate Usage:
● Delete lines from a file: sed '3,5d' filename deletes lines 3 to 5.
● Print only specific lines: sed -n '3,5p' filename prints lines 3 to 5.

Advanced Usage:
● Perform case insensitive search:

sed 's/find/replace/gI' filename

● Replace in place in a file (altering original file):

sed -i 's/find/replace/g' filename


● Replace in a specific line:
sed '2s/orange/grape/'

Now let's discuss how sed works:

Read: The sed command reads a line of input from its source one line at a time into an area of
memory known as the pattern space. The input is typically a file, the output of another
command, or input from a terminal.

Execute: Once the line is read, sed executes a set of instructions that you have passed to it.
These instructions are applied on the line read into the pattern space. The commands can
change the line, delete it, print it out, or store it in a hold buffer for further action.

Display: After it has applied all the commands on the pattern space, sed outputs the line to
standard output (usually the terminal, unless redirected). The line is then deleted from the
pattern space, and the process is repeated from step 1 with the next line.

Here's an example to illustrate this:

Suppose we have a file named file.txt with the following contents:

Hello World
Goodbye World
Hello again

Let's say we want to replace all occurrences of 'Hello' with 'Hi'. Here is how we can do it
using sed:

sed 's/Hello/Hi/g' file.txt

In this example, 's/Hello/Hi/g' is the command we are passing to sed. Here's what it does:

's' stands for substitute.


'Hello' is the search pattern – the text we want to replace.
'Hi' is the replacement text – what we want to replace 'Hello' with.
'g' stands for global, which means replace all occurrences on a line, not just the first one.

And the output will be:


Hi World
Goodbye World
Hi again

Remember, unless we specify -i as an option (like sed -i 's/Hello/Hi/g' file.txt), sed doesn’t
change the original file. It sends the output to the standard output (the terminal, usually). If you
want to save changes to a file,
you should redirect the output to a file.

If you want to edit the file in-place (i.e., change the original file), you can use the -i option with
sed:

sed -i 's/Hello/Hi/g' file.txt

This changes the input file directly. Be cautious when using -i, because it overwrites the original
file. It's a good idea to keep a backup of your original file when using this option.

Here’s how we can save the output of sed command to a file :

sed 's/Hello/Hi/g' file.txt > outputfile

If outputfile doesn't exist, the redirection operator > creates it. If it does exist, > overwrites it.
To append the output to the file without overwriting existing content, use the >> operator.

Deleting Lines using sed command:

We can delete specific lines in a file with sed.

For example, to delete the first line:

sed '1d' filename

To delete the last line:

sed '$d' filename

To delete a range of lines, like lines 3 through 5:

sed '3,5d' filename


Inserting and Appending Text:

You can use sed to insert or append text on a line. To insert "Hello" at the beginning of every
line:

sed 's/^/Hello /' filename

And to append "Goodbye" at the end of every line:

sed 's/$/Goodbye/' filename

Replacing Strings on Specific Lines Only:

If you want to replace a string only on specific lines, you can do that with sed. To replace "find"
with "replace" only on lines 2 through 4:

sed '2,4s/find/replace/' filename

Print Only Certain Lines:

By default, sed prints out all lines. With the -n option, you can make it quiet and only print lines
you specify. To print only line number 3:

sed -n '3p' filename

To print lines 3 through 5:

sed -n '3,5p' filename

Replacing Strings Except on Certain Lines:

sed can also replace strings everywhere except on certain lines. For example, to replace "Hello"
with "Hi" everywhere except lines 3 through 5:

sed '3,5!s/Hello/Hi/' filename


Replacing a Whole Line:

To replace the whole line when a match is found, use the c command in sed. The following
command will replace lines containing "match" with "Replacement text":

sed '/match/c\Replacement text' filename

Append Text at a Specific Line :

Use Case: Append "EOF" at the end of a file.

sed '$a\EOF' file.txt

The expression $a is used to append text after the last line of the input.

Here's a breakdown of the components:

$: This symbol represents the last line of the input stream or file in sed. It's a special address
that matches only the last line.

a: a in sed stands for "append". It's used to append text after a specific line.

Multiple Operations in a Single Command:

Use Case: Delete lines containing "error", and replace "old" with "new".

sed '/error/d; s/old/new/g' file.txt

AWK Command :

The awk command is a text-processing language that is particularly adept at handling


structured data, such as columns of numbers or text. Its operation can be understood in terms
of its basic workflow, which revolves around reading input line by line, splitting the input into
records, and processing each record based on specified patterns and actions.

Here's a step-by-step breakdown of how awk works:


Reading Input:

By default, awk reads its input line by line, treating each line as a record. This behavior can be
changed by adjusting the record separator.
The input can be provided in various ways: directly from the command line, from a file, or via a
pipe from another command.

Splitting into Fields:


● Each record (by default, a line) is automatically split into fields.
● The default field separator is white space (like spaces or tabs). This means awk splits a
line wherever it sees spaces or tabs.
● The field separator can be changed using the -F option or by setting the FS (Field
Separator) variable.
● Individual fields can be accessed using $1, $2, $3, etc., where $1 is the first field, $2 is
the second, and so on. $0 represents the entire record.

Pattern Matching:

awk scans each record for patterns you've specified. A pattern dictates which records should be
acted upon.
If no pattern is given, awk performs the specified action on every record.
Patterns can be regular expressions, relational expressions, or combinations thereof.

Executing Actions:

● For records that match a given pattern, awk performs the associated action, which is
typically enclosed in curly braces {...}.
● Actions can be any combination of commands, calculations, and functions. If no action is
specified for a pattern, the default action is to print the entire record ($0).
● Actions are executed in the order they appear in the command or script.

Built-in Variables:

● awk has several built-in variables like NR (Number of Records), NF (Number of Fields
in the current record), OFS (Output Field Separator), and others. These variables can
be used to gather insights about the input or to modify the behavior of awk.

End and Begin Blocks:

● awk scripts can have BEGIN and END blocks.


● The BEGIN block is executed before any input is read, which is useful for initializing
variables or printing headers.
● The END block is executed after all input has been read, which is handy for summarizing
data or performing final calculations.

Processing the Next Record:

After processing an input line, awk automatically reads the next line and starts the process
again, continuing until all input has been read.

Use Cases :

● Printing Entire Content of a File

Command:

awk '{print}' file.txt

Working:

The command is simply telling awk to print every line in file.txt. Since no pattern is specified
before the action {print}, awk assumes it should operate on all lines. {print} is an action telling
awk to print the line as it is. Since print without any field number defaults to printing $0 (the
whole line), the entire content of the file is printed.

● Printing Specific Fields

Command:

awk '{print $1, $3}' file.txt

Working:

This command prints the first and third fields of each line in file.txt. awk reads the file line by line,
splits each line into fields based on whitespace (or any other specified delimiter), then {print $1,
$3} tells awk to print the first and third fields of each line.

● Print line numbers from 3 to 6

Command:

awk 'NR==3, NR==6 {print NR,$0}' file.txt


● Conditional Printing

Command:

awk '$1 == "example" {print $2}' file.txt

Working:

Here, $1 == "example" is a condition that awk checks for each line in file.txt. If the first field of a
line is "example", awk performs the {print $2} action, which prints the second field of the line.

● Line Count Using awk

Command:

awk 'END {print NR}' file.txt

Working:

This command prints the total number of lines in file.txt. It does so by using the built-in awk
variable NR, which keeps a count of the number of records (lines, by default) processed. As
awk reads each line, it increments NR. At the END, it simply prints the value of NR, which is the
total line count.

● Print the lines with more than 10 characters:

awk 'length($0) > 10' file.txt

Note : It counts white spaces as well.

● Print the content with line numbers

Use Case: Print the content of the line along with line numbers

awk '{print NR, $0}' file.txt

● Print Specific Fields

Use Case: Suppose you have a file named employees.txt containing the name, age, and
designation of employees:
Alice 30 Developer
Bob 25 Designer
Charlie 35 Manager

We want to print only the names of the employees.

Command:

awk '{ print $1 }' employees.txt

Working:

● awk reads the file employees.txt line by line.


● For each line, it splits the data into fields using the default separator (whitespace).
● The action { print $1 } instructs awk to print the first field, i.e., the name of the employee.

Output:

Alice
Bob
Charlie

● Sum a Column of Numbers

Use Case: Given a file sales.txt that lists items and their respective sales:

Laptop 1500
Phone 800
Tablet 300

We want to find the total sales.

Command:

awk 'BEGIN { sum=0 } { sum += $2 } END { print sum }' sales.txt

Working:
● The BEGIN block initializes the variable sum to 0 before any line is processed.
● For each line, the value of the second field (the sales figure) is added to the sum.
● After processing all lines, the END block prints the final value of sum.

Output:

2600

● Filter Data Based on a Condition

Use Case: From the employees.txt file, you want to list only those employees older than 28.

Command:

awk '$2 > 28' employees.txt

Working:

● Each line is read and split into fields.


● The pattern $2 > 28 checks if the second field (age) is greater than 28.
● If the condition is true, the default action of printing the entire line ($0) is executed. If no
action is specified after a pattern, the default action is to print the line.
Output:

Alice 30 Developer
Charlie 35 Manager

● Modify Field Data

Use Case: For the sales.txt file, you want to increase the sales of each item by 10%.

Command:

awk '{ $2 = $2 * 1.10; print $0 }' sales.txt

Working:

● Each line's second field is updated to be 10% greater.


● The entire updated line is then printed using print $0.
Output:

Laptop 1650
Phone 880
Tablet 330

How to give different delimiter than space :

awk -F':' ...............

● Calculate Average

Use Case: Calculate the average of numbers in the second column.

awk '{ total+=$2; count++ } END { print "Average:", total/count }' data.txt

● Data Extraction and Transformation

Use Case: Convert a space-separated values file to CSV format.

awk '{OFS=","; print $1, $2, $3}' data.ssv > data.csv

● Write a linux command to display lines that have the same word at the beginning and the
end, in a file.

awk '{ if ($1 == $NF) print }' input

You might also like