Unix Interview Questions | Unix Scripting Interview Questions

Unix Scripting Interview Questions :

In my previous articles i have explained about different UNIX tutorials  with real life examples.I would like to give the set of different Unix Scripting Interview Questions which is useful for freshers as well as experienced UNIX developers or support engineers.These Unix Scripting Interview Questions are important to measure the knowledge of the person theoretically as well as practically.Unix operating system is one of the most used and secure operating system which is using in development and deployment of lot of applications.Because of the Security of the UNIX operating system this is used in 90 percent of applications as a server operating system.Unix is Multi-user and Multitasking operating system which is used mainly as server in most of the applications.

Following are different Unix Scripting Interview Questions with Answers :

1.What do you know about Unix operating system? ( 100% asked Unix Scripting Interview Questions )

Answer:

nix operating system is one of the most used and secure operating system which is using in development and deployment of lot of applications.Because of the Security of the UNIX operating system this is used in 90 percent of applications as a server operating system.Unix is Multi-user and Multitasking operating system which is used mainly as server in most of the applications.

“Unix is simple,stable,multi-user,multitasking operating system which is used for Servers,Desktops and laptops..”

2.What is mean by Shell in Unix Operating System? ( 100% asked Unix Scripting Interview Questions )

Answer :

Shell is an interface between the user and the kernel. Even though there can be only one kernel; a system can have many shell running simultaneously. So, whenever a user enters a command through the keyboard, the shell communicates with the kernel to execute it and then display the output to the user.

3.Which are Different Shells in Unix? ( 100% asked Unix Scripting Interview Questions )

Answer:

There are following different Shells in unix operating system:

  1. csh – This is also called as C-shell
  2. ksh – This is called as Korn shell. This is developed by David Korn.
  3. Bsh- Bourne shell. This is developed by Stephen Bourne.
  4. Bash- This is mostly used shell in Unix operating system. Bourne Again shell is most used for shell scripting in Unix operating system.

Unix Scripting Interview Questions

4.If user wants to display current user information which command is useful?

Answer :

Whoami :

This command displays current user information.

Example:

$Whoami

Output : Amit

5.Which command is useful to show present working directory?

Answer:

PWD :

PWD is most commonly used command which is used to show the present working directory of the user.

Example:

$PWD

Output:\home\Amit

6. What is the difference between soft and hard links?

Answer:

Soft links are link to the file name and can reside on different filesytem as well; however hard links are link to the inode of the file and have to be on the same filesytem as that of the file. Deleting the original file makes the soft link inactive (broken link) but does not affect the hard link (Hard link will still access a copy of the file).

7.How to display Calendar in Unix? Explain with different examples.

Answer:

Cal command is used to display the calendar in Unix. Following are its different examples.

Cal :

Cal command displays the current month calendar.

Cal 2000 :

Displays year 2000 calendar.

Cal 01 2000:

Displays the 2000 year calendar for month of January.

8.How to switch from one user account to other user account in Unix?

Answer:

The SU (Super user command) is used to switch from one user account to another user account.

Example :

$SU Rohit

the  above command switches account named “Amit” to account named “Rohit”.

9.What UNIX operating system command would you use to display the shell’s environment variables?

Answer:

Running the “env” command will display the shell environment variables.

Details: 
Sample env command output:

$ env
HISTFILE=/home/lfl/.history
PATH=/usr/local/bin:/bin:/usr/bin:/usr/X11R6/bin
SHELL=/bin/ksh
HOSTNAME=livefirelabs.com
USER=lfl
MAIL=/var/spool/mail/lfl
HOME=/home/lfl
HISTSIZE=1000

It would also be good to understand the purpose of the common shell environment variables that are listed in the env command output.

10. What is the significance of $#?

Answer:

$# shows the count of the arguments passed to the script.

11.What are different ways to create a file in Unix?( 100% asked Unix Scripting Interview Questions )

Answer:

There are following ways to create file in Unix:

1.Using CAT command

2.Using touch command

3.Using Echo and printf command

4.Using Different text editors-Vi,e mac

12.What is the difference between $* and $@?

Answer:

$@ :  $@ treats each quoted arguments as separate arguments

 $*:   $* will consider the entire set of positional parameters as a single string.

13.What needs to be done before you can run a shell script from the command line prompt?

Answer :

You need to make the shell script executable using the UNIX chmod command.

Details: 
This chmod command makes the shell script file “example1” executable for the user (owner) only:

$ chmod u+x example1

this syntax makes it executable for all (everyone):

$ chmod a+x example1

