Time To Earn

Saturday, August 24, 2013

Unix Pointers

How will you find the files that are accessed before 10 minutes?
find / -amin -10

find ~ -mmin -10

"history | awk '{print $2}' | sort | uniq -c | sort -nr | head -5".

What UNIX command do you use to find out the open ports in a system?
 lsof -i tcp | grep LISTEN | awk '{print $(NF -1)}' | sort | uniq

Explain kill() and its possible return values?

There are four possible results from this call:‘kill()’ returns 0. This implies that a process exists with the given pid, and the system would allow you to send signals to it. It is system-dependent whether the process could be a zombie.‘kill()’ returns -1, ‘errno == esrch’ EIther no process exists with...

As we all know kill command sends a signal to the kernel. This signals are of 64 types. To list all these signals you can use kill -l. Signal no 1 -31 can be invoked by user, whereas, signal no 34-64 ...

How to terminate a process which is running and the specialty on command kill 0?
With the help of kill command we can terminate the process.Syntax: kill pidkill 0 - kills all processes in your system except the login shell.

kill -0 : All processes in the current process group are signaled.
Kill -0 commnd is use to sends signal to all process and group of process.

kill 0  will delete all the processes

Difference between kill and kill -9
kill      ---> generates SIGTERM signal requesting process to terminate
kill -9 ---> generates SIGKILL signal for process to terminate immediately

Kill -9 is force termination of a job means processes which refuse to die

2. How do you find how many cpu are in your system and there details?
By looking into file /etc/cpuinfo for example you can use below command:
cat /proc/cpuinfo

7. In a file word UNIX is appearing many times? How will you count number?
grep -c "Unix" filename

10. How do you find whether your system is 32 bit or 64 bit ?
Either by using "uname -a" command or by using "arch" command.

1. How do you find which processes are using a particular file?
By using lsof command in UNIX. It wills list down PID of all the process which is using a particular file

5. If one process is inserting data into your MySQL database? How will you check how many rows inserted into every second?

1. How to display the 10th line of a file?
head -10 filename | tail -1
2. How to remove the header from a file?
sed -i '1 d' filename
3. How to remove the footer from a file?
sed -i '$ d' filename
4. Write a command to find the length of a line in a file?
The below command can be used to get a line from a file.
sed –n '<n> p' filename
We will see how to find the length of 10th line in a file
sed -n '10 p' filename|wc -c
5. How to get the nth word of a line in Unix?
cut –f<n> -d' '
6. How to reverse a string in unix?
echo "java" | rev
7. How to get the last word from a line in Unix file?
echo "unix is good" | rev | cut -f1 -d' ' | rev
8. How to replace the n-th line in a file with a new line in Unix?
sed -i'' '10 d' filename       # d stands for delete
sed -i'' '10 i new inserted line' filename     # i stands for insert
9. How to check if the last command was successful in Unix?
echo $?
10. Write command to list all the links from a directory?
ls -lrt | grep "^l"
11. How will you find which operating system your system is running on in UNIX?
uname -a
12. Create a read-only file in your home directory?
touch file; chmod 400 file
13. How do you see command line history in UNIX?
The 'history' command can be used to get the list of commands that we are executed.
14. How to display the first 20 lines of a file?
By default, the head command displays the first 10 lines from a file. If we change the option of head, then we can display as many lines as we want.
head -20 filename
An alternative solution is using the sed command
sed '21,$ d' filename
The d option here deletes the lines from 21 to the end of the file
15. Write a command to print the last line of a file?
The tail command can be used to display the last lines from a file.
tail -1 filename
Alternative solutions are:
sed -n '$ p' filename
awk 'END{print $0}' filename

For larger data sets where sorting may not be desirable, you can also use the following perl script:
./yourscript.ksh | perl -ne 'if (!defined $x{$_}) { print $_; $x{$_} = 1; }'
This basically just remembers every line output so that it doesn't output it again.
It has the advantage over the "sort | uniq" solution in that there's no sorting required up front.

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/
scp from detination
scp acomsl12:/tmp/Cheetah_Publish_Report_29-Feb-2012.log /home/anurverm
scp f

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

$ echo $VAR_NAME | tr '[:upper:]' '[:lower:]'
$ echo $VAR_NAME | tr '[:lower:]' '[:upper:]'
\
/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 {} \;

