Sunday, June 22, 2014

UNIX BASICS FOR INTERVIEW

Unix Architecture:


·         Kernel:
The kernel is the heart of the operating system. It interacts with hardware and most of the tasks like memory management, and file management.
·         Shell:
When you type in a command at your terminal, the shell interprets the command and calls the program that you want.
C Shell, Bourne Shell and Korn Shell are most famous shells which are available with most of the Unix variants.
·         Files and Directories: All data in UNIX is organized into files. All files are organized into directories. These directories are organized into a tree-like structure called the file system.






ESSENTIAL UNIX Commands:
Command
Example
Description
ls
$ ls
$ ls -ltr
$ ls-a
Lists files (not content) in current directory.
-ltr List current directory in detailed format.
-a List current directory including hidden files.
cd

$ cd tempdir
$ cd ..
$ cd ~ jbk/web
Change current directory.
Change directory to tempdir
Move back one directory
Move into jbk’s web directory
mkdir
$ mkdir trialdir
Make a directory
Make a directory called graphics
rmdir
$ rmdir  trialdir
Remove directory (must be empty)
pwd

$ pwd
Get full path of your current directory
cat
$ cat  filename
Look at the content of file

vi 
$ vi filename
Creates a new file if it already does not exist, otherwise opens existing file.

Command mode- default mode of vi editor.
       -Saving file
       -Moving courser
                 k
              h     l
                  j
save the contents of the editor is :w!
out of vi is :q! or :q.
save and exit together :wq! or :wq.

Insert mode- to insert text into file
cat>
$ cat > demo.txt
This text print in above file.
Creating file using cat
echo
$ echo 'The only winning move is not to play.' > demo.txt
Creating file using echo
cp
$ cp file1 web-docs
$ cp file1 file1.bak

Copy whole directory
$ cp -r  sourcedir targetdir
Copy file.
Copy file into directory
Make backup of file1
rm
$ rm file1.bak
$ rm *.tmp
$ rm –i file1.txt
Remove or delete file
Remove all file
Confirmation before file delete
mv
$ mv old.html new.html
Move or rename files.
more
$ more index.html
Look at file, one page at a time
lpr
$ lpr index.html
Send file to printer
man
$ man ls
$ man mv
Manual pages (help) about command
cc
$ cc HelloWorld.c
C compiler. Will compile c program
cat /etc/issue
$  cat /etc/issue
To find which version of linux using
useradd
$ useradd kiran

$  useradd -g groupname                   username

$useradd -g kiran umesh

$ groups username
To add user

To add user in specific group




Will show user belongs to which group
userdel
$ userdel kiran
To delete account from the system
groupadd
$ groupadd developers
To add group
passwd
$ passwd kiran
Set password to account
ps
$ ps
Snapshot of current processes
tail


tail -f
$ tail filename
$ tail –n filename

$ tail -f  filename
Shows last few lines.
-n to show last n lines

Shows last 10 lines of file and monitor file for updates. i.e if line added to file tail then continue to output and keep refreshing.

find
$ find . *.txt

To find files in current directory or its sub directories.
Here . (period) represent  current directory
top
$ top
Display all system processes
grep
$ grep "this" demo_file

$ grep "this" demo_*
 
$ grep -i "is" demo_file
 
 

To search for specific string containing line in specific file.
To search for specific string in multiple file
Case insensitive search in file.

Please find below screenshot for details
About grep command






chmod (To change file or directory permissions):
While using ls -l command it displays various information related to file permission as follows: $ ls –l demo.txt
-rw-r--r--    1 cliff    user 767392  Mar  6 14:28 demo.txt
^ ^  ^  ^     ^   ^       ^      ^     ^      ^     ^     
| |  |  |     |   |       |      |     |      |     |       
| |  |  |     | owner   group       size      date       time     name
| |  |  |     number of links to file or directory contents
| |  |  permissions for world
| |  permissions for members of group
| permissions for owner of file: permission r = read, w = write, x = execute - =no
Type of file: - = normal file, d=directory, l = symbolic link, and others...
Each permission is assigned a value, as the following table shows, and the total of each set of permissions provides a number for that set.

Number
Octal Permission Representation
Ref
    0
No permission
  ---
    1
Execute permission
  --x
    2
Write permission
  -w-
    3
Execute and write permission: 1 (execute) + 2 (write) = 3
  -wx
    4
Read permission
  r--
    5
Read and execute permission: 4 (read) + 1 (execute) = 5
  r-x
    6
Read and write permission: 4 (read) + 2 (write) = 6
  rw-
    7
All permissions: 4 (read) + 2 (write) + 1 (execute) = 7
  rwx



chmod
$ chmod 755 demo.txt
$ chmod o+wx demo.txt
$ chmod u-x demo.txt
$ chmod g=rx demo.txt
$ chmod o+wx,u-x,g=rx demo.txt


chown
$ chown kiran  filename