You can optionally use octal notation to set UNIX permissions using the chmod command (e.g., $ chmod 755 example1). This topic is beyond the scope of this article, but you can find more information by entering “Unix file permissions chmod numeric notation” in your favorite search engine.

14.What are the main features of UNIX?( 100% asked Unix Scripting Interview Questions )

Answer:

  • Machine independent
  • Portability
  • Multi-user operations
  • Unix Shells
  • Hierarchical file system
  • Pipes and filters
  • Background processors
  • Utilities
  • Development tools.

15.What are the responsibilities of a shell?

Answer:

  • Program Execution
  • Input/output redirection
  • Filename and variable substitution
  • Pipeline hookup
  • Environment control
  • Integrated programming language

16.Explain how to create file using CAT command in Unix?

Answer:

User can create a new file using ‘Cat’ command in unix.Using shell prompt directly user can create a file.Using ‘Cat’ command user will able to open a specific file also.If user wants to process the file and append data to the specific file use ‘Cat’ command.

To create new file:

Syntax:

$ Cat >File_Name

Example:

$ Cat >Amit_new.txt

This is my first unix file

Press: CTRL+D

After Completion of your text you need to press Control + d  which is used to save the file and come out of the prompt.The above statement will create a file named ‘Amit_new.txt’.

17.Explain the significance of touch command in unix?

Answer:

touch command is used to create the empty or zero byte file,touch command also used to update the access or modification time of the file.touch command is basically used to create multiple files in linux or unix.using Cat command one can create or update one file at a time but using touch command user can only create multiple zero byte files but can not update multiple files at a time.

Syntax:

To create single zero byte file:

$ touch File_Name

To create multiple zero byte files:

$ touch file1 file2….File_N

Example:

To create single zero byte file:

$ touch Yodhini.txt

To create multiple zero byte files:

$ touch Yodhini.txt,Amit.txt

The above command will create 2 zero byte files named Yodhini and Amit.

18.How to Delete files in Unix Operating System?( 90% asked Unix Scripting Interview Questions )

Answer:

rm Command is used to delete file in Unix.

Example:

rm: – To delete a file

E.g. $rm File_Name

$ rm –i :- To delete a file with permission/confirmation.

E.g: $ rm –I file_name

$ rm –f :- It deletes a file forcefully

E.g:- $ rm –f File_Name.

19.Can we delete multiple files at a same time in Unix? How?( 80% asked Unix Scripting Interview Questions )

Answer:

rm command is used to delete multiple files at a same time.

Example:

$ rm file1 file2 ……File_N

E.g: rm Amit_Emptyfile

20.Explain echo and printf commands in unix?

Answer:

Using Echo as well as printf command user can create file in unix.

Syntax:

echo ………lines of file.[\n]…. > nameoffile.txt

printf………lines o file………> nameoffile.txt

Example:

echo this is one line file . > Amit.txt

printf this is 2 lines file \n  this is second line > Amit1.txt

\n is used as new line character which will create more than 1 lines of the file.

21.What is difference between absolute path and related path?

Answer:

Absolute path refers to the exact path as defined from the root directory. Related path refers to the path related to the current locations.

22.What does (.) And (..) means in unix?

Answer:

The current directory (.)

In UNIX, (.) means the current directory, so typing cd .

NOTE: there is a space between cd and the dot

Means stay where you are (the unixstuff directory).

This may not seem very useful at first, but using (.) as the name of the current directory will save a lot of typing.

The parent directory (..)

(..) Means the parent of the current directory, so typing cd .. Will take you one directory up the hierarchy (back to your home directory).

23.What is the UNIX command to list files/folders in alphabetical order?

Answer:

The ‘ls –l’ command is used to list down files and folders in alphabetical order. When you use ‘ls –lt’ is list down files /folders sorted with modified time.

24.What code would you use in a shell script to determine if a directory exists?

Answer:

The UNIX test command with the -d option can be used to determine if a directory exists.

Example:
The following test command expression would be used to verify the existence of a specified directory, which is stored in the variable $mydir:

if [ -d $mydir ]
then
command(s)
fi

If the value stored in the variable mydir exists and is a directory file, the command(s) located between then and fi will be executed.

You can consult the test command’s man page (“$ man test”) to see what test command options are available for use.

25.Describe links and symbolic links in UNIX?

Answer:

The second name for a file called a Link. It is used to assign more than one name for a file. It is not valid to assign more than one name to a directory or to link filenames on different computers.

General command ‘– ln filename1 filename2’

A symbolic link is the files that use to contain only the name of other files include in it. Directed to the files pointed by it is the operation of the symbolic link.

General command ‘– ln -s filename1 filename2’

26.Explain about “s” permission bit in a file?

Answer:

