Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
61 views

Linux Interview Questions - Part 1: Home Shell Scripts Different Methods Awk & Sed Forum Contact Us About

This article contains a few interview questions related to UNIX or Linux Shell Scripting and command-line stuff. Some of them could be commonly asked, most of them are not so. For questions for which we have a detailed explanation, a link would be provided after the answer for detailed reference.

Uploaded by

Harideep Singh
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
61 views

Linux Interview Questions - Part 1: Home Shell Scripts Different Methods Awk & Sed Forum Contact Us About

This article contains a few interview questions related to UNIX or Linux Shell Scripting and command-line stuff. Some of them could be commonly asked, most of them are not so. For questions for which we have a detailed explanation, a link would be provided after the answer for detailed reference.

Uploaded by

Harideep Singh
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

The UNIX School: Linux Interview Questions - Part 1

http://unix-school.blogspot.in/2012/04/linux-interview-qu...

Share

Report Abuse

Next Blog

Create Blog

Sign In

Home

Shell Scripts

Different methods

awk & sed

Forum

Contact Us

About

Monday, April 30, 2012

Subscribe here for email updates


Email address... Submit

Linux Interview Questions - Part 1


Tweet 0 1 Like ShareThis 8

UNIX School Fans on Facebook


The UNIX School on Facebook
Like 76 people like The UNIX School.

This article contains a few interview questions related to Unix or Linux Shell Scripting and command-line stuff. Some of them could be commonly asked, most of them are not so. For questions for which we have a detailed explanation, a link would be provided after the answer for detailed reference. The idea of this article is not just to provide an answer to a question, instead to know of the many different options which are present to solve a particular problem. 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. Ref: What is Shell Scripting? 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. Ref: What is PATH variable? 3. How to find the length of a string in Linux? $ x="welcome" $ echo ${#x} 7 Ref: Different ways to find the length of a string. 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. Ref: 3 different timestamps associated with a file.

Sheng-Jhih Abhilasha

Edward

James

Pradipta

Vamshi

Rikki

Facebook social plugin

Subscribe via Feeds

Ads by Google

Variable Linux Unix Linux The UNIX School on G+


We're on
Follow

1 of 8

Saturday 14 July 2012 02:56 PM

The UNIX School: Linux Interview Questions - Part 1


5. How to get the list of files alone in a directory in Linux? $ ls -lrt | grep ^-

http://unix-school.blogspot.in/2012/04/linux-interview-qu...
The UNIX School Followers
Join this site
with Google Friend Connect

Members (70) More

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. Ref: Positional parameters in a shell script. 8. How to find the sum of all numbers in a file in Linux? $ awk '{x+=$0}END{print x}' file Ref: Different ways to find the sum of all numbers in a 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.

Already a member? Sign in

Popular Posts of The UNIX School


$ 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 Ref: 15 different ways to display or print the file contents 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. Ref: Difference between set, setenv and export. 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. Ref: File Permissions vs Directory Permissions 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. Ref: Positional parameters in a shell script. 14. How to remove the Control-M character from a file in Linux? $ dos2unix file Linux Interview Questions - Part 1 What is SUID Shell Script to do shell scripting faster 10 examples to group data in a CSV file Internal vs External commands How to connect to sqlplus from shell? find files modified in the last X days and X hours..? 5 important things to follow to be a fast learner Delete Contrl-M character in a file

grep The UNIX School

Search
Article Archive
2012 (45) July 2012 (2) June 2012 (9) May 2012 (9) April 2012 (11) Linux Interview Questions - Part 1 Different ways to zero pad a number or a variable What is umask? find files by name or part of name or extension

2 of 8

Saturday 14 July 2012 02:56 PM

The UNIX School: Linux Interview Questions - Part 1

http://unix-school.blogspot.in/2012/04/linux-interview-qu...

Ref: Different ways to remove the Control-M characters from a 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. Ref: Profile file for different shells. 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?

here-doc(EOF) issue in sqlplus or FTP What is CDPATH ? Different ways to print the first line of a file cp - 10 different copy command examples What is sourcing a file? Different ways to get the filename from absolute p... date & GNU date March 2012 (10) February 2012 (3) January 2012 (1) 2011 (29)

2010 (36) $ cat file apple orange Recommended links banana apple orange papaya Unix Forum $ awk '{for(i=1;i<=NF;i++)a[$i]++;}END{for(i in a){print i, a[i];}}' file banana 1 Copyright Protected 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. Ref: Internal vs External command. 19. x and y are two variables containing numbers? How to add these 2 numbers? $ expr $x + $y Ref: Different ways to do arithmetic operations in Linux 20. How to add a header record to a file in Linux? $ sed -i '1i HEADER' file Ref: Different ways to add header and trailer record to a 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?

3 of 8

Saturday 14 July 2012 02:56 PM

The UNIX School: Linux Interview Questions - Part 1

http://unix-school.blogspot.in/2012/04/linux-interview-qu...

$ find . -mmin 30 Ref: find files modified before X days, X hours and X mins in Linux. 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^.*/^^' Ref: Different ways to print the filename from the absolute path. 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

28. How to get tomorrow's date in Linux? $ date -d "1 day" Ref: GNU date in Linux 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 Ref: Join all lines in a file in Linux 31. How to join every 2 lines in a file in Linux? $ sed 'N;s/\n//' file Ref: Join every 2 lines in a file in Linux. 32. A shell script will ask for 3 inputs. The user will not be physically

4 of 8

Saturday 14 July 2012 02:56 PM

The UNIX School: Linux Interview Questions - Part 1

http://unix-school.blogspot.in/2012/04/linux-interview-qu...

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}' Ref: Different ways to find the total lines in a file in Linux. 34. How to print the first line or the header record in a file? $ head -1 file Ref: Different ways to print the first line of a file in Linux. 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

