Wednesday, June 27, 2012

Basic shell scripting part 3


1. What is Shell Scripting ?
        Shell scripting, in Linux or Unix,  is programming with the shell using which you can automate your tasks. A shell is the command interpreter which is the interface between the User and the kernel. A shell script allows you to submit a set of commands to the kernel in a batch. In addition, the shell itself is very powerful with many properties on its own, be it for string manipulation or some basic programming stuff. 

2. The command "cat file" gives error message "--bash: cat: Command not found". Why?
    It is because the PATH variable is corrupt or not set appropriately. And hence the error because the cat command is not available in the directories present PATH variable.

3. How to find the length of a string  in Linux?

$ x="welcome"
$ echo ${#x}
7
4. What are the different timestamps associated with a file?
  • Modification time:- Refers to the time when the file is last modified.
  • Access time :- The time when the file is last accessed.
  • Changed time :- The time when the attributes of the file are last changed.
5. How to get the list of files alone in a directory in Linux?
$ ls -lrt | grep ^-
6. How to find the last modified file or the newest file in a directory?
$ ls -lrt | grep ^- | awk 'END{print $NF}'
7. How to access the 10th command line argument in a shell script in Linux?
     $1 for 1st argument, $2 for 2nd, etc... For 10th argument, ${10}, for 11th, ${11} and so on.

8. How to find the sum of all numbers in a file in Linux?
$ awk '{x+=$0}END{print x}' file
9. How to delete a file which has some hidden characters in the file name?
       Since the rm command may not be able to delete it, the easiest way to delete a file with some hidden characters in its name is to delete it with the find command using the inode number of the file.

$ ls -li
total 32
9962571 -rw-r--r-- 1 guru users 0 Apr 23 11:35
$ find . -inum 9962571 -exec rm '{}' \;
10. Using the grep command, how can you display or print the entire file contents?
$ grep '.*' file
11. What is the difference between a local variable and environment variable in Linux?
         A local variable is the one in which the scope of the variable is only in the shell in which it is defined. An environment variable has scope in all the shells invoked by the shell in which it is defined.

12. What does the 'execute' permission in a directory stand for?
       Without the execute permission on a directory, the user will not be able to traverse or in other words, do a "cd" to the directory.

13. How to find the total number of arguments in a shell script in Linux?
      The shell special variable, $# ,contains the total number of arguments passed to a shell script.

14.  How to remove the Control-M character from a file in Linux?

$ dos2unix file
15. In which file should a variable be set  in order to make the setting permanent?
      The variable should be set in the profile file to make the setting permanent. The appropriate profile depends on the default shell being set for the user.

16. What is a she-bang line in a shell script?
          She-bang line in a shell script is the first line, if present. It starts with '#!' and followed up with a full path of a shell. The shell specified indicates the shell in which this script will be run. The entry of she-bang is not mandatory, however, if present, should be the first line of the script. With a number of shells available and syntax being specific for a given shell, it is always good to specify the she-bang line in a shell script.

17. A file contains many lines, and each line containing multiple words. How to find out the unique words and the word count of each of the words?
$ cat file
apple orange
banana apple orange
papaya
$ awk '{for(i=1;i<=NF;i++)a[$i]++;}END{for(i in a){print i, a[i];}}' file
banana 1
apple 2
orange 2
papaya 1
18. What is an internal command in Linux?
        Internal commands are also called shell built-in commands. Example: cd,fg. Since these are shell built-in, no process is created while executing these commands, and hence are considered to be much faster.

19. x and y are two variables containing numbers? How to add these 2 numbers?

$ expr $x + $y
20.  How to add a header record to a file in Linux?

$ sed -i '1i HEADER' file
21. How to find the list of files modified in the last 30 mins in Linux?

$ find . -mmin -30
22. How to find the list of files modified in the last 20 days?

$ find . -mtime -20
23. How to find the files modified exactly before 30minutes?

$ find . -mmin 30

24. A string contains a absolute path of a file. How to extract the filename alone from the absolute path in Linux?

$ x="/home/guru/temp/f1.txt"
$ echo $x | sed 's^.*/^^'
25. How to find all the files created after a pre-defined date time, say after 10th April 10AM?
       This can be achieved in 2 steps:
       1. Create a dummy file with the time stamp, 10th April 10AM.
       2. Find all the files created after this dummy file.

$ touch -t 1004101000 file
$ find . -newer file
26. How to print the contents of a file line by line in Linux?

$ while read line
> do
>   echo $line
> done < file
27. The word "Unix" is present in many .txt files which is present across many files and also files present in sub directories. How to get the total count of the word "Unix" from all the .txt files?
$ find  . -name *.txt -exec grep -c Unix '{}' \; | awk '{x+=$0;}END{print x}'
28.  How to get tomorrow's date in Linux?
$ date -d "1 day"
29. How to join all lines in a file using comma?
$ paste -s -d, file
30. How to join all lines in a file without any delimiter?
$ paste -s --delimiter="" file
31. How to join every 2 lines in a file in Linux?
$ sed 'N;s/\n//' file
32. A shell script will ask for 3 inputs. The user will not be physically present to be able to give it manually. How can the script be run without having to manually give the input?
    Put those 3 input values in a file, and make the script to read this file as input. For example:
     Assume the 3 values to be : 3, 10 and 20:
$ cat file
3
10
20
  and assuming the script is hello.sh, run it like:
./hello.sh < file
33.  How to find the total number of a lines in a file in Linux?
$ wc -l file | awk '{print $1}'


34. How to print the first line or the header record in a file?
$ head -1 file

35. How to  replace all occurrences of "Unix" to "Linux" in a file?

$ sed 's/Unix/Linux/g' file
36. How to make the above changes permanent in the file?

$ sed -i 's/Unix/Linux/g' file
37. How to  replace only the first occurrence of "Unix" to "Linux" in a string in Linux?

$ sed 's/Unix/Linux/' file
38. How to  replace only the second occurrence of "Unix" to "Linux" in a string in Linux?

$ sed 's/Unix/Linux/2' file
    In fact, to replace nth occurrence of a string in a file, it is:

$ sed 's/Unix/Linux/n' file  #where n is the nth occurrence

39. How to add leading zeros to every line in a file in Linux?

$ sed 's/^/0000/' file
40. How to add trailing zeros to  every line in a file in Linux?

$ sed 's/$/00/' file

41. How to get yesterday's date in Linux?

$ date -d "1 day ago"
42. I have a file with SQL commands. How can I open a sqlplus session in Linux and run this SQL file?

$ sqlplus guru/unix11@XE @file.txt
    where file.txt is the ASCII file containing the sql instructions.

43. The ps command will disclose the sqlplus connect string if any sqlplus session is ON. How to prevent the sqlplus connect string from appearing in the ps command in Linux?
     While connecting to sqlplus, instead of connecting in the normal way, connect as below:

$ sqlplus /nolog
> connect guru/unix11@XE
44. How to rename a group of files from .txt to .exe in Linux?

for i in *.txt
do
  x=`basename $i .txt`
  mv $i $x.exe
done
45. After logging in to your account in Linux, you did "cd log".  There was no "log" directory under the current directory, still the "cd" command traversed to a log directory under a different location? How it happened?
     It is because the CDPATH variable is set.

46. How to zero pad a number in Linux?
      Say, to zero pad a number to 4 places:

$ x=20
$ printf "%04d\n" $x
46. How to find all the .c and .h files in Linux?

$ find . -name "*.[ch]"
47. How to find the list of all the .c files and display only the file name, instead of the default find output which includes the relative path?

$ find . -name *.c | sed 's^.*/^^' 
48. How to copy a file with the same time stamp as the source file in Linux?

$ cp --preserve=timestamp file1 file2
49. How to copy a file "file1" to "file2" by passing only one argument to cp command?

$ cp file{1,2}
50. What is the difference between the source command and dot(.) command in Linux?
       No difference. Both are used for sourcing a file in Linux. bash, csh and some more shells using source command, whereas ksh uses dot(.) command to source a file.

Basic shell scripting part 2

How do you find out what’s your shell?
Answer: echo $SHELL
What’s the command to find out today’s date?
Answer: date
 What’s the command to find out users on the system?
Answer: who
How do you find out the current directory you’re in?
Answer: pwd
How do you remove a file?
Answer: rm
How do you remove a file?
Answer: rm -rf
How do you find out your own username?
Answer: whoami
How do you send a mail message to somebody?
Answer: mail somebody@techinterviews.com -s ‘Your subject’ -c ‘ cc@techinterviews.com
How do you count words, lines and characters in a file?
Answer: wc
How do you search for a string inside a given file?
Answer: grep string filename
How do you search for a string inside a directory?
Answer: grep string *
How do you search for a string in a directory with the subdirectories recursed?
Answer: grep -r string *
What are PIDs?
Answer: They are process IDs given to processes. A PID can vary from 0 to 65535.
How do you list currently running process?
Answer: ps
How do you stop a process?
Answer: kill pid
How do you find out about all running processes?
Answer: ps -ag
How do you stop all the processes, except the shell window?
Answer: kill 0
How do you fire a process in the background?
Answer: ./process-name &
How do you refer to the arguments passed to a shell script?
Answer: $1, $2 and so on. $0 is your script name.
What’s the conditional statement in shell scripting?
Answer: if {condition} then … fi
How do you do number comparison in shell scripts?
Answer: -eq, -ne, -lt, -le, -gt, -ge
How do you test for file properties in shell scripts?
Answer: -s filename tells you if the file is not empty, -f filename tells you whether the argument is a file, and not a directory, -d filename tests if the argument is a directory, and not a file, -w filename tests for writeability, -r filename tests for readability, -x filename tests for executability
How do you do Boolean logic operators in shell scripting?
Answer: ! tests for logical not, -a tests for logical and, and -o tests for logical or.
How do you find out the number of arguments passed to the shell script?
Answer: $#
What’s a way to do multilevel if-else’s in shell scripting?
Answer: if {condition} then {statement} elif {condition} {statement} fi
How do you write a for loop in shell?
Answer: for {variable name} in {list} do {statement} done
How do you write a while loop in shell?
Answer: while {condition} do {statement} done
How does a case statement look in shell scripts?
Answer: case {variable} in {possible-value-1}) {statement};; {possible-value-2}) {statement};; esac
How do you read keyboard input in shell scripts?
Answer: read {variable-name}
How do you define a function in a shell script?
Answer: function-name() { #some code here return }
How does getopts command work?
Answer: The parameters to your script can be passed as -n 15 -x 20. Inside the script, you can iterate through the getopts array as while getopts n:x option, and the variable $option contains the value of the entered option.

What’s a way to do multilevel if-else’s in shell scripting?
Answer: if then elif fi
How do you write a for loop in shell?
Answer: for in do done
How do you write a while loop in shell?
Answer: while do done
How does a case statement look in shell scripts?
Answer: case in ) ;; ) ;; esac
How do you define a function in a shell script?
Answer: function-name()
How do you find out about all running processes?
Answer: ps -ag
How do you stop a process?
Answer: kill pid
How do you remove a file?
Answer: rm
How do you remove recursively?
Answer: rm -rf
What are PIDs?
Answer: They are process IDs given to processes. A PID can vary...
How do you list currently running process?
Answer: ps
What is $$?
What is a named pipe?
What does || mean?
What does && mean?
What is a loop?
What does while do?
What is a function? 
What are the different kinds of loops available in shell script?
for if while case