1.“s” bit is called “set user id” (SUID) bit.

2.“s” bit on a file causes the process to have the privileges of the owner of the file during the instance of the program.

For example,

executing “passwd” command to change current password causes the user to writes its new password to shadow file even though it has “root” as its owner.

27.How will you find the 99th line of a file using only tail and head command?( 80% asked Unix Scripting Interview Questions )

Answer:

tail +99 file1|head -1

28.What are zombie processes?

Answer:

These are the processes which have died but whose exit status is still not picked by the parent process. These processes even if not functional still have its process id entry in the process table.

29.How to create directory in Unix Operating Systems? Explain with example.( 100% asked Unix Scripting Interview Questions )

Answer:

user can create the directory using Mkdir command in unix.

Syntax :

$Mkdir Directory_name

Example:

$Mkdir Amit

Mkdir command is used to create  Unix Directory in present working directory.If user wants to create directory on the specific path then just use following syntax:

Syntax :

$Mkdir path/Directory_name

Example:

$Mkdir usr/bin/Amit

The above statement will create directory in usr/bin folder.Mkdir command produces no output if successfully created directory.

30.How to create Parent Directory in Unix?

Answer:

Sometimes user wants to create a directory where the specified directory is not exist.Means user is trying to create parent directory.

$mkdir /tmp/Pradnya/Unix
mkdir: Failed to make directory "/tmp/Pradnya/Unix"; 
No such file or directory $

 

If user is facing above error then user needs to use -p option which is used to create parent directory and all the necessary directories directly.

Example :

$mkdir -p /tmp/Pradnya/Unix
$

The above statement will create the directory named ‘Pradnya’ in tmp folder and ‘Unix’ in ‘Pradnya’ folder.

31.How to remove directory in Unix?

Answer:

To remove directory user will have use rmdir command which stands for ‘Removing Directory’.Before removing directory user needs to check that the specified directory is empty or there is no any file or subdirectory inside that directory.

Syntax:

$rmdir directory_name

Example:

$rmdir Amit

$

The above statement will remove the directory named ‘Amit’.

32.How to change directory in unix?

Answer:

One of the most used command in unix is Cd which changes the directory path.User can be able to go to any specified path using cd command.

Syntax:

$cd directory-path

$

directory path is path of directory which you want to change.

Example:

$cd usr/amit/bin

$

The above statement will go to the bin directory and user will able to work in ‘Bin’ Directory.

33.I want to connect to a remote server and execute some commands, how can I achieve this?( 80% asked Unix Scripting Interview Questions )

Answer:

We can use ssh to do this:

ssh username@serverIP -p sshport

Example

ssh root@122.52.251.171 -p 22

Once above command is executed, you will be asked to enter the password.

34.If there are 2 files. How to print the records which are common to both?

Answer:

We can use “comm” command as follows:

comm -12 file1 file2               … 12 will suppress the content which are

unique to 1st and 2nd  file respectively.

35.How do you access command line arguments from within a shell script?

Answer:
Arguments passed from the command line to a shell script can be accessed within the shell script by using a $ (dollar sign) immediately followed with the argument’s numeric position on the command line.

Example:
For example, $1 would be used within a script to access the first argument passed from the command line, $2 the second, $3 the third and so on. Bonus: $0 contains the name of the script itself.

36.How to rename directory in Unix?

Answer:

Using mv command user will rename the directory name.

Syntax :

$mv Old_directory New_directory

Example :

$mv Amit pradnya

The above mv statement is used to rename the directory named ‘Amit’ to ‘Pradnya’.

37.How to create hidden files in Unix?

Answer:

Any file_name/directory_name starts with ‘.’ is hidden file/directory.

E.g.– $ cat > .file_name         $ mkdir .directory_name

—————-

Ctrl+d [To save & close file]

 

$ mv emp .emp         Hide existing file

$mv .emp emp          Unhide existing file

$ mv abc .abc            Hide directory

$ mv .abc abc            Unhide directory

38.What are different types of permissions in Unix?

Answer:

Following are different types of permissions in Unix:

Owner Permissions :

Owner Permissions are Unix File Permissions given to the specific owner to open,read,write or execute the file

Group Permissions :

Group permissions are Unix File Permissions  to specific group of users to open,read,write and execute the file.

World Permissions :

These are Unix File Permissions  to other user to perform open,read,write, execute file permission.

39.If user wants to give all permissions to file which command is useful?

Answer:

chmod 477 Permission_File    

It gives Read Permission to User, Read/Write/Execute  Permissions to Group & Others.

$ ls –l Permission_File& see the O/P

40.Give me different commands to set different permissions to the file?

Answer:

