100% found this document useful (1 vote)
2K views

Tcs Ipa Unix Part 1, 2

It covers tcs xplore unix course notes for part 1, 2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
2K views

Tcs Ipa Unix Part 1, 2

It covers tcs xplore unix course notes for part 1, 2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 50

Linux

 Andriod , Tizen OS(samsung) , webOS (LG) , Sailfish mobile OS ( Jolla) , Firefox OS ( Mozilla) is
based on Linux .
 Apple’s ios is based on BSD unix , a close cousin.
 60 % of all websites run on Linux
 Majority of DB servers are on Linux
 Scripting languages like PHP , Python , Ruby are open source and run on linux
 Cloud based companies like googl;e , amazon , twitter , facebook rely on linux to provide
their services.
 Devices running Linux - Roku and Tivo media players , Linksys , Buffalo and Netgear routers
i.e home networking equipment , samsung and Barnes and e-readers , google’s
chromebooks.
 GNU is a unix like OS made up of many applications , libraries and developer tools. The
development of GNU started in January 1984 is known as GNU project.
 GNU is an acronym for GNU’s Not Unix
 Attributes of Unix – Powerful , stable , secure , modular
 Linus Torvalds combined his kernel with the GNU tools, compilers and libraries to form linux
 Later linux kernel , combined with a lot of software from other projects , including GNU
project , is known as distribution - Red Hat linux distribution – 1994
 Closed source doesn’t allow users to improve it , most closed source software can’t be
distributed freely
 Open source encourages collaboration and allows users to make changes , can be distributed
freely .Linux is open source and anyone can contribute.
 DistroWatch.com – contains various linux distributions and popularity - mostly debian or red
hat linux
 Distributions based on Debian – Ubuntu , linux Mint , kali
 Debian is one of earliest distributions - created by Ian Murdock – 1993 – maintained by
community and not associated with any corporation – has online software library with
50,000 software packages - focuses on freedom and officially includes free software –
manages software packages with advanced package tool ( APT )
 Distributions based on Red hat – CentOS , Fedora , Mandriva
 Red hat linux – 1994 –commercial distribution - uses RPM( red hat package manager ) for
software package management.
 Non standard linux - Andriod , Tizen OS(samsung) , webOS (LG) , Sailfish mobile OS ( Jolla) ,
Ubuntu Touch – difficult to install , need SDK

1. Why is it imperative to select the "make this user administrator" box during install?" It allows you
to log in as a user and still be able to do administrative tasks

2. Virtual machine snapshots allow us to _____ at any time. roll back to a previous snapshot

3. What is a major difference between GNOME 3 and other desktop operating systems? Most
applications are run by going into overview mode and typing the application name. GNOME 3 does
not by default have an application menu.
Part 2 basic commands
 Major distributions of linux falls under 4 categories – Arch , Debian , RedHat , Slackware
 The Bash shell is available on most distros and Oses.
 If we don’t have GUI , we can interact through CLI
 CLI ( command line interface ) is a text based interface where we type commands and direct
text based input and output to the screen , to files, and to other programs. Also known as
Shell or interpreter .
 General command syntax : command options arguments

 Command is mandatory , others are optional.


 Options tell the command how to operate , changing the behaviour of command , pr telling
it to perform different actions. Most options start with a dash and usually one letter . Most
commands offer one or more option .
 Argument tell the command what to operate on . its usually a file path or a set of files or
folders .
 ls – list contents i.e files and folders in current directory
 ls -l : long listing – list with more details
 Tab completion feature in shell – automatically completes a file or folder name , makes
guesses based on contents of the path
 man – open manual pages – built in documentation for commands
 apropos - searches for a command and their descriptions for text you provide as an
argument- use when you don’t know exact name of command
1. The command line is also called the _____. Shell
2. A shell that's available on many different platforms is called: bash
3. You enter the command list and receive an error containing suggestions for alternate
commands. Why did this happen? list is not a valid command.
4. To find the manual page for the command ip, you can use: man ip
5. You are trying to list the contents of a directory, but you forget the options you can add to ls.
What shows you a brief amount of information about the command you entered? ls –help
6. To cancel a command you've typed but don't want to run, press: Ctrl + C
7. Why isn't the statement /home/alice ls -l valid? The argument should be at the end of the
command, so it reads ls -l /home/alice
8. To help you look up a command by its description rather than its name, you can use: apropos
9. To move to the beginning of a line of text, you can use: Ctrl + A

3. Files , folders and permissions

 Files are collections of information that represent photos , docs, source code and databases
 file command – file myfile.txt - determines file type
 stat command – stat myfile.txt – display ownership modification information etc. - excess