What does $# stand for?
$# returns the number of parameters that are passed to a shell script
$? returns the exit code of the last executed command (0 : Successful, 1 or other: Failed)
What does $? return?
Will return the status of the command which is executed lastly.
0 > Success
2 > Error
How do u open a read only file in Unix?
"vi -R filename"
What is the difference between a shell variable that is exported and the one that is not exported?
If you have a string "one two three", Which shell command would you use to extract the strings?
How do you schedule a command to run at 4:00 every morning?
How will you list only the empty lines in a file (using grep)?
grep ^$ filename.txt
When you login to a c shell, which script would be run first? (before the terminal is ready for the user)
first /etc/.login script is run & after that
~/.login is run & then ~/.cshrc is run.
How would you get the character positions 10-20 from a text file?
cat filename.txt | cut -c 10-20
How would you print just the 25th line in a file (smallest possible script please)?
tail -n +25Â | head -1 OR
head -n 25 | tail -1
How would you replace the n character in a file with some xyz?
sed 's/n/xyz/g' filename > new_filename

Basic shell scripting part 1


What is $*?
Its mainly used for showing up all params. This show all parameter values passed in shell script

What does $# stand for?
# will return the number of parameters that are passed as the command-line arguments.

What does $? Return?
$? will return exit status of command .0 if command gets successfully executed ,non-zero if command failed.

