UNIX
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
b) 4th and 5thfields
Sol:cut –d”|” –f4,5 l.txt
c) Last field if file contains 4 fields
Sol:cut –d”,” –f4 l.txt
d) Last filed (if number of fields in each line are not same)
e) from 3 field to 8th filed
sol:cut –d”|” –f3-8 l.txt
9. from the following data
empno,deptno,salary
1,10,5000
2,20,2000
3,10,3000
4,10,4400
5,30,2000
7,20,5005
6, 10, 2600
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
Sol:type $d in Ex command mode
e) Delete the current line (line where cursor presents )
Sol:type d at present line
f) Replace Satya with Jeevan
g) Display the line numbers
16. Display all the files which contains “Shahul”
a) In current directory
Sol:grep “Shahul” *
b) Including subdirectories
Sol:grep –ir “Shahul” *
c) By ignoring case sensitive
Sol:grep –i “Shahul” *
d) Display with line numbers
Sol:grep –inr “shahul”
e) Starting of the file
Sol: grep –i “^Shahul” *
f) End of the file
Sol: grep –i “Shahul$” *
g) Number of times match string repeated for each line
h) Total number matches in a particular file
Sol:grep –ic “Shahul” r.txt
i) All lines which doesn’t contains “Shahul”
Sol: grep –iv “Shahul” r.txt
j) Display all lines which doesn’t contains “Shahul” either at starting or ending
Sol:grep –iv “^Shahul$” r.txt
k) Display the lines which contains “Phani” in first 50 lines
Sol:head -50 t.txt|grep –i “phani”
l) Display all lines which contains phani or Shahul
Sol:egrep –i “(phani|Shahul)” t.txt
m) Display all the lines which x repeats more than twice.
n) Grep all the lines a[]b ([] specifies any character )
Sol:grep “a[abc]b” t.txt
o) Explain the difference between grep , egrep and fgrep with an example
Sol:grep –i “shahul” t.txt
fgrep –i “Shahul
>ramana” t.txt
egrep –i “(shahul|ramana)” t.txt
17. Find all
a) Directories from the directory /home
Sol:find /home –type d
b) Directories only in current directory
Sol:find . –type d
c) Display all empty size files
Sol:find . –type f –size0
d) Find 10 days old directories
Sol:find . –type d +10
e) Find all files from the current directory which are older than 30 days and size
>10MB
Sol:find . –type f –mtime +30 –size +10m
f) Find all files from the current directory which are older than 30 days and size
>10MB and zip them in one file
Sol:find . –type f –mtime +30 –size +10m > test.dat
g) Find all files from the current directory which are older than 30 days and size
>10MB and remove them
18. How to
a) Find all process running in system
Sol:ps
b) How to kill a process
Sol: kill -9 processid
c) Userid
Sol:logname
d) Servername
Sol:hostname
e) Osname
Sol:uname
f) First line of the script
g) Run a shell script
h) Redirect one command output to another command
Sol:catr.txt|wc -w
i) Redirect output of a command to file
Sol:sort –t”,” –k3 t.txt > r.txt
j) Run the shell script with arguments and without arguments
19. Write a script to print scriptname, first argument, second argument and total no of
arguments passed to the script and all argument passed to the script.
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