UNIX
UNIX
1. What is ls –ltra
ls lists the files in the current working directory - but if another directory is specified, then ls will
list the files there, and in fact the user may specify any list of files and directories to be listed.
Options:
a) - l : long format, displaying Unix file types, permissions, number of hard links, owner,
group, size, last-modified date and filename
b) -t : sort the list of files by modification time.(newest first)
c) -a : lists all files in the given directory, including those whose names start with "."
(Which are hidden files in Unix). By default, these files are excluded from the list.
d) -r : will reverse the sorting order.
2. How to rename a file abc.dat to xyz.dat
$ mv abc.dat xyz.dat
3. Create a file with time stamp 2015-jan-15
$ touch -t 1501150808 xyz.dat
4. How to create empty files abc.dat and xyz.dat
$ touch abc.dat
$ touch xyz.dat
5. Remove
a) All files in current directory $ rm *
b) All files in current directory (ask confirmation before delete)
$ rm –i *
rm: remove regular empty file 'a.txt'? y
rm: remove regular empty file 'b.txt'? y
PARINITA REDDY@PARINITAREDDY ~/abc
$ ls -ltr
total 0
c) Remove the directory /home/abc (including files under abc dir)
$ rm -R /home/abc
6. Find
a) Number of lines in a file
$wc –l filename.txt
b) Number of words from 5 to 15 lines
1
UNIX
$ sed -n 5,15p a.txt | wc –w
$ head -15 a.txt|tail -11| wc -w
c) Number of characters in first 10 lines
$ sed –n 1,10p a.txt| wc –m
$ head -10 a.txt| ws -m
d) It will display only uniq values
$ sort za.txt | uniq –u
e) It will print only repeated lines
$ sort za.txt | uniq -d
f ) delete first and last lines
$ sed '1d;$d' zc.txt
g )It will display specified lines
$ sed -n -e 1p -e 4,8p -e 12p z.dat
h ) specified line will be incremented (double lines )
$ sed -e 1p -e 4,8p -e 12p z.dat
7. How to change file permissions to read and write only (no execution )
$ chmod 666 a.txt
a) Give read, write and execute permission’s to all the files in only current directory
$ chmod 777 *
b) Remove write permissions to file called test.dat
$ chmod 555 test.dat
$ chmod a-w test.dat
c) Give read, write and execute permission’s to all files including subdirectory
$ chmod -R 777 *
d) Change the owner of a file from jk123 to xy123
$chown xy123 filename.txt
8. Display by using cut and without using cut command for a pipe (“|”) delimiter file
a) First field
$cut –d “|” –f1 filename.txt
b) 4
th and 5thfields
$cut –d “|” –f4, 5 filename.txt
c) Last field if file contains 4 fields
$ rev c.txt| cut -d "|" -f1|rev
$ cut -d "|" –f4 c.txt
d) Last filed (if number of fields in each line are not same)
$ rev c.txt| cut -d "|" -f1|rev
e) from 3 field to 8th filed
$ cut -d "|" –f3-8 c.txt
9. From the following data
empno,deptno,salary
1,10,5000
2,20,2000
3,10,3000
2
UNIX
4,10,4400
5,30,2000
7,20,5005
6, 10, 2600
a) number of employees
$ cut -d "," -f1 emp.txt|uniq|wc -l
b) number of departments
$ cut -d "," -f2 emp.txt|sort|uniq|wc -l
c) number of times department repeated
cut -d',' -f2 filename|sort|uniq -c
d) find duplicated department numbers
$ cut -d "," -f2 emp.txt|sort|uniq -d
e) Create a file dept_distinct.dat with the department numbers repeated only once.
f) Create a file with highest salary from each department
$ cut -d "," -f3 emp.txt|sort -t "," –r |head -1
$ cut -d "," -f3 emp.txt|sort -t "," |tail -1
g) Create a file with lowest salary from each department
$ cut -d "," -f3 emp.txt|sort -t "," |head -1
$ cut -d "," -f3 emp.txt|sort -t "," –r |tail -1
h) Create a file with deptno, no of employees
cut -d',' –f1,2 filename>new filename
10. a) Display last record using sed command?
$ sed -n '$p' emp.txt
b) How can u display the 20 records from 50th record?(using sed and without using sed )
$sed –n ’50, $p’ filename.txt | sed –n ‘1,20p’
$tail –n +50 filename.txt | head –n 20
c) Delete the records from 10 to 50 records
$ sed –n ’10,50d’ filename.txt
d) Delete the first line (using sed and without using sed )
$ sed –n ‘1d’ filename.txt
e) Delete the last line (using sed and without using sed )
$ sed -n '$d' emp.txt
f )Delete specify word lines
$sed -i '/test/d' file.txt
3
UNIX
11. Replace Jeevan with Satya
a) Only first occurrence
$sed –e “s/Jeevan/Satya/1” file.txt
b) Only second occurrence
$sed –e “s/Jeevan/Satya/2” file.txt
c) All occurrences
$sed –e “s/Jeevan/Satya/g” file.txt
d) multiple lines
$ sed -e '1,3 s/string/str/g' -e '5,10 s/decimal/dec/g' zc.txt
e) Add starting “Y” and ending “N”
$ sed 's/.*/y & n /' zc.txt
f)removing line spaces
$sed '/^$/d' zp.txt
g) it wil print line by line
$ sed 'n;d' zp.txt
h)counting the no of lines
$sed -n '$=' zp.txt (or) $wc -l both same
i) it will display numbers onley data
$sed '/./=' zp.txt | sed '/./N; s/\n/ /'
(or)
It will display numbers of total file
$sed = zp.txt| sed 'N; s/^/ /; s/ *\(.\{6,\}\)\n/\1 /'
Use the following command to delete blank lines in a file
$ cat country.txt | sed -e '/^$/d'
j) Use the following command to delete blank lines in a file
$ cat country.txt | sed -e '/^$/d'
h) Add comma in beginning of the file
$sed "s/^/,/" zc.txt =>add semicolon last line==> $awk '{print $0 ";"}' zc.txt
12. Replace Jeevan(any case) with Satya including subdirectories from the current directory
13. display the first 10 records
$head filename.txt
14. display the last 10 records
$tail filename.txt
15. VI commands
Go to the first line :0
Go to the last line :$
Delete the lines from 5 to 10 : 5,10 d
Delete the last line :$ enter D
Delete the current line (line where cursor presents ) D
Replace Satya with Jeevan: ‘%s/satya/jeevan/g’
Display the line numbers: setnum
16. Display all the files which contains “knr”
In current directory
$grep “knr” *
Including subdirectories
$grep -R “knr” *
By ignoring case sensitive
$grep -i “knr” *
Display with line numbers
$grep -n“knr” *
Starting of the file
^
4
UNIX
End of the file $
Number of times match string repeated for each line
grep -in "string" filename|uniq -c
Total number matches in a particular file
$grep –c “knr” k.txt
All lines which doesn’t contains “knr”
$grep –v “knr” k.txt
Display all lines which doesn’t contains “Shahul” either at starting or ending
Display the lines which contains “Phani” in first 50 lines
$head 50 file.txt|grep “^phani”
Display all lines which contains phani or Shahul
Display all the lines which x repeats more than twice.
Grep all the lines a[]b ([] specifies any character )
$grep “a*b” filename.txt
Explain the difference between grep , egrep and fgrep with an example
grep is an acronym that stands for "Global Regular Expressions Print". grep is a program which
scans a specified file or files line by line, returning lines that contain a pattern.
egrep is an acronym that stands for "Extended Global Regular Expressions Print".
The 'E' in egrep means treat the pattern as a regular expression. "Extended Regular
Expressions" abbreviated 'ERE' is enabled in egrep. egrep (which is the same as grep -E)
treats +, ?, |, (, and ) as meta-characters.
fgrep is an acronym that stands for "Fixed-string Global Regular Expressions Print".
fgrep (which is the same as grep -F) is fixed or fast grep and behaves as grep but does NOT
recognize any regular expression meta-characters as being special. The search will complete
faster because it only processes a simple string rather than a complex pattern.
17. Find all
Directories from the directory /home $find /home –type d
Directories only in current directory $find . –type d
Display all empty size files $ find . -type f -size 0M
Find 10 days old directories $ find . –type d –mtime -10
5
UNIX
Find all files from the current directory which are older than 30 days and size >10MB
$ find . –type f –mtime -30 -size 10M
Find all files from the current directory which are older than 30 days and size >10MB and zip
them in one file
Find all files from the current directory which are older than 30 days and size >10MB and
remove them
find . –type f -mtime +30 -size +10M -exec rm -r {} \;
18. How to
Find all process running in system : ps
how to kill a process: kill -9 processid
Userid: id –u username
Servername: Uname -n
Osname: uname
First line of the script
Run a shell script
Redirect one command output to another command
Cat file1.dat file2.dat file3.dat|sort
Redirect output of a command to file
Cat file1.dat file2.dat file3.dat|sort>file4.dat
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.
a. Example:kshtest.ksh a b c
b. Output should be
c. Script name:test.ksh
d. Total arguments passed to script: 3
e. First argument is: a
f. Second argument is: b
g. Third argument is: c
h. Arguments passed to the scripts are: a b c
6
UNIX
20. What is $0, $1, $3, $#, $@, $?
$0 scriptname
$1 first argument
$3 third argument
$# count of arguments
$@ no.of arguments
$? Last execution position
21. For the following data
a. 1|a|b|c
b. 2|x|y
c. 3|x
d. 4|5|6|7
e. 7|8|9|10
First filed
Last field
Number of fields in each line
Display distinct fileds in entire file
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
Copy the file test123.dat from server2:/home/pk1234 to server 1 /home/jk1234/xyz/
a) Copy the file test123.dat from server2:/home/pk1234 to server 1
/home/jk1234/xyz/
scp pk1234@server2:/home/pk1234/test123.dat ~/xyz/test123.dat
Copy the file to xzy.dat from server1 to server2:/home/pk1234
i. scp xyz.dat pk1234@server2:/home/pk1234/
7
UNIX
24. AWK
a) Print only specific field from a file
$ awk '{print $2,$5;}' employee.txt
b) rami_reddy ==>it will display rami reddy
$ awk -F "_" '{print $2,$3}' zc.txt
Comments
Post a Comment