Your SlideShare is downloading. ×
11 qlx02vutil
Upcoming SlideShare
Loading in...5

Thanks for flagging this SlideShare!

Oops! An error has occurred.

Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

11 qlx02vutil


Published on

Published in: Technology

  • Be the first to comment

  • Be the first to like this

No Downloads
Total Views
On Slideshare
From Embeds
Number of Embeds
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

No notes for slide


  • 1. Unit 11Linux Utilities
  • 2. Objectives_After completing this unit, you should be able to:• Use the find and locate command to search for files• Use the cut command to list specific columns of a file• Use the grep command to search text files for patterns• Use the head and tail commands to view specific lines in a file• Use the sort command to sort the contents of a file• Use the type, which and whereis commands to find commands• Use the file command to find out the content of a file• Use the join and paste commands to combine files• Manipulate files with gzip, gunzip and zcat
  • 3. The find Command• Search one or more directory structures for files thatmeet certain specified criteria• Display the names of matching files or executecommands against those filesSyntax:$ find path expression
  • 4. Sample Directory Structureblue box bigbrown circle giantgreen phone littlesquare small
  • 5. Using find• Generally, you want to search a directory structure for fileswith certain names and list the names found.$ CD /HOME/JOE $ FIND . -NAME PHONE ./SHAPE/PHONE./PHONEOn many other UNIX systems, with find you have to tell itspecifically to print the names using -print$ FIND . -NAME PHONE -PRINT ./SHAPE/PHONE./PHONE
  • 6. Executing Commands with find•The -exec option executes a command on each of the file namesfound.$ find . -name b* -exec ls -i {} ;187787 ./color/blue187788 ./color/brown 187792 ./shape/box202083 ./size big 132754 ./blues" { } " IS A PLACEHOLDER FOR EACH FILENAME. THE BACKSLASHESCAPES THE FOLLOWING SEMICOLON.
  • 7. Interactive Command Execution•The -ok option also causes command execution but on aninteractive basis:$ find . -NAME B* -OK RM {} ;< RM .. ./COLOR/BLUE > ? Y< RM .. ./color/brown > ? y< rm .. ./SHAPE/BOX > ? Y< rm .. ./size/big > ? y< rm .. ./blues > ? y
  • 8. Additional find Optionstypesizemtimepermusernewero afd+n-nnc+x-xxonummodeuserordinary file directorylarger than n blockssmaller than n blocksequal to n charactersmodified more than x days agomodified less than x days agomodified x days agoaccess permissions match onumaccess permissions match modefinds files owned by userfile was modified more recentlythan ref.fileLogical ORLogical AND
  • 9. find Examples$ find . -perm 777./SIZE/LITTLEFile matches expr 1 and expr 2: $ find . -name S* -type f -a -size +2 >-exec ls -i{} ; 187791 ./SHAPE/SQUARE 202086 ./SIZE/SMALLFile matches expr 1 or expr 2: $find . -name big -o -name c*./COLOR./SHAPE/CIRCLE ./SIZE/BIG
  • 10. locate Commandlocate allows you to quickly find a file on thesystem, based on simple criteria$ locate passwd/USR/SHARE/MAN/MAN1/PASSWD.1.GZ/USR/SHARE/MAN/MAN5/PASSWD.5.GZ/ETC/PASSWD/USR/BIN/PASSWD• Requires that the superuser runs updatedb regularly •Most distributions run updatedb automatically •SuSEdoes not install locate/updatedb by default
  • 11. The cut Command• Pull selected columns or fields from one or more files.• Syntax:cut -f(ields) -d(elimiter) file(s) cut -c(haracters) file(s)
  • 12. cut Example (1)$ cat /etc/passwdroot:x:0:0:Big Brother:/root:/bin/bashshutdown:x:6:0:shutdown:/sbin:/sbin/shutdowntux1:x:500:500::/home/tux1:/bin/bashtux2:x:501:501::/home/tux2:/bin/bash$ cut -f1,6,7 -d: /etc/passwdroot:/root:/bin/bashshutdown:/sbin:/sbin/shutdowntux1:/home/tux1:/bin/bashtux2:/home/tux2:/bin/bash
  • 13. cut Example (2)$ psPID TTY STAT TIME COMMAND374 p0 S 0:00 -bash460 p0 R 0:00 ps$ ps cut -c-5,20-PID COMMAND374 -bash471 ps
  • 14. The grep Command• Searches one or more files or standard input for linesmatching pattern• Simple match or Regular Expression Syntaxgrep [options] pattern [filel ...]
  • 15. grep Sample Data Files• Phone 1:Allet 10300 internJudith 20500 internKees 30500 externLeo 40599 externNannie 50599 externPeter 60300 internPhone 2:Allet 1342 internJudith 2083 internKees 3139 externLeo 4200 internNannie 5200 internPeter 6342 extern
  • 16. Basic grep$ grep 20 phonelJudith 20500 intern$ grep 20 phone*phonel: Judith 20500 internphone2: Judith 2083 internphone2: Leo 4200 internphone2: Nannie 5200 intern$ grep -v Judith phone2Allet 1342 internKees 3139 externLeo 4200 internNannie 5200 internPeter 6342 extern
  • 17. grep with Regular Expressions• Patterns with metacharacters should be in single quotes ( )so that the shell will leave it alone*Valid metacharacters with grep: $ . * A [ - ] .ANY SINGLE CHARACTERZero or more occurrences of the precedingcharacter Any ONE of the characters in therange a through f Any line that starts witha Any line that ends with z[a-f]AAz$
  • 18. Regular Expression1. Display all the processes that belong to tuxlUsing grep, write commands to select the following linesfrom the phonel file:2. Select all the lines of the file (blank and non-blank)grep_phonel3. Select all the lines that contain an e and end in a 0grep_phone 1
  • 19. grep Options-v Print lines that do not match-c Print only a count of matching lines-l Print only the names of the files withmatching lines-n Number the matching lines-i Ignore the case of letters when makingcomparisons-w Do a whole word search-f <file> Read expressions from file instead ofcommand line
  • 20. Other greps• fgrep allows only fixed strings (no regular expressions)• egrep allows for multiple (alternate) patterns$ egrep ■20500|40599|50599 PHONE1Judith 20500 internLeo 40599 externNannie 50599 extern•What does the following command do?$ grep 30 phone1 | grep intern????????
  • 21. The sort Command•The sort command sorts the lines in the file specified and writesthe result to standard outputsort -t(delimiter) +field -options file$ cat animalsdog.2cat.4penguin.10$ sort animalscat.4dog.2penguin.10
  • 22. sort Examples$ sort +0.1 animalscat.4penguin.10dog.2$ sort -t. +1 animalspenguin.10dog .2cat.4$ sort -t. -n +1 animalsdog. 2cat.4penguin.10Options:-d sorts in dictionary order. Only letters, digits and spaces areconsidered in comparisons -r reverses theorder of the specified sort -n sorts numeric fields inarithmetic value
  • 23. The head and tail Commands•The head command can be used to view the first few lines ofa file or files. The command syntax is: $ head [-lines] file(s)$ HEAD -5 MYFILE $ LS -L | HEAD -12•The tail command displays the last few lines of a file or files.The command syntax is: $ tail [{-lines|+lines|-f}] file(s)$ tail -20 file$ tail +20 file$ tail -f file
  • 24. The type, which and whereis Commands•To find out what the path to a command is, use type$ type find echo find is/usr/bin/find echo is ashell builtin•To find out where the binary is located, use which$ which find echo/USR/BIN/FIND/BIN/ECHO•To locate the binary, source and manual page files of acommand, use whereis$ whereis find echofind: /usr/bin/find /usr/man/man1/find.1echo: /bin/echo /usr/man/man1/echo.1
  • 25. The file Command•With the file command, you can find out what the type of data inthe file is.$ FILE /ETC/PASSWD /BIN/LS /HOME/PETER /TMP/FAKE.JPG /ETC/PASSWD:ASCII TEXT/BIN/LS: ELF 32-BIT LSB EXECUTABLE, INTEL 80386,version 1, dynamically linked, stripped /home/peter:directory/TMP/FAKE.JPG: PDF DOCUMENT, VERSION 1.3
  • 26. The gzip, gunzip and zcat CommandsTo compress or uncompress files use gzip, gunzip or zcat$ ls -l file1-rw-rw-r-- 1 team01 team01 32031 Apr 6 23:40 file1 $gzip -v file1file1: 89.9% -- replaced with file1.gz$ ls -l file1.gz-rw-rw-r-- 1 team01 team01 3265 Apr 6 23:40 file1.gz $ zcatfile1(output is the same as the output of the cat command with theuncompressed file) $ gunzip file1 $ ls -l file1-rw-rw-r-- 1 team01 team01 32031 Apr 6 23:40 file1
  • 27. The join and paste Commandsjoin and paste combine files$ cat one a apple another b beebeast $ cat two a ape b broken $join one two a apple another apeb bee beast broken $ paste onetwo a apple another a ape b beebeast b broken
  • 28. CheckpointT/F 1. The command ps -aux | grep tux | grep netscapelists all Netscape processes of a user named tux.2. Which command would best be used to locate all files in yoursystem that begin with the string "team"?a. find / -name "Ateam"b. find / -name "team*"c. find / -name "*team*"d. find / -type f -name "team"3. Translate the following command into your native language:LS -LR | EGREP "TXT$|TAB$"| SORT -RN +4 | TAIL +4 | HEAD -5
  • 29. Unit SummaryThe following commands were considered:•The find command is used to recursively searchdirectories for files with particular characteristics•The grep command is used to select entire linescontaining a particular pattern•The head and tail commands are used to view specific linesin a file•The sort command sorts the contents of a file by the optionsspecified• Find out where you can find commands with type, where andwhereis•The gzip, zcat and gunzip commands can be used tocreate and work with compressed files