0% found this document useful (0 votes)
37 views15 pages

Linux Environment and Tools

This document provides an overview of Linux environment tools. It discusses the history of Linux and Unix, how Linux distributions are comprised of the Linux kernel and GNU tools. It also covers setting up passwordless SSH, reasons to learn command line tools like control and availability. The rest of the document demonstrates various Linux commands like find, awk, sed and provides real examples of combining commands to automate tasks. It concludes with showing how to view installed software and provides exercises for readers to try out commands.

Uploaded by

Pratha Garg
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)
37 views15 pages

Linux Environment and Tools

This document provides an overview of Linux environment tools. It discusses the history of Linux and Unix, how Linux distributions are comprised of the Linux kernel and GNU tools. It also covers setting up passwordless SSH, reasons to learn command line tools like control and availability. The rest of the document demonstrates various Linux commands like find, awk, sed and provides real examples of combining commands to automate tasks. It concludes with showing how to view installed software and provides exercises for readers to try out commands.

Uploaded by

Pratha Garg
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/ 15

Linux environment and Tools

Subendu Santra
[email protected]
May 2023
History
● https://eylenburg.github.io/os_familytree.htm

● https://upload.wikimedia.org/wikipedia/comm
ons/7/77/Unix_history-simple.svg

● GNU coreutils
Linux
● Linux kernel (Linus Torvald) and GNU Project
(Richard Stallman)
● True UNIX vs UNIX like – Open Group Consortium.
● Linux distribution = GNU/Linux Kernel + GNU tools
+ Documentation + package manager + Window
system + desktop environment
● 1000’s of Distros available - RedHat, Ubuntu,
Fedora, Debian etc
Lets setup passwordless ssh…
Source Machine Destination Machine

1. ssh-keygen -t rsa 3. vi ~/.ssh/authorized_keys


2. cat ~/.ssh/id_rsa.pub 4. chmod 600 ~/.ssh/authorized_keys

ssh, a4 ssh, scp, a4 scp


Why learn command line tools
● Mandatory – That's the only option available.
● More control over the machine – Lot of options
to tweak.
● Faster than using GUI.
● Lends itself naturally to scripting and can be
used for automation.
● Available everywhere.
Shell

Looks like an outer layer around the OS (shell


around an oyster)

Interface to an OS
Takes user commands and hands them over to the
OS to execute
Terminal

Used to be a hardware (screen, keyboard),


nowadays it is a software that runs the shell.

Terminal → Shell(bash, zsh etc) → OS


Some useful commands
whoami rmdir tail [-f] uniq ps sleep ln
[-d, -u, -c]

man [help] rm date diff top gzip who


[-fr] [-r, -u] [ -k, -d ]

clear touch cat find kill gunzip su


[-x]

pwd open less grep killall tar sudo


(MAC only) [-r] [-cvzf, -xvf]

ls mv echo du jobs nano passwd

cd cp wc df fg alias chown

mkdir head [-n] sort history bg xargs chmod


[-p] [-u, -n] [ctrl-r, !, fc ]
find
find /path [args] -exec [cmd] {} \; for each found result, the command cmd is executed once with the found result.
cmd result1; cmd result2; …; cmd result N

find /path [args] -exec [cmd] {} \+ for all found results, the command cmd is executed with all the found results.
cmd result1 result2 … result N

find /tmp/ -type f -mtime +10 -exec ls -l {} \; List files older than 10 days in /tmp directory
find /tmp/ -type f -mtime +10 -exec rm -rf {} \; To remove files older than 10 days in /tmp directory

find / -type f -name ‘howtouselinux*’ -exec mv {} {}_renamed \; The files are renamed with _renamed extension

find /tmp/dir1/ -type f -exec chown root:root {} \; -exec chmod Each consecutive -exec command is executed only if
o+x {} \; the previous ones returned true (i.e. 0 exit status of the
commands).
awk (Aho, Weinberger & Kernighan)
SYNTAX: awk 'pattern{action}' file

First field of each line awk '{print $1}' file

Second field of each line awk '{print $2}' file

Skip first line and print first field of other lines awk 'NR!=1{print $1}' file

Print entire line awk '{print $0}' file


awk '1' file
1 means True

Field separator of , instead of space awk -F"," '{print $1}' file

Another syntax awk -F, '{print $2}' file

Print multiple fields awk -F"," '{print $1, $3}' file


awk '{print $1,$3}' FS="," file
awk special variable FS

awk special variable FS and OFS awk 'NR!=1{print $1,$3}' FS=”,” OFS="," file
sed (Stream EDitor)
Append to the beginning s/^/Fruit: /

Append to the end s/$/ Fruit/

Replace first occurrence s/a/A/

Replace all occurrences s/a/A/g

Replace only 2nd occurrence s/a/A/2

Replace all occurrences from 2nd occurrence s/a/A/2g

Replace only in a specific line 3s/a/A/g

Replace in a range of lines 1,3s/a/A/g

Entire line with something s/.*/& is a Fruit/

Multiple substitution s/a/A/g; s/p/P/g


Real example
a4 mut show testing | /usr/bin/grep "started\|investigating" | awk '{print $1}' | tr '\n' ' ' |
sed -n 's/\([0-9]*\) / -i \1 /gp' | xargs a4 mut override testing --reason "XXX"
Explanation:
a4 mut show testing has output like:
129881076 23394813 started ...
129881074 23394813 overriden ...
129881151 23394813 passed ...
129881152 23394813 investigating ...
● We are only interested in lines which has "started" OR "investigating"
● We want the mtsID which is the first field ($1)
● tr - translate - We want to replace \n with " " therefore the output is going to be a stream of numbers separated by " ":
[<num1> <num2> <num3> ...]
● We are searching and replacing each number with "-i number" therefore the output is going to be: [ -i <num1> -i <num2> -i
<num3> ...]
● We treat all of this as argument to a4 mut override testing --reason "XXX"and pass them as [-i <num1> -i <num2>
-i <num3> ...]
How to view what software is installed ?

● rpm ● yum
○ rpm -qa ○ yum repolist
○ rpm -qi ○ yum install
○ rpm -qf ○ yum remove
○ yum reinstall
○ rpm -ql
○ yum update
○ rpm -q --requires |
--provides
Exercises
1. Count the number of lines, words and characters in a file. Get hold of a .c
file and count the number of for loops in it.
2. List all the files with a given pattern and check if a word is present in any of
them
3. Check the current disk usage.
4. List all processes running on the system. Can you identify process ID and
parent process ID.
5. Create a .tar file. List the contents of a .tar file without extracting it.
Thank you

You might also like