What are Different types of shells?
sh : the oldest shell 
csh : C shell 
ksh : Korn Shell 
bash : bourne again shell 


How do you read arguments in a shell program – $1, $2?
Shell script accepts parameters in following format… 
$1 would be the first command line argument, $2 the second, and so on 
$0 is the name of the script or function
If your script has more than 9 params then accept in following way… 
${12} : 12th param 
${18} : 18th param

What are the different kinds of loops available in shell script?
for, if, while, case

What is the difference between a shell variable that is exported and the one that is not exported?
The Shell variable which is exported would available to all the programs outside the Shell also. And the shell variable which is not exported, would available for that shell or for the shell program only, in which the variable is declared.

Export LANG=C 
will make the variable LANG the global variable, put it into the global environment. All other processes can use it. 
 
LANG=C 
will change the value only in the current script. 

  
If you have a string “one two three”, which shell command would you use to extract the strings?
echo $string | cut -d” ” -f1 
echo $string | cut -d” ” -f2 
echo $string | cut -d” ” -f3 

How will you list only the empty lines in a file (using grep)?
grep “^$” filename.txt

How would you get the character positions 10-20 from a text file?
cat filename.txt | cut -c 10-20
or
cut -c10-20 <filename.txt>

How would you replace the n character in a file with some xyz?
sed ‘s/n/xyz/g’ filename > new_filename

