Time To Earn

Showing posts with label Unix Symlink Creation. Show all posts
Showing posts with label Unix Symlink Creation. Show all posts

Saturday, November 30, 2013

Unix Job Interview Questions

Adding 30 days to current date using PERL inside SHELL

#! /usr/bin/ksh

dt1=`perl -l -e '$date=(localtime(time + (30 * 24 * 60 *60))); print $date;'`

dt=`echo $dt1|awk '{if($3<10) print $1" "$2" 0"$3" "$4" "$5;else print $0}'`

echo $dt|awk '{print $5"-"$2"-"$3"23:59:59"}'|sed -e 's/Jan/01/g'\;'s/Feb/02/g'\;'s/Mar/03/g'\;'s/Apr/04/g'\;'s/May/05/g'\;'s/Jun/06/g'\;'s/Jul/07/g'\;'s/Aug/08/g'\;'s/Sep/09/g'\;'s/Oct/10/g'\;'s/Nov/11/g'\;'s/Dec/12/g'





Print 3 lines before pattern match using sed and awk ..

Q: How do you display your running kernel version? (Solaris, AIX, Linux)
A: Linux # uname –r , Solaris # showrev

Q: Which command do you use to display a table of running processes? (Solaris, AIX, Linux)
A: Linux # ps –ef and top , Solaris # prstat

Q: Which file do you modify to configure a domain name resolver? (Solaris, AIX, Linux)
A: Linux # /etc/resolv.conf , Solaris # /etc/resolv.conf

Q: Which file contains a list of locally defined hostnames and corresponding IP addresses? (Solaris, AIX, Linux)
A: Linux # /etc/hosts , Solaris # /etc/hosts and linked file /etc/inet/hosts

Q: How do you display a routing table? (Solaris, AIX, Linux)
A: Linux # ip route show or #netstat –nr or #route –n and Solaris # netstat –nr and #route -n

Q: Which command would you use to view partitions and their sizes on Solaris?
A: # df -kh

Q: Which OpenBoot command would you use to print/view OpenBoot environment variables on a SUN server?
A: #printenv

Q: What does "ypwhich" command do? (Solaris, AIX, Linux)
A: # Will display NIS server to which client is connected to and which NIS Server is master for particular map specified with this command

Q: which command would you use to create an OS user on Solaris and Linux?
A: Linux # useradd and Solaris #useradd

Q: Which file contains passwords for local users on Solaris, Linux and on AIX?
A: Linux #/etc/shadow and Solaris # /etc/shadow

Q: Which command would you use to list partitions on Linux?
A: Linux # mount –l or # df -kh

Q: Which command/commands would you use to manage installed packages on RedHat Linux?
A: Linux # rpm

Q: What is the default port for SSH server?
A: 22

Q: Which command/commands would you use to manage installed packages on Solaris?
A: #pkginfo #pkgrm # pkgadd #pkgchk

Q: What command would you use to install an OS patch on Solaris?
A: #showrev –p and #patchadd -p

Q: Which Veritas command would you use to display a list of Veritas volumes?
A: # vxprint

Q: Which Veritas command would you use to display a list of disks on a system?
A: # vxdx list

Q: What is the main system configuration utility in AIX?
A:

Q: Which file has a list of filesystems to be mounted at boot time on Solaris, Linux and AIX?
A: Linux # /etc/fstab and Solaris #/etc/vfstab

Q. How to change all .my files to .txt in current directory??
Solution: for i in `ls *.my` ; do NEW=`echo $ised -e 's/.my/.txt/g'` ; mv $i $NEW ; done

Q. How to change all .extn files to .my in entire directory recursively??
Solution:
find ./ -type f grep ".extn$" >files.txt;for i in `cat files.txt`;do NEW=`echo $ised 's/.extn/.my/g'`;mv "${i}" "${NEW}";done;rm -f files.txt

Q: What is umask??
Answer: umask is used to set default permission level in entire unix system. It can be set in .kshrc/.bashrc/.cshrc or init (So that to load at the time of login into shell)

