Tcs Ipa Unix Part 1, 2
Tcs Ipa Unix Part 1, 2
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
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.
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
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
Pipe character - | - we type this character in between the commands that we want to be piped
together
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
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
.tar files : common way to package and distribute software and data , data compression is optional ,
compressed formats : tar.gz ,.tgz , .tar.bz2
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
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.
Installing shells
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
Bash shell has characters called meta characters that have special meaning.
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
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 give us an idea of which processes start at other processes , we pass hierarchy option , pe -eh
Ps -e –format shows the user , process id , parent process ID, cpu utilisation and command for all
processes .
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 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
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
pgrep is more powerful and allows us to filter on lot of items like username and command
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.
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 .