info
 cd – change directory - we can type the path we want to go to , eg : cd Documents/
 pwd – print working directory
We got error because exercise files is interpreted as two words , two separate arguments as there is
space in between the words . we have to tell that space is not a separator between two arguments
or commands.In this case we can put string of text inside quotes or just escape special characters i.e
type aback slash in front of it.

 ls -R : to list folders recurively – to explore whole structure of programs.


 cd - : takes you back to previously used folder .
 cd - takes back to home folder
 ls -lh : displays detailed listing similar to -l , but file sizes are shown in human readable
format
 mkdir - to create new directories. We can also specify where to create directory by giving
path.
 Eg – mkdir departments/customerservice
 mkdir -p : allows you to create a directory structure , including any necessary parent
directories that do not already exist , syntax : mkdir -p/path/to/directory

 Rmdir – remove directory


 cp file1name file2name where you want to copy file1name to file2name
 mv – to move files to specific path eg : mv file1.txt depts/marketing/

Wildcards in linux are special characters used in command line interfaces to represent one or more
characters in filenames or commands. Commonly used wildcards are asterisk , question mark
 – any number of characters eg – ls *.txt lists all files with a .txt extension in current directory
cp file* /backup/ copies all files that start with “file” to the /backup/directory
 ? – one character – eg – ls file?.txt matches file1.txt, fileA.txt , but not file10.txt
 [ ] – represents any single character within the brackets eg – ls file[1-3].txt matches file1.txt ,
file2.txt and file3.txt , but not file4.txt
 { } – used to create multiple text strings eg- echo file{1,2,3} produces file1.txt,
file2.txt,file3.txt
 Wildcards can be used in various commands like cp , mv , rm , and grep
 find command – search for files and directories in directory hierarachy based on various
criteria . syntax : find [path][options][expression]

user roles

Linux is a multi user environment and user’s files are kept separate.

su command – switch between users , to use this command , type su followed by the name of the
user we want to switch to.

Normal user can modify their own files but cannot make system changes they cant browse other
users home folders

Superuser or root , can make changes to system , install software , start and stop the services

Sudo command gives superuser privileges and tells to run whatever command given

File permissions

 The first represents the user , or the owner of the file


 The second group of three represents the group that owns the file
 The third group represents all other users not in the group that owns the file.
 Rwx - read, write , execute

Chmod – change the permissions on a file by modifying the file mode bits and this can be done in
two ways

Two methods to represent permission – octal ( eg 755 , 644, 777) or symbolic notation ( eg a=r , g+w
, o-x) which uses shorthand for user , group , others and all, an operator and a list of permissions to
change .
 For eg : to set my user permissions to read , write and execute , I would use chmod u+rwx
 To set my group permisssions to only read , I would use chmod g=r
 To remove read, write and execute from others , I would write chmod o – wrx
 Test.sh – small bash script - prints out a line of text when we run it.
 ./ before file name is one way of running executable files at command line. Executable means
file can run on its own without having to be loaded by another program first .
 Cat command – displays contents of a file
 Touch command – to create new file
 Chown command - changes the owner or group of a file or directory , syntax : chown
[options]new_owner:new_group file
 Links are files that are references to other files , and they are used to avoid having multiple
copies of same file in different places. Hard links point to data on the disk (inode) and
symbolic links or soft link points to a file on the disk ( relative path )
 I can create a symbolic link with the ln command and a -s option. Then I give it the name of
source file , the file I want to make a link to. And then the name of the link I want to create.
 Know that this kind of link is relative, that is if you move the link somewhere else on the file
system the system wont be able to reference the original file anymore.
 We can create hard link by leaving off the -s option
 A hard link appears to be a regular file in a file listing but its also just a pointer to original file
or more specifically it’s a pointer to the data that the original file references.
 Hard links can be moved around the file system and it doesn’t matter if the original file is
moved , because a hard link points to the underlying data for a file instead of file itself.
 Infact every file on your system is a hard link to its underlying data.
File system hierarchy standard

Most linux distros follow filesystem hierarchy standard which contains where certain files are stored
on filesystem.

 You want to make a copy of the poems.txt file. Which command would you use? cp
poems.txt poems2.txt
 To delete a folder called Photos which contains many subfolders, you could write rm -rf
Photos
 The -r option tells rm to remove the file hierarchy, not just files. And -f skips the prompt that
rm would normally display for each folder.
 To rename the file report.txt to output.txt, you could write: mv report.txt output.txt
 While mv is usually used to move files, 'moving' a file within the same directory has the
