[go: up one dir, main page]

0% found this document useful (0 votes)
61 views23 pages

Linux and Shell Scripting: Shubhankar Thakur (Ethashu)

This document provides summaries of Linux commands for viewing and managing processes, disk usage, terminating processes, counting file elements, removing duplicate lines, cutting text, translating characters, searching files, adding and deleting text in files, remotely accessing systems, and creating hard and soft file links. It explains commands like ps, top, du, df, kill, wc, uniq, sort, cut, tr, grep, awk, sed, ssh, scp and types of file links and how they differ.

Uploaded by

Aashish
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)
61 views23 pages

Linux and Shell Scripting: Shubhankar Thakur (Ethashu)

This document provides summaries of Linux commands for viewing and managing processes, disk usage, terminating processes, counting file elements, removing duplicate lines, cutting text, translating characters, searching files, adding and deleting text in files, remotely accessing systems, and creating hard and soft file links. It explains commands like ps, top, du, df, kill, wc, uniq, sort, cut, tr, grep, awk, sed, ssh, scp and types of file links and how they differ.

Uploaded by

Aashish
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/ 23

Linux and Shell

Scripting

Shubhankar Thakur
(ETHASHU)
Day 2
Command : ps
To view the processes that you’re running:
Command : top
To view the CPU usage of all processes:
Command: du, df
The df and du commands are two small utilities that are extremely useful. The df
command displays the disk free space and the du command displays the disk
usage. There basic usage is df [options] and du [options].

Using du

Using df
Command: kill
To terminate a process use “kill”
Command: wc
To count the characters, words, and lines in a file
use “wc”
The first column in the output is lines, the second
is words, and the last is characters
Command: uniq
The uniq command in LNIX is a command line utility for reporting or
filtering repeated lines in a file. It can remove duplicates, show a
count of occurrences, show only repeated lines, ignore certain
characters and compare on specific fields.
Command: uniq, sort
The command expects adjacent comparison lines so it is often combined with the
sort command.

To show a count of the number of times a line occurred

To only show repeated lines pass the -d option to uniq.


To only show lines that are not repeated pass the -u option to uniq.
Command: cut
The cut command in UNIX is a command line utility for cutting sections from each
line of files and writing the result to standard output.
To cut out a section of a line by specifying a byte position use the -b
option.

To cut by character use the -c option. This selects the characters given to
the -c option.

Where your input stream is character based -c can be a better option than selecting by bytes
as often characters are more than one byte.

In the above example character ‘♣’ is three bytes.


Command: cut
To cut using a delimiter use the -d option. This is normally used in
conjunction with the -f option to specify the field that should be cut.
In the following example a CSV file exists and is saved as names.csv.

The delimiter can be set to a comma with -d ','. cut can then pull out the fields of
interest with the -f flag. In the following example the first field is cut.
Command: tr
The tr command in UNIX is a command line utility for translating or deleting characters. It
supports a range of transformations including uppercase to lowercase, squeezing repeating
characters, deleting specific characters and basic find and replace.

⚫ To convert lower case to upper case

⚫ To search for a complement of a pattern

⚫ To delete specific characters

⚫ To find and replace


Input/Output Redirection
(“piping”)
• Programs can output to other programs
• Called “piping”
• “program_a | program_b”
• program_a’s output becomes program_b’s input

• “program_a > file.txt”


• program_a’s output is written to a file called “file.txt”

• “program_a < input.txt”


• program_a gets its input from a file called “input.txt”
A few examples of piping
Command: grep
To search files in a directory for a specific string use “grep”
Command: awk
AWK is an interpreted programming language. It is very powerful and
specially designed for text processing. Its name is derived from the family
names of its authors − Alfred Aho, Peter Weinberger, and Brian
Kernighan.
Command: sed
With sed you can do all of the following:
• Select text
• Substitute text
• Add lines to text
• Delete lines from text
Command: sed
Select text
To select some lines from the file, we provide the start and end lines of the range we want to select. A single
number selects that one line.
To extract lines one to four, we type this command:
sed -n '1,4p' coleridge.txt
Note the comma between 1 and 4. The p means “print matched lines.”

Substitute text
In our first example, we showed you the following basic format for a sed substitution:
echo howtogonk | sed 's/gonk/geek/gi'
The s tells sed this is a substitution
The g use to perform a global search so all matches in each line are processed
Adding an i to the command at the end of the expression to indicate case-insensitivity
sed -n 's/day/week/gi' coleridge.txt
Command: sed
Add lines to text
We can also insert new lines and text into our file. To insert new lines after any matching ones, we’ll use the
Append command (a).
We type the following to search for lines that contain the word “He,” and insert a new line beneath them:
sed '/He/a --> Inserted!' test.txt

Delete lines from text


The Delete command (d) deletes lines that match a search pattern, or those specified with line numbers or ranges.

For example, to delete the third line, we would type the following:
sed '3d' geeks.txt
To delete lines outside a range, we use an exclamation point (!), as shown below:
sed '6,7!d' geeks.txt
Command: ssh, scp
ssh is used to securely log in to remote systems, successor to telnet
ssh [username]@[hostname]
Try:
ssh yourusername@localhost
Type “exit” to log out of session

Scp is used to copy files to/from remote systems, syntax is similar to cp:
scp [local path] [usernme]@[hostname]:[remote file path]

Try:
scp hello.txt yourusername@localhost:scp-test.txt
Links
In your Linux file system, a link is a connection between a file name and the
actual data on the disk.
There are two main types of links that can be created: "hard" links, and "soft"
or symbolic links.

An inode is a data structure that stores various information about a file in Linux,
such as the access mode (read, write, execute permissions), ownership, file type, file
size, group, number of links, etc. Each inode is identified by an integer number. An
inode is assigned to a file when it is created.
Links
Soft Link
• ln -s source.file softlink.file
• can cross the file system,
• allows you to link between directories,
• has different inode number and file permissions than original file,
• permissions will not be updated,
• has only the path of the original file, not the contents.

Hard Link
• ln source.file hardlink.file
• can't cross the file system boundaries (i.e. A hardlink can only work on the same
filesystem),
• can't link directories,
• has the same inode number and permissions of original file,
• permissions will be updated if we change the permissions of source file,
• has the actual contents of original file, so that you still can view the contents, even if the
original file moved or removed.
END OF DAY 2

You might also like