We can replace n characters by using the following command:
1,$s/./xyz/g
where 1 shows that the search string will start searching patterns from first line of the file.
           ‘.’ for any character.
            g for global replacemet.


What is the difference between a ‘thread’ and a ‘process’?
A process is a collection of virtual memory space, code, data, and system resources. A thread is code that is to be serially executed within a process. A processor executes threads, not processes, so each application has at least one process, and a process always has at least one thread of execution, known as the primary thread. A process can have multiple threads in addition to the primary thread
Thread – is stream of executable code within process. They are light weight process. All thread with in a process  share process instruction,code & data segment,open file descriptor,signal handler,userID and GroupID. Thread has its own set of register including program counter,stack pointer 

What is this line in the shell script do ?#!/bin/ksh?
To invoke the shell indirectly this line is added as the first line in the file.This particular line invokes korn shell


What is use of “cut” command? Give some examples.
Cut – Utility used to cut/Strip out the required data/text from the source.
Cut can be used in three modes,
  Stripping by Character
     cut -c 1-3
  Striping by Byte length
     cut -b -1-72
Stripping by delimiter and fields.
     cut -d “|” -f1
Where   
-d “|” -> Delimiter used in input text to separate columns
-f1 -> Field/Column number  
While processing huge input files, Cut’s performance is far better than awk

Friday, June 22, 2012

Unix questions with answers


This article attempts to refresh your Unix skills in the form of a question/answer based Unix tutorial on Unix command lines. The commands discussed here are particulary useful for the developers working in the middle-tier (e.g. ETL) systems, where they may need to interact with several *nx source systems for data retrieval.

How to print/display the first line of a file?

There are many ways to do this. However the easiest way to display the first line of a file is using the [head] command.
$> head -1 file.txt
No prize in guessing that if you specify [head -2] then it would print first 2 records of the file.
Another way can be by using [sed] command. [Sed] is a very powerful text editor which can be used for various text manipulation purposes like this.
$> sed '2,$ d' file.txt
How does the above command work? The 'd' parameter basically tells [sed] to delete all the records from display from line 2 to last line of the file (last line is represented by $ symbol). Of course it does not actually delete those lines from the file, it just does not display those lines in standard output screen. So you only see the remaining line which is the 1st line.

