UNIX INTERVIEW QUESTIONS AND ANSWERS – 3

UNIX-INTERVIEW-QUESTIONS-AND-ANSWERS-3

21) What is the difference between ‘ls -ltr’ and ‘ls -lrt’?

No difference is there, both commands gives the same result.

ls-command-unix-ltr-and-lrt-options-same-result

22) What happens to the file if we move/copy that file, will the modified time changed? Or will it carry the same created time?

If we copy the file, modified time will be changed. If we move the file there will be no change in the time.

move-copy-files-unix-change-modified-time

Read also: Unix interview questions and answers part – 1

23) Can we preserve the time even when we copy the files across directories?

Yes we can. We need to use ‘-p’ option while copying the file.

cp-command-with--p-option-unix

24) How can you remove duplicates from a file?

We can use ‘uniq’ command to remove duplicates from a file. ‘uniq’ does not detect repeated lines unless they are adjacent.  You may want to sort the input first, or use ‘sort -u’ without ‘uniq’.

$ sort emp_data.txt | uniq

uniq-command-in-unix-remove-duplicates

25) How can you display the duplicates in a file?

uniq -d’ command is useful to display duplicates from a file.

$ sort emp_data.txt | uniq -d

uniq-command-in-unix-capture-duplicates

26) I have a one file which contains employee name and department number in it. Can you display the employee count by GROUP BY department number?

We can use ‘uniq -c’ command to employee count by GROUP BY department number.

$ cut -d ',' -f2 emp_data.txt | sort | uniq -c

 

uniq-command-in-unix-group-by-count

Read also: Unix interview questions and answers part – 2

27) How to check whether a file is arrived in a particular directory?

We need to write a shell script for this requirement. This script can be customized according to your requirement.

FILE_FLG="N"

cd $src_loc

while [ FILE_FLG ­eq "N" ]; do

if [ ­-e $src_file_nm ]; then ;# If file exists in directory

FILE_FLG="Y"

fi

sleep 600 # loop wait for 10 minutes

done

28) Differentiate cmp command from diff command in Unix?

The cmp command is used mainly to compare two files byte by byte, after which the first encountered mismatch is shown. On the other hand, the diff command is used to indicate the changes that is to be made in order to make the two files identical to each other.

diff-command-cmp-command-in-unix

29) How would you count every occurrence of the term “unix” in all the files appearing under the current directory, and its subdirectories, recursively?

$ grep -orI unix . | wc -l

To list every occurrence of the term “unix” on a separate line, one must run grep -o unix <path>. Adding the ‘r’ option to the command makes the search recursively process every file under the given path, and the ‘I’ option ensures that matches in binary files are ignored. In addition, the ‘w’ option can be included to match the exact term only, and ignore superstrings such as “unixsystem”, and to make the search case-insensitive, the i flag can be added as well:

$ grep -iworI potato . | wc -l

30) How would you write a shell script that prints all the additional arguments passed to it in reverse order?

for (( i = ${#}; i &amp;gt; 0; i-- )); do

echo ${!i}

done

The arguments are available as $<n>, where n is the position of the argument.

For example, $0 would give the name of the script, $1 would give the first additional argument, $2 the second, and so on. The total number of additional arguments is found in $#.

A loop that starts at $# and ends at 1 can be used to print each additional argument in reverse order.

Comments

comments

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: