Wednesday, July 2, 2014

Interview Questions: Linux Commands



Top 25 Unix interview questions with answers (Part I)
How to print/display the last line of a file?
tail -1 file.txt
sed -n '$ p' test

How to print/display the first line of a file?
head -1 file.txt
sed '2,$ d' file.txt
the 'd' parameter basically tells [sed] to delete all the records from display output from line no. 2 to last line of the file (last line is represented by $ symbol).

How to display n-th line of a file?
sed –n '<n> p' file.txt
head -<n> file.txt | tail -1

How to remove the first line?
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.
sed –i '1 d' file.txt

How to remove the last line?
sed –i '$ d' file.txt

sed –i '5,7 d' file.txt

How to remove the last n-th line from a file?
tt=`wc -l a.txt | cut -f1 -d' '`;sed -i "`expr $tt - 4`,$tt d" a.txt

How to check the length of any line in a file?
sed -n '35 p' a.txt | wc -c

How to get the nth word of a line?
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.

echo "A quick brown fox jumped over the lazy cat" | cut -f4 -d' '

How to reverse a string?
echo "unix" | rev

How to get the last word from a line file?
echo "C for Cat" | rev | cut -f1 -d' ' | rev

wc -c file.txt | cut -d' ' -f1
wc -c a.txt | awk '{print $1}'

How to replace the n-th line in a file with a new line?
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 test if a zip file is corrupted?
unzip –t file.zip

How to unzip a file?
unzip –j file.zip

How to check if a file is zipped ?
file a.txt
file -i a.txt

How to list down file/folder lists alphabetically?
[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?
ls –l file.txt; echo $?

How to check all the running processes?
ps -e -o stime,user,pid,args,%mem,%cpu
By using “-o” switch, you can specify the columns that you want [ps] to print out.

ps –ef
If you wish to see the % of memory usage and CPU usage, then consider the below switches
ps aux

How to tell if my process is running?
ps -e -o stime,user,pid,args,%mem,%cpu | grep "opera"

How to get the CPU and Memory details in Linux server?
/proc/meminfo
/proc/cpuinfo

References
Top 25 Unix interview questions with answers (Part I)

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