Friday, June 27, 2014

Collected Interview Questions: Linux Shell Commands



https://www.cyberciti.biz/faq/how-to-find-my-public-ip-address-from-command-line-on-a-linux/
  1. dig +short myip.opendns.com @resolver1.opendns.com
  2. Or dig TXT +short o-o.myaddr.l.google.com @ns1.google.com
myip="$(dig +short myip.opendns.com @resolver1.opendns.com)"
echo "My WAN/Public IP address: ${myip}"
http://superuser.com/questions/508881/what-is-the-difference-between-grep-pgrep-egrep-fgrep

fgrep

fgrep is an acronym that stands for "Fixed-string Global Regular Expressions Print".
fgrep (which is the same as grep -F) is fixed or fast grep and behaves as grep but does NOT recognize any regular expression meta-characters as being special. The search will complete faster because it only processes a simple string rather than a complex pattern.
For example, if I wanted to search my .bash_profile for a literal dot (.) then using grep would be difficult because I would have to escape the dot because dot is a meta character that means 'wild-card, any single character':
grep "." myfile.txt
The above command returns every line of myfile.txt. Do this instead:
fgrep "." myfile.txt
Then only the lines that have a literal '.' in them are returned. fgrep helps us not bother escaping our meta characters.

pgrep

pgrep is an acronym that stands for "Process-ID Global Regular Expressions Print".
pgrep looks through the currently running processes and lists the process IDs which matches the selection criteria to stdout. pgrep is handy when all you want to know is the process id integer of a process. For example, if I wanted to know only the process ID of my mysql process I would use the command pgrep mysql which would return a process ID like 7312.

How to grep a text file which contains some binary data?
You could run the data file through cat -v, e.g
$ cat -v tmp/test.log | grep re
http://tech-wonderland.net/blog/shell-pattern-explained-with-shell-script-examples.html
Reference: http://javarevisited.blogspot.com/2011/05/unix-command-interview-questions.html
What is difference between ps -ef and ps -auxwww?
ps -ef will omit process with very long command line while ps -auxwww will list those process as well.

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.

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

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.

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.

http://linuxcommando.blogspot.com/2007/10/grep-with-color-output.html
There are 3 color options available to you:
  • --color=auto
  • --color=always 
  • --color=never

With color=always, it colors the matched string.
$ grep --color=always  abc  a_file.txt
abcdef

Quite often, you want to page through the output:
$ grep --color=always  abc  a_file.txt |less 
ESC[01;31mabcESC[00mdef
(END)

The problem is that less does not understand those control characters, by default. You need to use the -R parameter.
$ grep --color=always  abc  a_file.txt |less -R
abcdef

Alternatively, use more.
$ grep --color=always  abc  a_file.txt | more 
abcdef

Another problematic scenario is when you want to save the grep output to a file. The output file will contain those control characters.

$ grep --color=always  abc  a_file.txt > myoutput.txt
$ less myoutput.txt
ESC[01;31mabcESC[00mdef
myoutput.txt (END)

With color=auto, it displays color in the output unless the output is piped to a command, or redirected to a file.
http://unix.stackexchange.com/questions/9252/determining-what-process-is-bound-to-a-port
netstat -lntp
netstat -lnp will list the pid and process name next to each listening port. This will work under Linux, but not all others (like AIX.) Add -t if you want TCP only.
There is a file Unix_Test.txt which contains words Unix, how will you replace all Unix to UNIX? 
sed s/Unix/UNIX/g fileName

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.

IP address using nslookup command
nslookup servername

IP address using ping command
ping servername

hostname -i

How to find all the links in a folder in UNIX or Linux ?
You need to use ls command which list everything in directory and then you need to list all the links, as they starts with "l" as first characters.
ls -lrt | grep '^l'

List the files in current directory sorted by size ? -

ls -l | grep ^- | sort -nr

List the hidden files in current directory ?

ls -a1 | grep "^\."

Delete blank lines in a file ?

cat sample.txt | grep -v ‘^$’ > new_sample.txt

How to find a process and kill that ?
ps -ef| grep process_identifier // will give you PID
kill -9 PID
How to run a program in background in UNIX or Linux ?
You can use & to run any process in background and than you can use jobs to find the job id for that process and can use  fg and bg command to bring that process into foreground and background.

How do you find all files which are modified 10 minutes before ?
This is another the Linux interview questions from frequently used command e.g. find and grep. you can use -mtime option of find command to list all the files which are modified 10 or m minutes before.
find . -mtime 1  (find all the files modified exact 1 day)
find . -mtime -1 (find all the files modified less than 1 day)
find . -mtime +(find all the files modified more than 1 day)


Display disk usage in Kilobytes ? - du -k
References: http://opensourcewin.wordpress.com/2012/05/11/linux-system-administrator-interview-questions-and-answer-with-pdf-part-01/
What command can be used to findout server architechure (x86 or x64) apart from uname?
arch

How do u findout the users who are NOT logged in for more than 30 days? which file u will check?
last , lastlog,  /var/log/wtmp
References

Labels

Review (572) System Design (334) System Design - Review (198) Java (189) Coding (75) Interview-System Design (65) Interview (63) Book Notes (59) Coding - Review (59) to-do (45) Linux (43) Knowledge (39) Interview-Java (35) Knowledge - Review (32) Database (31) Design Patterns (31) Big Data (29) Product Architecture (28) MultiThread (27) Soft Skills (27) Concurrency (26) Cracking Code Interview (26) Miscs (25) Distributed (24) OOD Design (24) Google (23) Career (22) Interview - Review (21) Java - Code (21) Operating System (21) Interview Q&A (20) System Design - Practice (20) Tips (19) Algorithm (17) Company - Facebook (17) Security (17) How to Ace Interview (16) Brain Teaser (14) Linux - Shell (14) Redis (14) Testing (14) Tools (14) Code Quality (13) Search (13) Spark (13) Spring (13) Company - LinkedIn (12) How to (12) Interview-Database (12) Interview-Operating System (12) Solr (12) Architecture Principles (11) Resource (10) Amazon (9) Cache (9) Git (9) Interview - MultiThread (9) Scalability (9) Trouble Shooting (9) Web Dev (9) Architecture Model (8) Better Programmer (8) Cassandra (8) Company - Uber (8) Java67 (8) Math (8) OO Design principles (8) SOLID (8) Design (7) Interview Corner (7) JVM (7) Java Basics (7) Kafka (7) Mac (7) Machine Learning (7) NoSQL (7) C++ (6) Chrome (6) File System (6) Highscalability (6) How to Better (6) Network (6) Restful (6) CareerCup (5) Code Review (5) Hash (5) How to Interview (5) JDK Source Code (5) JavaScript (5) Leetcode (5) Must Known (5) Python (5)

Popular Posts