How to print/display the last line of a file?

The easiest way is to use the [tail] command.
$> tail -1 file.txt
If you want to do it using [sed] command, here is what you should write:
$> sed -n '$ p' test
From our previous answer, we already know that '$' stands for the last line of the file. So '$ p' basically prints (p for print) the last line in standard output screen. '-n' switch takes [sed] to silent mode so that [sed] does not print anything else in the output.

How to display n-th line of a file?

The easiest way to do it will be by using [sed] I guess. Based on what we already know about [sed] from our previous examples, we can quickly deduce this command:
$> sed –n '<n> p' file.txt
You need to replace <n> with the actual line number. So if you want to print the 4th line, the command will be
$> sed –n '4 p' test
Of course you can do it by using [head] and [tail] command as well like below:
$> head -<n> file.txt | tail -1
You need to replace <n> with the actual line number. So if you want to print the 4th line, the command will be
$> head -4 file.txt | tail -1

How to remove the first line / header from a file?

We already know how [sed] can be used to delete a certain line from the output – by using the'd' switch. So if we want to delete the first line the command should be:
$> sed '1 d' file.txt
But the issue with the above command is, it just prints out all the lines except the first line of the file on the standard output. It does not really change the file in-place. So if you want to delete the first line from the file itself, you have two options.
Either you can redirect the output of the file to some other file and then rename it back to original file like below:
$> sed '1 d' file.txt > new_file.txt
$> mv new_file.txt file.txt
Or, you can use an inbuilt [sed] switch '–i' which changes the file in-place. See below:
$> sed –i '1 d' file.txt

How to remove the last line/ trailer from a file in Unix script?

Always remember that [sed] switch '$' refers to the last line. So using this knowledge we can deduce the below command:
$> sed –i '$ d' file.txt

How to remove certain lines from a file in Unix?

If you want to remove line <m> to line <n> from a given file, you can accomplish the task in the similar method shown above. Here is an example:
$> sed –i '5,7 d' file.txt
The above command will delete line 5 to line 7 from the file file.txt

How to remove the last n-th line from a file?

This is bit tricky. Suppose your file contains 100 lines and you want to remove the last 5 lines. Now if you know how many lines are there in the file, then you can simply use the above shown method and can remove all the lines from 96 to 100 like below:
$> sed –i '96,100 d' file.txt   # alternative to command [head -95 file.txt] 
But not always you will know the number of lines present in the file (the file may be generated dynamically, etc.) In that case there are many different ways to solve the problem. There are some ways which are quite complex and fancy. But let's first do it in a way that we can understand easily and remember easily. Here is how it goes:
$> tt=`wc -l file.txt | cut -f1 -d' '`;sed –i "`expr $tt - 4`,$tt d" test
As you can see there are two commands. The first one (before the semi-colon) calculates the total number of lines present in the file and stores it in a variable called “tt”. The second command (after the semi-colon), uses the variable and works in the exact way as shows in the previous example.

How to check the length of any line in a file?

We already know how to print one line from a file which is this:
$> sed –n '<n> p' file.txt
Where <n> is to be replaced by the actual line number that you want to print. Now once you know it, it is easy to print out the length of this line by using [wc] command with '-c' switch.
$> sed –n '35 p' file.txt | wc –c
The above command will print the length of 35th line in the file.txt.

How to get the nth word of a line in Unix?

Assuming the words in the line are separated by space, we can use the [cut] command. [cut] is a very powerful and useful command and it's real easy. All you have to do to get the n-th word from the line is issue the following command:
cut –f<n> -d' '
'-d' switch tells [cut] about what is the delimiter (or separator) in the file, which is space ' ' in this case. If the separator was comma, we could have written -d',' then. So, suppose I want find the 4th word from the below string: “A quick brown fox jumped over the lazy cat”, we will do something like this:
$> echo “A quick brown fox jumped over the lazy cat” | cut –f4 –d' '
And it will print “fox”

How to reverse a string in unix?

Pretty easy. Use the [rev] command.
$> echo "unix" | rev
xinu