1) chmod 477 Permission_File    

It gives Read Permission to User, Read/Write/Execute  Permissions to Group & Others.

$ ls –l Permission_File& see the O/P

 

2) chmod700Permission_File  

It gives Read/Write/Execute Permission to User and No  Permissions to Group & Others

$ ls –l Permission_File& see the O/P

 

3) chmod777Permission_File      

It gives Read/Write/Execute Permission to User, Group & Others

$ ls –l Permission_File& see the O/P

 

4) chown:To change Owner of the File/Directory

Syntax:$ chownOwner_Name File/Directory

41.What is Grep Command in Unix.Explain with exmaple.

Answer:

Grep Command which is used to search file or directory or string in a File or directory in unix operating system.Grep command actually searches the  file which has the given pattern.Grep command is also used to search the string or regular expression in given file or files.

Syntax :

$grep options [Pattern] File1….File n.

42.How to search given string in the file?

Answer:

You can directly search the specified string in a file using grep command.But make sure that the searchis case sensitive and the string which you want to search is also case sensitive.

Syntax :

$grep Search String Filename

Examples :

a) $ grep “complexsql” Grep_File

It prints the line containing complexsql.

Output :

The Website is named as complexsql Technologies

b) $ grep “Complexsql” Grep_File

It prints the line containing Complexsql

Output :

Planning for coaching classes with Complexsql

c) $ grep “Complex” Grep_File

It prints the line containing Complex

d) $ grep “Complexsql” file1 file2 file3

It searches ‘Complexsql’ string in file1, file2 & file3 files and print those lines.

e) $grep Complex*

It searches the Complex string in current directory all files.

43.Which option is used for case insensitive search in Grep Command?

Answer:

The Grep also used to search the string which is used for case insensitive search. We need to use the option named -i for case insensitive search.

Syntax :

$grep -i Search String Filename

Example :

$grep -i “complexsql” Filename

The above statement is used to fetch the lines which has complex word whether it is capital letters or in small letters.

Output :

The Website is named as complexsql Technologies

Planning for coaching classes with Complexsql

Complexsql

44.How to count number of lines in file?

Answer: 

Using Grep Command user can count the number of lines which is having that specific string.

Syntax :

$grep -c Search String Filename

Example :

$grep -c “complexsql” Filename

Output :

3

45.How to print lines with line number in Unix?

Answer:

grep command is used to print the line with line number using -n option of Grep command.

Syntax :

$grep -n Search String Filename

Example :

$grep -n “complexsql” Filename

Output :

The Website is named as complexsql Technologies 2

46.How to search file which contains specific string name?

Answer:

Grep command is also used to search the filename which contains the specific keyword.The option -l is used to search the filenames which contains that specific string.

Syntax :

$grep -v Search String Filename

Example :

$grep -v “complexsql” Filename

Output :

grep_file

47.Give the Grep command examples with specific character pattern search?

Answer:

a) $ grep “Complexsql*” Grep_File:

It prints complete line where the pattern is found

Output :

Planning for coaching classes with Complexsql

Complexsqltechnologies

Complexsqltechnologies

Complexsql

 

b) $ grep “I[tud]” Grep_File

It prints lines starting with I and any pattern matching in []

Output :

This institute is at Hadapsar  Pune 411001

c) $ grep “N..e” Grep_File

It prints (.) any single character

Note:(.) is a wild card character, it matches any single character

48.How to delete empty lines from the File? ( 80% asked Unix Scripting Interview Questions )

Answer:

To Remove empty lines from the file following commands are used:

$ grep -v “^$” Grep_File>Test

$ mv Test Grep_File

 OR

$ grep -v “^$” Grep_File>Test | mv Test Grep_File

49.What is fgrep command in unix?

Answer:

It is used to search multiple strings, but it doesn’t allow searching regular expressions. It searches the strings faster that the grep.

$ fgrep “Unix

Oracle

dba’’ Student

It prints line containing Unix, Oracle or dba from Student file

$ fgrep “Hari

Oracle

103” Student

It prints line containing Hari, Oracle or 103 from Student file

50.What is egrep command in unix?

Answer:

It is combination of grep&fgrep plus some additional wild card characters.

Additional Wild Characters:

1)  (|): It matches any one string in the given list.

$ egrep “Unix|Oracle|dba” Student

It prints line containing Unix, Oracle or dba from Student file

$ egrep “Hari|Oracle|103” Student

It prints line containing Hari, Oracle or 103 from Student file.

Hope you like this article of 50 most important unix interview questions and answers with example.Please comment in comment section for different suggestions.

Leave a Reply

Your email address will not be published. Required fields are marked *