Ref: Replace or substitute file contents - Part 1 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?

5 of 8

Saturday 14 July 2012 02:56 PM

The UNIX School: Linux Interview Questions - Part 1

http://unix-school.blogspot.in/2012/04/linux-interview-qu...

$ sed 's/$/00/' file Ref: sed replace or substitute file contents - Part 2 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. Ref: Different ways to connect to sqlplus from Linux. 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 Ref: Secure sqlplus connection in Linux. 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 Ref: 3 different ways to rename a group of files in Linux. 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. Ref: What is CDPATH? 46. How to zero pad a number in Linux? Say, to zero pad a number to 4 places: $ x=20 $ printf "%04d\n" $x Ref: Different ways to zero pad a number in Linux. 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?

6 of 8

Saturday 14 July 2012 02:56 PM

The UNIX School: Linux Interview Questions - Part 1

http://unix-school.blogspot.in/2012/04/linux-interview-qu...

$ find . -name *.c | sed 's^.*/^^' Ref: find files by name or extension or part of file name. 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} Ref: cp - 10 different cp command examples in Linux. 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. Ref: What is sourcing a file in Linux?

Tweet

Like

ShareThis

You might also like: sed - 25 examples to delete a line or pattern in a file A Shell Script to do shell scripting faster 10 tips to improve Performance of Shell Scripts
LinkWithin

Labels: commands, Linux Interview Questions and answers

4 comments: Amit April 30, 2012 11:48 PM nice 1.. you missed one important question. difference between exec and fork ( with an example pls :) ).... Reply Replies Guru Prasad May 1, 2012 8:59 AM

7 of 8

Saturday 14 July 2012 02:56 PM

The UNIX School: Linux Interview Questions - Part 1

http://unix-school.blogspot.in/2012/04/linux-interview-qu...

thanks amit..this is just the first part, and as said in the beginning, it is mainly on the command-line stuff and shell scripting..we will have lot many parts in future in which it will be covered. Reply

NARESH S July 3, 2012 4:07 PM The documentary which you have provided is really superb.. It is really useful for Linux Administrators. Thanks for grate information......... Reply

NARESH S July 3, 2012 4:16 PM Thanks To Providing the great Information. This is very useful who are into Linux & Unix Environment. Reply
Enter your comment...

Comment as:

Select prole... Preview

Publish

Newer Post
Subscribe to: Post Comments (Atom)

Home

Older Post

LinkWithin

Travel template. Powered by Blogger.

8 of 8

Saturday 14 July 2012 02:56 PM

You might also like