How to get the last word from a line in Unix file?

We will make use of two commands that we learnt above to solve this. The commands are [rev] and [cut]. Here we go.
Let's imagine the line is: “C for Cat”. We need “Cat”. First we reverse the line. We get “taC rof C”. Then we cut the first word, we get 'taC'. And then we reverse it again.
$>echo "C for Cat" | rev | cut -f1 -d' ' | rev
Cat

How to get the n-th field from a Unix command output?

We know we can do it by [cut]. Like below command extracts the first field from the output of [wc –c] command
$>wc -c file.txt | cut -d' ' -f1
109
But I want to introduce one more command to do this here. That is by using [awk] command. [awk] is a very powerful command for text pattern scanning and processing. Here we will see how we may use of [awk] to extract the first field (or first column) from the output of another command. Like above suppose I want to print the first column of the [wc –c] output. Here is how it goes like this:
$>wc -c file.txt | awk ' '' {print $1}'
109 
The basic syntax of [awk] is like this:
awk 'pattern space''{action space}'
The pattern space can be left blank or omitted, like below:
$>wc -c file.txt | awk '{print $1}'
109
In the action space, we have asked [awk] to take the action of printing the first column ($1). More on [awk] later.

How to replace the n-th line in a file with a new line in Unix?

This can be done in two steps. The first step is to remove the n-th line. And the second step is to insert a new line in n-th line position. Here we go.
Step 1: remove the n-th line
$>sed -i'' '10 d' file.txt       # d stands for delete
Step 2: insert a new line at n-th line position
$>sed -i'' '10 i This is the new line' file.txt     # i stands for insert

How to show the non-printable characters in a file?

Open the file in VI editor. Go to VI command mode by pressing [Escape] and then [:]. Then type [set list]. This will show you all the non-printable characters, e.g. Ctrl-M characters (^M) etc., in the file.

How to zip a file in Linux?

Use inbuilt [zip] command in Linux

How to unzip a file in Linux?

Use inbuilt [unzip] command in Linux.
$> unzip –j file.zip

How to test if a zip file is corrupted in Linux?

Use “-t” switch with the inbuilt [unzip] command
$> unzip –t file.zip

How to check if a file is zipped in Unix?

In order to know the file type of a particular file use the [file] command like below:
$> file file.txt
file.txt: ASCII text
If you want to know the technical MIME type of the file, use “-i” switch.
$>file -i file.txt
file.txt: text/plain; charset=us-ascii
If the file is zipped, following will be the result
$> file –i file.zip
file.zip: application/x-zip

How to connect to Oracle database from within shell script?

You will be using the same [sqlplus] command to connect to database that you use normally even outside the shell script. To understand this, let's take an example. In this example, we will connect to database, fire a query and get the output printed from the unix shell. Ok? Here we go –
$>res=`sqlplus -s username/password@database_name <<EOF
SET HEAD OFF;
select count(*) from dual;
EXIT;
EOF`
$> echo $res
1
If you connect to database in this method, the advantage is, you will be able to pass Unix side shell variables value to the database. See below example
$>res=`sqlplus -s username/password@database_name  <<EOF
SET HEAD OFF;
select count(*) from student_table t where t.last_name=$1;
EXIT;
EOF`
$> echo $res
12

How to execute a database stored procedure from Shell script?

$> SqlReturnMsg=`sqlplus -s username/password@database<<EOF
BEGIN
Proc_Your_Procedure(… your-input-parameters …); 
END;
/
EXIT;
EOF`
$> echo $SqlReturnMsg

How to check the command line arguments in a UNIX command in Shell Script?

In a bash shell, you can access the command line arguments using $0, $1, $2, … variables, where $0 prints the command name, $1 prints the first input parameter of the command, $2 the second input parameter of the command and so on.

How to fail a shell script programmatically?

Just put an [exit] command in the shell script with return value other than 0. this is because the exit codes of successful Unix programs is zero. So, suppose if you write
exit -1
inside your program, then your program will thrown an error and exit immediately.

How to list down file/folder lists alphabetically?