effect of renaming it
1. Why would a normal user not do any harm by trying to change system files and folders?
Normal users do not have sufficient permissions to modify these files and folders.
2. The rmdir command _____. can only delete an empty folder
3. While the rmdir command can only delete empty folders, you can use rm -rf to delete non-
empty folders and trees of folders.
4. While all other users' home folders are kept in /home/, the superuser's home folder is stored
at: /root
5. A symbolic link will break if: the link or the referenced file is moved . If you want to make a
link that can be moved without breaking, choose a hard link instead.
6. You have come across an unknown file named myfile.txt. Which command would you type to
display ownership or modification information about this file? stat myfile.txt
7. If a file path has a space in it, you can wrap the path in quotes, or you can _____ the space
with a \ character. Escape
8. The command ln -s outcome.txt report.txt creates: A symlink named report.txt that refers to
outcome.txt. Remember that a symbolic link will break if the link or the original file is moved.
9. If your current working directory is /home/alice/Documents/Financial, and you want to
change it to /home/alice, you can use this command: cd ../../
10. File permissions let us control who can: read, write, and execute a file
11. We use this command in front of a command we want to execute using superuser privileges:
sudo
12. To find files in Alice's home folder with report in the name, you could write: find
/home/alice -name "*report*"

Part 4

UNIX philosophy – programs or tools should do one thing and do it well , they should use text
interfaces (take in text , and output text once it’s done processing ) , many modular tools rather than
one big one , standard GNU tools follow this pattern

Pipes – to take output of one command and send it to another

Pipe character - | - we type this character in between the commands that we want to be piped
together

Echo – prints what we give

Wc comand – display info by counting number of lines , words , and characters in files or given input.

Cat command – concatenate – to link things , can be used to putpput text file contents to the screen
or to another tool

Head, tail – view lines from the beginning or end of a file , we can pass -n option to ask how many
lines to get displayed
Less command – to view contents of a text file , allowing you to navigate nthrough the file
interactively . used to view large files bacause it loads and displays parts of file as needed rather than
loading entire file at once .

Grep command – nsearches text or files for a given string or pattern of text

-i ignore case distinctions in both pattern and input files, -r search for a pattern recursively in all files
under a dir , -c display only the count of lines that match the pattern , -n shows line numbers along
with matching lines , -w match only whole words, -v displays lines that do not match pattern

Manipulating text : the awk and sed commands can both be used to programmatically manipulate
text in streams or files

Awk : prog lang designed for patttern canning and processing , operates on lines of input , allowing
to perform actions based on patterns

Syntax : awk ‘pattern {action}’ input_file

Eg :
Sed : stream editor – for parsing and transforming text in pipeline.
 rev command – reverses the characters in each line of file or standard input , syntax :
rev[file]
 tac command – concatenates and displays files in reverse order , meaning it will display last
line first and the first line last , syntax : tac[file]
 tr command – translates or deletes characters from standard input , used to replace or
remove specific characters. . syntax: tr[options] SET1 [SET2]

editing text

vim or vi - this software is a powerful and flexible command -line text editor .
Nano editor - lightweight text modification tool , to open a fil ein nano , nano filename , if file
doesn’t exist , nano create a new file with that name , to save ctrl+o , to exit nano ctrl + x

Tape archives – put many files toegther into one file

.tar files : common way to package and distribute software and data , data compression is optional ,
compressed formats : tar.gz ,.tgz , .tar.bz2

To create compressed files ,

To extract ,
Zip and unzip commands – create and open compressed data archive files.

Redirection - text in a shell travels through one of three streams , text can be redirected to screen or
to files

Env command – used to print or set environment variables.


1. To view only the first eight lines of the file report.txt, you could write: head -n8 report.txt
2. On a Linux system, the PATH environment variable represents: locations which the shell will
search for executable programs , Without being able to search in the PATH, we would have to
type out the full path to common tools every time we used them.
3. You redirect output to a specific file but when you open it, you find the file is missing its
original contents. Why are the original contents gone? You used a single > sign, which
overwrites the contents of the file.
4. Input and output streams are numbered. Which of these represents the standard error? 2
5. What purpose do zip files serve in Linux? They allow us to combine files in a space-saving
way, and are commonly used when we exchange files with other platforms or across the
internet.
6. The UNIX Philosophy can be summed up as: Tools should do one thing well, and
communicate with each other using text.
7. This character represents a UNIX pipe: |
8. To extract files from a tar file, use the option: x
9. In nano, common commands are listed: at the bottom of the screen
10. In nano, what does Ctrl + O (^O) do? saves (outputs) the file
11. Primarily, Vim has two operating modes: command mode and insert mode
12. To modify information from a file or stream, this is the best command of those listed to use:
sed
13. How would you alter the below code to add numbers to the output lines?
14. grep "the" poems.txt - grep -n "the" poems.txt
15. To show the lines from the file report.txt which match the search term "completed", write:
grep "completed" report.txt
16. This command lets us scroll around and search in a text file: less

