You are on page 1of 5

awk -F "," '{ sum += $6 } END { print sum }' SampleCSVFile_556kb.

csv

1. How to display the 10th line of a file?


head -10 filename | tail -1

2. How to remove the header from a file?


sed -i '1 d' filename

3. How to remove the footer from a file?


sed -i '$ d' filename

4. Write a command to find the length of a line in a file?

The below command can be used to get a line from a file.


sed �n '<n> p' filename

We will see how to find the length of 10th line in a file


sed -n '10 p' filename|wc -c

5. How to get the nth word of a line in Unix?


cut �f<n> -d' '

6. How to reverse a string in unix?


echo "java" | rev

7. How to get the last word from a line in Unix file?


echo "unix is good" | rev | cut -f1 -d' ' | rev

8. How to replace the n-th line in a file with a new line in Unix?
sed -i'' '10 d' filename # d stands for delete
sed -i'' '10 i new inserted line' filename # i stands for insert

9.How to check if the last command was successful in Unix?


echo $?

10. Write command to list all the links from a directory?


ls -lrt | grep "^l"

11. How will you find which operating system your system is running on in UNIX?
uname -a

12. Create a read-only file in your home directory?


touch file; chmod 400 file

13. How do you see command line history in UNIX?

The 'history' command can be used to get the list of commands that we are
executed.

14. How to display the first 20 lines of a file?

By default, the head command displays the first 10 lines from a file. If we
change the option of head, then we can display as many lines as we want.
head -20 filename

An alternative solution is using the sed command


sed '21,$ d' filename
The d option here deletes the lines from 21 to the end of the file

15. Write a command to print the last line of a file?

The tail command can be used to display the last lines from a file.
tail -1 filename

Alternative solutions are:


sed -n '$ p' filename
awk 'END{print $0}' filename

16. How do you check how much space left in current drive ?
By using "df" command in UNIX. For example "df -h ."

17. What is the difference between Swapping and Paging?


Swapping:
Whole process is moved from the swap device to the main memory for execution.

Process size must be less than or equal to the available main memory.
It is easier to implementation and overhead to the system.
Swapping systems does not handle the memory more flexibly as compared to the
paging systems.

Paging:
Only the required memory pages are moved to main memory from the swap device
for execution.
Process size does not matter. Gives the concept of the virtual memory.
It provides greater flexibility in mapping the virtual address space into the
physical memory of the machine.
Allows more number of processes to fit in the main memory simultaneously.
Allows the greater process size than the available physical memory.
Demand paging systems handle the memory more flexibly.

18. How do you find how many cpu are in your system and there details?
By looking into file /etc/cpuinfo for example you can use below command:
cat /proc/cpuinfo

19. What is difference between HardLink and SoftLink in UNIX?


I have discussed this Unix Command Interview questions in my blog post
difference between Soft link and Hard link in Unix

20. What is Zombie process in UNIX? How do you find Zombie process in UNIX?
When a program forks and the child finishes before the parent, the kernel
still keeps some of its information about the child in case the parent might need
it - for example, the parent may need to check the child's exit status.
To be able to get this information, the parent calls 'wait()'; In the
interval between the child terminating and the parent calling 'wait()', the child
is said to be a 'zombie' (If you do 'ps', the child will have a 'Z' in its status
field to indicate this.)
Zombie : The process is dead but have not been removed from the process
table.

21. What is "chmod" command? What do you understand by this line �r-- -w- --x?
chmod command is used to change permission of a file or directory in UNIX.
The line you see shows the permission for three different set of people :
user, group and others.
User is the currently logged in user, while group is for all other member
which are part of certain group and others means anyone other than user and group
member.
Each group has three permissions rwx stands for read, write and execute and
they are written as user_group_others.
So in above line, user has only read permission, group members has write
permissions and other people has only execute permission.
If it is a directory then you need execute permission to go inside that
directory.
See here for more detailed answer.

22. How will you run a process in background? How will you bring that into
foreground and how will you kill that process?
For running a process in background use "&" in command line.
For bringing it back in foreground use command "fg jobid" and for getting job
id you use command jobs, for killing that process find PID and use kill -9 PID
command.
This is indeed a good Unix Command interview questions because many of
programmer not familiar with background process in UNIX.

23. How do you find how many cpu are in your system and there details?
By looking into file /etc/cpuinfo for example you can use below command:
cat /proc/cpuinfo

http://javarevisited.blogspot.in/2011/05/unix-command-interview-questions.html

https://career.guru99.com/top-50-unix-interview-questions/

http://www.folkstalk.com/2011/12/101-examples-of-using-find-command-in.html

###################################################################################
###############################
The cmp command tells you if two files are different and where the first difference
appears.
Here's an example comparing text files:

$ cmp file1 file2

###################################################################################
###############################

For most of us, this use of diff is probably not something we'd do very often.

If you only want to know if the files are different, you can try a simpler
approach.

$ diff -q file1 file2


###################################################################################
###############################

###################################################################################
###############################
The diff3 command works a lot like diff, but allows you to compare three files
instead of only two.
However, this command doesn't have all the options that the diff has and, no,
there's no diff4, diff5, etc.
Comparing files two at a time with your favorite comparison tool is probably a
better strategy most of the time.
$ diff3 file1 file2 file3
###################################################################################
###############################

cksum:
Checksums can also tell you if files are different.
While this might not be advantageous when the files are on the same system,
it can help a lot when they're on different systems.
By running the cksum command on each of the two systems,
you can determine if they're the same without having to move either of the files to
the other system or share a file system or directory.
Note that the cksum command is often used to verify the integrity of system files.

###################################################################################
###############################
The comm command will display the differences in text files in a different format.
In the example below, you can probably see that we're looking at three separate
columns of output.
The first and second represent the first and second files.
The third shows the lines which are the same in both of the two files.

$ comm file1 file2


###################################################################################
###############################
The cmp command tells you if two files are different and where the first difference
appears.
Here's an example comparing text files:

$ cmp file1 file2

###################################################################################
###############################

For most of us, this use of diff is probably not something we'd do very often.

If you only want to know if the files are different, you can try a simpler
approach.

$ diff -q file1 file2


###################################################################################
###############################

mkdir
Make Directory - ie. Create a directory.
rmdir
Remove Directory - ie. Delete a directory.
touch
Create a blank file.
cp
Copy - ie. Copy a file or directory.
mv
Move - ie. Move a file or directory (can also be used to rename).
rm
Remove - ie. Delete a file.
###################################################################################
###############################

UNIX text manipulation commands:


cat displays text
cut displays designated fields/characters
expand converts tabs to spaces
fmt simple text formatter
grep displays that match a pattern
head displays designated number of lines from top of file
join join two files together
less display text a page at a time
more display text a page at a time
nl numbers lines
od displays octal, decimal, hex dump of file
paste merges lines from two files
sort sorts lines in a file
strings displays text strings in a binary file
tac displays text in reverse order
tail displays designated number of lines from bottom of file
tee redirect STDOUT to another location
tr transliterate utility
unexpand converts spaces to tabs
uniq displays unique occurences of lines
wc counts characters, words, and lines

cat � Output the contents of a file.


diff � Identify the differences between two files.
merge � Performs a merge of the contents of three files.
sdiff � Compare two files, side-by-side.

You might also like