Normally [ls –lt] command lists down file/folder list sorted by modified time. If you want to list then alphabetically, then you should simply specify: [ls –l]

How to check if the last command was successful in Unix?

To check the status of last executed command in UNIX, you can check the value of an inbuilt bash variable [$?]. See the below example:
$> echo $?

How to check if a file is present in a particular directory in Unix?

Using command, we can do it in many ways. Based on what we have learnt so far, we can make use of [ls] and [$?] command to do this. See below:
$> ls –l file.txt; echo $?
If the file exists, the [ls] command will be successful. Hence [echo $?] will print 0. If the file does not exist, then [ls] command will fail and hence [echo $?] will print 1.

How to check all the running processes in Unix?

The standard command to see this is [ps]. But [ps] only shows you the snapshot of the processes at that instance. If you need to monitor the processes for a certain period of time and need to refresh the results in each interval, consider using the [top] command.
$> ps –ef
If you wish to see the % of memory usage and CPU usage, then consider the below switches
$> ps aux
If you wish to use this command inside some shell script, or if you want to customize the output of [ps] command, you may use “-o” switch like below. By using “-o” switch, you can specify the columns that you want [ps] to print out.
$>ps -e -o stime,user,pid,args,%mem,%cpu

How to tell if my process is running in Unix?

You can list down all the running processes using [ps] command. Then you can “grep” your user name or process name to see if the process is running. See below:
$>ps -e -o stime,user,pid,args,%mem,%cpu | grep "opera"
14:53 opera 29904 sleep 60                     0.0  0.0
14:54 opera 31536 ps -e -o stime,user,pid,arg  0.0  0.0
14:54 opera 31538 grep opera                0.0  0.0

How to get the CPU and Memory details in Linux server?

In Linux based systems, you can easily access the CPU and memory details from the /proc/cpuinfo and /proc/meminfo, like this:
$>cat /proc/meminfo
$>cat /proc/cpuinfo
Just try the above commands in your system to see how it works

Beginners UNIX Interview Questions Answers

1. Write command to list all the links from a directory?
In this UNIX command interview questions interviewer is generally checking whether user knows basic use of "ls" "grep" and regular expression etc
You can write command like:
ls -lrt | grep "^l"


2. Create a read-only file in your home directory?
This is a simple UNIX command interview questions where you need to create a file and change its parameter to read-only by using chmod command you can also change your umask to create read only file.
touch file
chmod 400 file
3. How will you find which operating system your system is running on in UNIX?
By using command "uname -a" in UNIX

4. How will you run a process in background? How will you bring that into foreground and how will you kill that process?
For running a process in background use "&" in command line. For bringing it back in foreground use command "fg jobid" and for getting job id you use command "jobs", for killing that process find PID and use kill -9 PID command. This is indeed a good Unix Command interview questions because many of programmer not familiar with background process in UNIX.

5. How do you know if a remote host is alive or not?
You can check these by using either ping or telnet command in UNIX. This question is most asked in various Unix command Interview because its most basic networking test anybody wants to do it.


6. How do you see command line history in UNIX?
Very useful indeed, use history command along with grep command in unix to find any relevant command you have already executed. Purpose of this Unix Command Interview Questions is probably to check how familiar candidate is from available tools in UNIX operation system.

7. How do you copy file from one host to other?
Many options but you can say by using "scp" command. You can also use rsync command to answer this UNIX interview question or even sftp would be ok.

8. How do you find which process is taking how much CPU?
By using "top" command in UNIX, there could be multiple follow-up UNIX command interview questions based upon response of this because “TOP” command has various interactive options to sort result based upon various parameter.

9. How do you check how much space left in current drive ?
By using "df" command in UNIX. For example "df -h ." will list how full your current drive is. This is part of anyone day to day activity so I think this Unix Interview question will be to check anyone who claims to working in UNIX but not really working on it.

10. What is the difference between Swapping and Paging?
Swapping:
Whole process is moved from the swap device to the main memory for execution. Process size must be less than or equal to the available main memory. It is easier to implementation and overhead to the system. Swapping systems does not handle the memory more flexibly as compared to the paging systems.
Paging:
Only the required memory pages are moved to main memory from the swap device for execution. Process size does not matter. Gives the concept of the virtual memory. It provides greater flexibility in mapping the virtual address space into the physical memory of the machine. Allows more number of processes to fit in the main memory simultaneously. Allows the greater process size than the available physical memory. Demand paging systems handle the memory more flexibly.

