Unix Questions
Unix Questions
1. What is ls –ltra
Sol: it displays long listing files based on time stamp and reverse including hidden files
2. How to rename a file abc.dat to xyz.dat
Sol:mv abc.dat xyz.dat
3. Create a file with time stamp 2015-jan-15
Sol:touch –t 2015-01-15-12-34.45 t.txt
4. How to create empty files abc.dat and xyz.dat
Sol:touch abc.dat xyz.dat
5. Remove
a) All files in current directory
Sol:rm*
b) All files in current directory (ask confirmation before delete)
Sol:rm –i *
c) Remove the directory /home/abc (including files under abcdir)
Sol:rm –r /home/abc
6. Find
a) Number of lines in a file
Sol:wc –l t.txt
b) Number of words from 5 to 15 lines
Sol:sed –n 5,15p t.txt | wc -w(or) head -15 t.txt|tail -10|wc -w
c) Number of characters in first 10 lines
Sol:sed –n 1,10p t.txt | wc–c (or) head -10 t.txt|wc -c
7. How to change file permissions to read and write only (no execution )
a) Give read, write and execute permission’s to all the files in only current directory
Sol:chmod 777 *
b) Remove write permissions to file called test.dat
Sol:chmod555 test.dat
c) Give read, write and execute permission’s to all files including subdirectory
Sol:chmod 777 –R *
d) Change the owner of a file from jk123 to xy123
Sol: chown xy123 r.txt
8. Display by using cut and without using cut command for a pipe (“|”) delimiter file
a) First field
Sol:cut –d”|” –f1 l.txt
a) number of employees
Sol: cut –d”,” –f1 n.txt |wc -l
b) number of departments
sol:cut –d”,” –f2 n.txt|sort –u|wc -l
c) number of times department repeated
sol:cut –d”,” –f2 n.txt|sort|uniq -c
d) find duplicated department numbers
Sol:cut –d”,” –f2 n.txt|sort|uniq -d
e) Create a file dept_distinct.dat with the department numbers repeated only once.
Sol:cut –d”,” –f2 n.txt|sort|uniq –u > dept_distinct.dat
f) Create a file with highest salary from each department
g) Create a file with lowest salary from each department
h) Create a file with deptno, no of employees
Sol:cut –d”,” –f2 n.txt|sort|uniq–c> e.txt
10. Display last record using sed command?
Sol:sed –n “$p” t.txt
a) How can u display the 20 records from 50th record?(using sed and without using
sed )
Sol:sed –n “50,70p” n.txt (or) head -70|tail -20
b) Delete the records from 10 to 50 records
Sol:sed“10,50d” t.txt
c) Delete the first line (using sed and without using sed )
Sol:sed “^d” n.txt (or)
d) Delete the last line (using sed and without using sed )
Sol:sed “$d” n.txt
11. Replace Jeevan with Satya
a) Only first occurrence
Sol:sed “s/Jeeavan/Satya/1” s.txt(but not working in mobaxterm)
b) Only second occurrence
Sol:sed “s/Jeeavan/Satya/2” s.txt(but not working in mobaxterm)
c) All occurrences
Sol:sed “s/Jeeavan/Satya/g” s.txt
12. Replace Jeevan(any case) with Satya including subdirectories from the current
directory
Sol:sed“s/jeevan/satya/gi” *
13. display the first 10 records
Sol: head -10 t.txt
14. display the last 10 records
Sol: tail -10 t.txt
15. VI commands
a) Go to the first line
Sol:type % in Ex command mode
b) Go to the last line
Sol:type $ in Ex command mode
c) Delete the lines from 5 to 10
Sol:type 5,10d in Ex command mode
d) Delete the last line
Example:kshtest.ksh a b c
Output should be
Script name:test.ksh
Total arguments passed to script: 3
First argument is: a
Second argument is: b
Third argument is: c
Arguments passed to the scripts are: a b c
20. What is $0, $1, $3, $#, $@, $?
21. For the following data
1|a|b|c
2|x|y
3|x
4|5|6|7
7|8|9|10
a) First filed
Sol:cut –d”|” –f1 t.txt
b) Last field
Sol:cut –d”|” –f4 t.txt
c) Number of fields in each line
Sol:
d) Display distinct fileds in entire file
e) Display all lines which contains only one pipe.
22. Write a script to accept two numbers and print product of two numbers.
23. If Pwd is pointing to Server1:/home/jk1234
a) Copy the file test123.dat from server2:/home/pk1234 to server 1
/home/jk1234/xyz/
b) Copy the file to xzy.dat from server1 to server2:/home/pk1234