Q. What is inode??
Answer : inode represents any file/directory residing in your unix system with a numner known as inode number. It is unique for every file/dir. It is basically a reference that kernel use for any work related to that entity. "ls -i filename" will give you the inode number of that particu;ar file.

Q. Explain general commands like ps,df, export, env, ufsdump, tar, cron, system.
Answer: EASY ONE

Q. Explain the make utility in unix?? what are the parameters of a makefile and how to use them. What are macros in makefile.
Answer: Unix make utility is used to build a software package. It requires a makefile where all the bunch of the scripts/codes/programs are declared/defined. The main parameters of the make file are variable declaration, Macro declaration, Phony Target declaration and Clean-up block. Macros in a makefile represents the order in which the scripts would get executed.

Q. What are the benefits of makefile over shell script??
Answer: Using makefile one can run the different scripts or programs or codes in one go. This can also be done by making a wrapper shell script which can execute the different scripts in one go. But then it would not be termed as a package because when we say a package it means that we have one installer (.exe) which can execute the bunch more efficiently and also this .exe will be in encrypted format. So in order to maintain efficiency and security makefile is given precedence over wrapper shell scripts.

Q. What are hard links in UNIX??
Answer: A hard link is a reference to a file or directory that appears just like a file or directory, not a link. Hard links only work within a filesystem. In other words, don't use hard links between mounted filesystems. A hard link is only a reference to the original file, not a copy of the file. If the original file is deleted, the information will be lost.
To create a hard link of the file /export/home/fred/stuff to /var/tmp/thing, use:

ln /export/home/fred/stuff /var/tmp/thing

The syntax for creating a hard link of a directory is the same. To create a hard link of /var/www/html to /var/www/webroot, use:

ln /var/www/html /var/www/webroot

Q. What are the soft/symbolic links in unix ??
Answer: A symbolic link is a pointer to another file or directory. It can be used just like the original file or directory. A symbolic link appears in a long listing (ls -l) with a reference to the original file/directory. A symbolic link, as opposed to a hard link, is required when linking from one filesystem to another and can be used within a filesystem as well.
To create a symbolic link, the syntax of the command is similar to a copy or move command: existing file first, destination file second. For example, to link the directory /export/space/common/archive to /archive for easy access, use:

ln -s /export/space/common/archive /archive

To link the runtime control script /etc/init.d/httpd to /etc/rc2.d/S77httpd, use:

cd /etc/rc2.d
ln -s ../init.d/httpd S77httpd

Q. What is a command to kill the last background job??
Answer: kill $!

Q. Bring a job to foreground by specifying its job number after the percent sign.
Answer: fg %jobnumber
(Note: jobnumber can be obtained by using jobs -l command)

Q. Explain top command.
Answer: top provides an ongoing look at processor activity in real time. It displays a listing of the most CPU-intensive tasks on the system, and can provide an interactive interface for manipulating processes. It can sort the tasks by CPU usage, memory usage and runtime. can be better configured than the standard top from the procps suite. Most features can either be selected by an interactive command or by specifying the feature in the personal or system-wide configuration file.

Q. Write a script which give you those records which are having CPU utilization more than 80%.
Answer: df -k awk 'NR==1{print $1"\t"$5"\t"$6}sub("%","",$5){if($5 >= 80) print $1"\t"$5"%\t"$NF}'

Q. How to pass arguments in an awk script??Answer: Using, cat a.txtawk -v awk_var="$shell_var" '{print $1 "\t" awk_var}'
e.g. #! /usr/bin/sh
var=$*
cat ussd.txt >offpeak_input.txt
cat offpeaknawk -v var1="$var" '{print "\"1-"$1"\"""""\""$2"\"""""1006"""""$3"""0""""-1""""-1""""\""var1" .Account balance is Rs..Bonus minutes.\""}'>>offpeak_input.txt

Saturday, August 24, 2013

Unix Scripts You May Need Day to Day

