Linux and Shell Scripting: Shubhankar Thakur (Ethashu)
Linux and Shell Scripting: Shubhankar Thakur (Ethashu)
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 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.
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.
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
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