Intermediate UNIX Interview Questions Answers

1. What is difference between ps -ef and ps -auxwww?
This is indeed a good Unix Interview Command Question and I have faced this issue while ago where one culprit process was not visible by execute ps –ef command and we are wondering which process is holding the file.
ps -ef will omit process with very long command line while ps -auxwww will list those process as well.

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

3. What is difference between HardLink and SoftLink in UNIX?
I have discussed this Unix Command Interview questions  in my blog post difference between Soft link and Hard link in Unix

4. What is Zombie process in UNIX? How do you find Zombie process in UNIX?
When a program forks and the child finishes before the parent, the kernel still keeps some of its information about the child in case the parent might need it - for example, the parent may need to check the child's exit status. To be able to get this information, the parent calls 'wait()'; In the interval between the child terminating and the parent calling 'wait()', the child is said to be a 'zombie' (If you do 'ps', the child will have a 'Z' in its status field to indicate this.)
Zombie : The process is dead but have not been removed from the process table.

5. What is "chmod" command? What do you understand by this line “r-- -w- --x?

6. There is a file some where in your system which contains word "UnixCommandInterviewQuestions” How will find that file in Unix?
By using find command in UNIX for details see here 10 example of using find command in Unix

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

8. How do you set environment variable which will be accessible form sub shell?
By using export   for example export count=1 will be available on all sub shell.

9. How do you check if a particular process is listening on a particular port on remote host?
By using telnet command for example “telnet hostname port”, if it able to successfully connect then some process is listening on that port. To read more about telnet read networking command in UNIX

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.


Advanced UNIX Questions and Answers

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.

2. How do you find which remote hosts are connecting to your host on a particular port say 10123?
By using netstat command execute netstat -a | grep "port" and it will list the entire host which is connected to this host on port 10123.

3. What is nohup in UNIX?

4. What is ephemeral port in UNIX?
Ephemeral ports are port used by Operating system for client sockets. There is a specific range on which OS can open any port specified by ephemeral port range.

5. If one process is inserting data into your MySQL database? How will you check how many rows inserted into every second?
Purpose of this Unix Command Interview is asking about "watch" command in UNIX which is repeatedly execute command provided with specified delay.

6. There is a file Unix_Test.txt which contains words Unix, how will you replace all Unix to UNIX?
You can answer this Unix Command Interview question by using SED command in UNIX for example you can execute sed s/Unix/UNIX/g fileName.

7. You have a tab separated file which contains Name, Address and Phone Number, list down all Phone Number without there name and Addresses?
To answer this Unix Command Interview question you can either you AWK or CUT command here. CUT use tab as default separator so you can use
cut -f3 filename.

8. Your application home directory is full? How will you find which directory is taking how much space?
By using disk usage (DU) command in Unix for example du –sh . | grep G  will list down all the directory which has GIGS in Size.

9. How do you find for how many days your Server is up?
By using uptime command in UNIX

10. You have an IP address in your network how will you find hostname and vice versa?
This is a standard UNIX command interview question asked by everybody and I guess everybody knows its answer as well. By using nslookup command in UNIX, you can read more about Convert IP Address to hostname in Unix here.




Difference between the fork() and vfork() system call?
Answers:
During the fork() system call the Kernel makes a copy of the parent process?s address space and attaches it to the child process.
But the vfork() system call do not makes any copy of the parent?s address space, so it is faster than the fork() system call. The child process as a result of the vfork() system call executes exec() system call. The child process from vfork() system call executes in the parent?s address space (this can overwrite the parent?s data and stack ) which suspends the parent process until the child process exits.

fork: Both the parent and child share all of the page tables until any one of them does a write. Then paging will create private page copy of the dirty page for the purpose of modifying process. This process is done on demand.

vfork - In this system call until child exits or execs, the parent will be suspended. The memory and stack are shared by the child.