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)
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)