The chown command changes the ownership of a file
chgrp
$ chgrp developers  filename

Changes the group of the given file to developers group.
 | (pipe symbol)
To sort students name we write:
$ who > students.txt
$ sort <  students .txt

Instead of it use pipe:
$ who | sort

$ ls –al | more
The symbol | is the Unix pipe symbol.
Give output of one command is input to other.

< (input redirection)
> (output redirection)
This is simple and fast.
tee
$ ls | tee file

$ ls | tee file1 file2

$ ls | tee –a file



It display the output and also saves it in the file at same time.

This is useful when you wanted to know which data going to store in your file.

-a instruct tee command to append data in file.
history
$  history
Record (or history) of commands is kept for your login session.
sudo
$ programmers
  ALL=(ALL) ALL

Allow permitted user to execute a command as a superuser.

ALL: Allow sudo access from any terminal (any machine).
(ALL): Allow sudo command to be executed as any user.
ALL: Allow all commands to be executed.

gzip
$ gzip readme
 
$ gzip –c readme
 
Compress files and save them in .gz format, to occupy less space than original
Compress the file named readme. Creates readme .gz and deletes readme.
-c keeps readme.
gunzip
$ gunzip readme.gz
 
Decompress the .gz file
tar
$ tar –cvf   /archives/data-history.tar    data/notes
Tape archive.
It create single file with the content of directory structure.
–cvf to view tar progress
–tvf to view content in tar file
rpm
$ rpm -ivh mozilla-mail-1.7.5-17.i586.rpm
 
$ rpm -Uvh mozilla-mail-1.7.6-12.i586.rpm
 
$ rpm -ev mozilla-mail
 
$  rpm -qa
Red Hat Package Manager.
RPM command is used for installing, uninstalling, upgrading RPM packages on Linux system.
Package can be archive file
-ivh  Install the package
-Uvh upgrade the package
-ev erase the package
-qa display installed packages
su
$ su kiran
$ su root
To change the ownership of a login session to root or to any other user.
halt
reboot
poweroff

$ halt
$ reboot
$ poweroff
Reboot or stop the system
kill
$ kill processname
To kill running process
shutdown
$ shutdown -h now
$ shutdown -h 20:00
$ shutdown -r now
$ shutdown -c
 

Shutdown the system in a safe way. shutdown the machine immediately, or schedule a shutdown using 24 hour format.
-h halt the system
-r reboot the system
-c cancel a running shutdown

 

awk
$ awk '{print;}' employee.txt

$ awk '{print $2,$5;}' employee.txt

Awk command is used to generate
Formatted output.
It uses  search pattern is a regular expression.
If the line has 4 words, it will be stored in $1, $2, $3 and $4 variabe.

NF represents total number of fields in a record.

$2,$5 will print 2nd and 5th colum
alias
$ alias ls="ls -al"
$ alias p="pwd"

p and pwd now work same
Allow user to give simple name for
Complex commands combination
vim
Go to 143rd line of file
$ vim +143 filename.txt
 
Open file in read only format
$ vim -R filename

To provide extra file handling features
sed
Replaces the word "unix" with "linux" in the file.
 
$ sed 's/unix/linux/' file.txt
 
Replaces the second occurrence of the word "unix" with "linux" in a line.
 
$ sed 's/unix/linux/2' file.txt
 

Sed command is mostly used to replace the text in a file
sort
$ sort
$ sort names.txt
$ sort -r names.txt

Used to sort
-r for descending order
whereis
$ whereis ls
To find out where a specific Unix command exists
whatis
$ whatis ls
 
Single line description about a command.
mysql
$ mysql -u root –p root
 
To connect mysql database




Some basic Unix commands:
cal, date , time , who , whoami , sort , echo, df, du, ping, logout
How to create and run perl script:
1. Create a new text file and type the following exactly as shown:
#!usr/bin/perl

print "Enter your name: ";
$ name=<STDIN>;
print "Hello, ${name} ... you will soon be a Perl
creater!";
Save it as hello.pl to a location of your choice.
2. Back at the command prompt, change to the directory where you saved your Perl script.
$ cd c:\perl\scripts
$ perl hello.pl
you'll be prompted to enter your name.
Enter your name: Kiran
Hello, Kiran
... you will soon be a Perl creater!
-e with perl allows you to run code from the command line, instead of having to write your program to a file and then execute it.
$ perl -e 'print "Hello World\n";'
Output:
Hello World

To protect your directory:
We need to create two files .htaccess and .htpasswd in our directory

.htaccess file contains security directives for the specific directory

.htpasswd file contains the userids and associated encrypted passwords.
.htaccess file must contain:
AuthUserFile /users/www/userid/welcome_html/class1/.htpasswd
AuthGroupFile /dev/null
AuthName Private
AuthType Basic
<Limit GET>
require user USERID1 USERID2
</Limit>

.htpasswd

.To create this file use command


htpasswd -c .htpasswd USERID1