Module 5

Uname -a used to display detail;ed information about systems kernel and hardware like kernel
name , node name , kernel release , kernel version , machine , processor , hardware platform , os .

Free -h is used to display info about the systems memory usage in human readable format.
Cat /proc/cpuinfo used to display detailed info about CPU on system .

Df -h used to display information about disk space usage on mounted file systems.

Sudo du/-hd1 used to display disk usage of directories.


Managing software with packages

To add software to system we can use package manager


1. You can find out how much available disk space a system has with: df -h
2. This command can show you what version of the Linux kernel is currently running: uname ,
To get the full amount of information, use the -a option.
3. While most commands work on most Linux systems, it's important to know the _____ you're
working on, to be aware of differences in package managers and configuration schemes -
distribution
4. You are trying to install software and run the tree command. You see the software is not
installed. Which command would you use on Ubuntu or Debian Linux to install this software?
- sudo apt install tree
Part 3 : shells and process

 A shell allows you to execute operating system commands.


 When a command is entered in terminal , shell checks if it’s a built-in command , also
checks if its an alias of a command , checks if the command is on the hard disk and tries
to execute there , if it doesn’t exist anywhere , it will say “command not found”.
 There is a standard for unix like os called POSIX – compliant shell. One of the first shell
was bourne shell – sh ( 1977 )
 Csh shell – c shell – syntax resembled the c language – no used anymore
 1983 – ksh or korn shell introduced adding job control and command history
 later Bash (bourne again shell ) was introduced as a replacement for bourne
 Bash - can be mostly POSIX compliant , expose array indices , regular exp conditionals ,
increment assignment operator , current version : Bash 4
 Dash – mostly compatible with Bash , POSIX compliant , low-memory usage , low-
memory usage , faster execution , doesn’t have a command history , not easily installed
in centOS
 Fish – friendly interactive shell – most compatible with bash , designed for interactivity ,
syntax highlighting on the CLI , interactive autosuggestions , cleaner syntax for
conditionals and loops , only available in third party EPEL software repository .
 Zsh(z shell ) – mostly compatible with Bash , wonderful command completion , better
pattern matches on command line

Installing shells

sudo yum install -y zsh

sudo yum install -y ksh

sudo yum install -y csh


Shell session variables

 Environmental variables – variables that are defined for the current shell and are inherited
by any child shells or processes.
 Shell variables – variables that are contained exclusively within the shell in which they were
defined, they often keep tracking of data like current working directory.
 To see environmental variables in a terminal , type printenv and hit enter.
 If you want to get value of individual variable you can specify it

To see the shell variables , we use the set command . By default set command shows shell functions
as well as variables which can be quite long. To change the behaviour of set to POSIX mode , which
doesn’t show functions , use -o posix option , to unset any shell option , use +o
 If you want to set a variable for all users , set it in either the /etc/profile or a script in
/etc/profile.d
 If you want to set a variable specific to a user , change it in the user’s.bash_profile.
 If you want to add aliases or functions for all users, add them to the /etc/bashrc
 If you want to add aliases or functions specific to a user , add them to the user’s.bashrc
 Note: when you add aliases or functions to the user’s.bashrc file , be sure add them to the
end of the file after /etc/bashrc has been called
 To verify where we are in filesystem , type pwd into terminal and typing in cd .. takes us to
our parent directory

Histcontrol command - it is used to manage how the command history is stored and displayed

 To ignore commands that are completely empty or only contain spaces : export
HISTCONTROL=ignorespace
 To ignore both empty commands and commands that start with a space : export
HISTCONTROL=ignoredups:ignorespace
 to ignore duplicate commands : export HISTCONTROL=ignoredumps
File globs
File globs : character classes

Pattern matching with extended globs

Type shopt and hit enter . check if extglob is turned on


 If it is not turned on , type shopt -s extglob and hit enter
 Extended globs grant us the power to specify the number of matches , allow grouping