split -l 1000 -a 3 install_ctl_file.ksh  new-install_ctl_file.ksh

1.  if u want to select the arguement by number i.e. if u know that the element is placed  in nth row and nth column.
cat sam | awk -F" " '{print $1}' | sed -n '4p'
this lil script will take the 4th row element from 1st column. filename is "sam" here.
2.  if u know the pattern which u wanna take out, then:
cat sam | awk -F" " '{print $1}' | sed -n '/manshul/p'
u can subtitute manshul wid the pattern u require
Now if u wanna execute this in ur script,then
cat sam | awk -F" " '{print $1}' | sed -n '4p' | xargs -t ./zz
here "zz" is the script name u wanna execute after picking the variable.

to find a string in all files and in all subdirectories......
find   . -type f  -exec  grep "Albert" /dev/null {} \;
find . -type f -exec grep "199.106.64.131" /dev/null {} \;
to find the biggest 10 files under a user:
du -ha /home/user30 | sort -n -r | head -n 10

mailx -s "Subject" mailid

EXAMPLE CRON FILE
     # use /bin/sh to run commands, overriding the default set by cron
     SHELL=/bin/sh
     #
     # mail any output to `paul', no matter whose crontab this is
     MAILTO=paul
     #
     # run five minutes after midnight, every day
     5 0 * * *      $HOME/bin/daily.job >> $HOME/tmp/out 2>&1
     #
     # run at 2:15pm on the first of every month -- output mailed to paul
     15 14 1 * *     $HOME/bin/monthly
     #
     # run at 10 pm on weekdays, annoy Joe
     0 22 * * 1-5    mail -s "It's 10pm" joe%Joe,%%Where are your kids?%
     23 0-23/2 * * * echo "run 23 minutes after midn, 2am, 4am ..., everyday"
     5 4 * * sun     echo "run at 5 after 4 every sunday"

ps -efx | grep perfuser

2. Last 3 words in line print
date=`echo $line | awk '{print $(NF-2), $(NF-1), $NF}'`
3. Send a varibale through mail:
echo | mailx -s "this is current memory status $temp1 mail_id

lsof | grep /tmp

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/
scp from detination

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

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

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

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

split -l 1000 -a 3 install_ctl_file.ksh  new-install_ctl_file.ksh

1.  if u want to select the arguement by number i.e. if u know that the element is placed  in nth row and nth column.
cat sam | awk -F" " '{print $1}' | sed -n '4p'
this lil script will take the 4th row element from 1st column. filename is "sam" here.
2.  if u know the pattern which u wanna take out, then:
cat sam | awk -F" " '{print $1}' | sed -n '/manshul/p'
u can subtitute manshul wid the pattern u require
Now if u wanna execute this in ur script,then
cat sam | awk -F" " '{print $1}' | sed -n '4p' | xargs -t ./zz
here "zz" is the script name u wanna execute after picking the variable.

to find a string in all files and in all subdirectories......
find   . -type f  -exec  grep "Albert" /dev/null {} \;
find . -type f -exec grep "199.106.64.131" /dev/null {} \;
to find the biggest 10 files under a user:
du -ha /home/user30 | sort -n -r | head -n 10

mailx -s "Subject" mailid

EXAMPLE CRON FILE
     # use /bin/sh to run commands, overriding the default set by cron
     SHELL=/bin/sh
     #
     # mail any output to `paul', no matter whose crontab this is
     MAILTO=paul
     #
     # run five minutes after midnight, every day
     5 0 * * *      $HOME/bin/daily.job >> $HOME/tmp/out 2>&1
     #
     # run at 2:15pm on the first of every month -- output mailed to paul
     15 14 1 * *     $HOME/bin/monthly
     #
     # run at 10 pm on weekdays, annoy Joe
     0 22 * * 1-5    mail -s "It's 10pm" joe%Joe,%%Where are your kids?%
     23 0-23/2 * * * echo "run 23 minutes after midn, 2am, 4am ..., everyday"
     5 4 * * sun     echo "run at 5 after 4 every sunday"

ps -efx | grep perfuser

2. Last 3 words in line print
date=`echo $line | awk '{print $(NF-2), $(NF-1), $NF}'`

# 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

No comments:

Post a Comment