CleanUp Your Server :

find . -mtime +85 -exec ls -ltr {} \;
find . -mtime +77 -exec rm -rf {} \;
find . -type f -name *.Z* -mtime +30 -exec ls -ltr {} \;
find . -type f -name *.dat.Z* -mtime +30 -exec rm -f {} \;
find . -type f -name *.gz* -mtime +30 -exec ls -ltr {} \;
find . -type f -name *.gz* -mtime +30 -exec rm -f {} \;
find . -type f -name *.dmp* -mtime +30 -exec ls -ltr {} \;
find . -type f -name *.dmp* -mtime +30 -exec rm -f {} \;
find . -type f -name *.dmp -mtime +2 -exec compress {} \;
find . -type f -name *.dat -mtime +2 -exec compress {} \;
find . -mtime +93 -exec ls -ltr {} \;
find . -mtime +93 -exec mv {} ./archive/ \;
find . -mtime +93 -exec rm -rf {} \;


search exact word :
grep -nw '101' autogen_dimensions.xml
8:         <PVAL>101</PVAL>

Print a particular line with line number in Unix :
awk 'NR==8' autogen_dimensions.xml

# substitute "foo" with "bar" ONLY for lines which contain "baz" {selective substitution}
sed '/baz/s/foo/bar/g' sourcefile > target_file

How Do I Add User Jerry To A Secondary Group Called Sales?
Type the following command:
# useradd -G sales -m jerry
# passwd jerry
How Do I Add /bin/ksh as A Shell While Creating A User Account?
Use the following syntax:
useradd -m -s /bin/ksh usernameHow Do I Setup HomeDirectory Path While Creating A User Account?
Use the following syntax:
useradd -m -s /bin/ksh -d /path/to/home/user usernameYou can pass all those options as follows:
# useradd -d /nas/users/v/vivek -m -s /bin/bash -c "Vivek Gite" vivek
# passwd vivek

You can search recursively i.e. read all files under each directory for a string "192.168.1.5"
$ grep -r "192.168.1.5" /etc/

To see the port
netstat -an | grep portno

Key Gen :
ssh-keygen -t rsa

./dos2ux fap_item.txt   > fap_item.txt.bak

bad interpreter issue:
perl -i -pe 's/\r//g' inputfile
dos2unix inputfile

symlink creation:
ln -s mysql-advanced-5.5.15-linux2.6-x86_64 mysql
--> pipe the find command with grep with the help of xargs

sed -e '/[^0-9]$/{N;s/\n//;}' testing.txt > new.txt

uuencode tail_file.txt tail_file.txt | mailx -m -s "test" mail_id

$ echo $VAR_NAME | tr '[:upper:]' '[:lower:]'
$ echo $VAR_NAME | tr '[:lower:]' '[:upper:]'


pbrun mailq
/var/spool/mqueue is empty
                Total requests: 0

Unix Top 10 Files:
find /start/dir -type f -exec du {} \; | sort -rn | head -10

find ./ -type d -name .svn -exec rm -rf {} \;


 sed –i '5,7 d' file.txt

 sed –i '$ d' file.txt

  sed '1 d' file.txt > new_file.txt
$> mv new_file.txt file.txt
 sed –n '4 p' test

 $ ln -s 1.3 latest

 $ ln -nsf 1.2 latest

 find . -perm 644

find . –iname "error"–print ( -i is for ignore )
find . -name"*.tmp" -print | xargs rm–f

find . -maxdepth1 -type f -newer first_file

find . -type f -cmin 15 -prune

find . -size +1000c -exec ls-l {} \;

Always use a c after the number, and specify the size in bytes, otherwise you will get confuse because find -size list files based on size of disk block.
to find files using a range of file sizes, a minus or plus sign can be specified before the number. The minus sign means "less than,"
and the plus sign means "greater than." Suppose if you want to find all the files within a range you can use find command as in below example of find:

find . -size +10000c -size-50000c-print
find . -mtime +10 -size +50000c -execls -l{} \;