matches , know patterns can be more than one character , logical OR
1. A set comprised of two square brackets _____. matches one character which can be any of
the items in the range or list between the brackets.
2. What command brings back the entire previous line? !! , It can be appended to the end of a
command as well such as sudo !!
3. What does an asterisk in a file glob match? any type of character and any number of them
4. If you want to match whole words, what do you need to use? brace expansion
5. What command executes the last line, starting with ls? !ls
6. Typing cd .. takes you to _____. the parent directory
7. To do a reverse-i search through your command history, what should you do? press Ctrl+R
and start typing
8. If you want to use extended globs to match exactly one occurrence, you should use _____ .
the + operator
9. To keep commands from being saved in our command history you should _____ export
HISTCONTROL=ignorespace in your shell startup files and precede the command name with a
space

Shell meta characters

Bash shell has characters called meta characters that have special meaning.

Escaping characters and quoting

 If we want ot get the value of a variable , we can use echo command and prefix the variable
with dollar sign , this tells echo to read the value of variable
 Single quotes tell the shell to present the characters inside literally. We use these to pass the
characters to a command unaltered by the shell . $ has no value inside single quotes.
 If you only want to escape one character , use backslash , we can also use backslash to
escape single quotes.

 Type ls -lh and hit enter . the top two items are the . and .. directories.

 The tilde( ~ ) is a meta character with special meaning , it allows us to expand path to include
directories like the users home , the current directory , parent directory or even previous
directory
Command and variable substitution

We can insert a variable into a textstring or command line and can have the variable value subsituted
for the variable name.

Unnamed pipe

 Common use of pipe is taking output of one command and sending it as input of another
command .
 Unnamed pipe is direct connection between two commands running in the same terminal .
 If we want to send output from a command in one terminal to another command in different
terminal , we can use a named pipe or FIFO . this is pipe that exists in file system
Redirect –

1. What is the key difference between a redirect and a tee? The tee sends output to STDOUT
and a file.
2. You can place directories on the directory stack using the _____ command. Pushd`

Linux processes

A program is an executable file that is stored on disk.

A program is a passive entity until it is launched , at which time it becomes a process.

Every process ha unique ID number .

The process ID number is used by kernel to manage and control the process during its lifetime.

System administrators use the process ID for changing task priority and ending tasks.

Ps command – to monitor processes , by default , ps only shows the processes run by the user
executing it. Oddly ps command have 3 types of syntax options : unix , BSD , GNU

To get ps to display every process , use ps -e

To give us an idea of which processes start at other processes , we pass hierarchy option , pe -eh

To show more info about processes , type ps -ef option

Ps -elf for long format

Ps -e –format shows the user , process id , parent process ID, cpu utilisation and command for all
processes .

Ps -u – process run by root user

Top command – to display real time process information , display will be updated for every couple of
seconds top shows how long the system has been up, the load average , how many process are
running , how many are waiting for cpu time , overall cpu usage and memory usage.

Press L to show or hide the load average / uptime line.

Press number 1 if you want to show CPU usage for all CPU cores if you have more than one.
Press t if you want to toggle between task and cpu states.

Pressing num 1 and T allows to have a solid bar for each cpu core

Press M to toggle between diff memory displays or turn off memory statistics

Press c to toggle between command name and command line

Press U followed by username to show only processes run by that user

 Higher the nice value , nicer the processis to the cpu and thus less priority it gets.

GNOME system monitor – to monitor processes and other system resources graphically , we can find
this application in application menu , system tools and system monitor.

GNOME monitor has three tabs : processes , resources and file systems

processes tab shows running processes on the system started by the user , the process porperties
shows the status , who ran the process , memory usage , cpu usage , nice level and process ID

resources is for CPU , MEMORY , Network usage history

to get process id we can use ps command or pidof or pgrep

pgrep is more powerful and allows us to filter on lot of items like username and command

Once we get process id , we can use kill command to send it a signal

To list all possible signal types , type in kill -l

There are 64 diff signals

Generally , we want to send the process a non destructive signal , like SIGHUB or SIGUSR1 , or we
want to terminate a proces with SIGTERM or SIGKILL , in many cases , SIGHUB forces a process to
reread its configuration file.
This returned process id in firefox

If we don’t specify a signal , the default number is 15 ,a SIG term , we can see firefox process is
termintaed , it no longer listed in app switcher at bottom of screen.

DD command – to duplicate disks , but no visual output.

Process niceness / priority

Linux process priorities are handled by a system called nice.

Nice levels go from -1 to -19 for privileged users and 0-19 for non privileged users. Default nice level
is 0.

The nicer a process is to CPU , the less it interrupts it and the lower its priority is .

Higher the nice number , lower the CPU priority

You might also like