29
Unit 11 Linux Utilities

11 qlx02vutil

Embed Size (px)

Citation preview

Page 1: 11 qlx02vutil

Unit 11 Linux Utilities

Page 2: 11 qlx02vutil

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

Page 3: 11 qlx02vutil

The find Command

• Search one or more directory structures for files that meet certain specified criteria

• Display the names of matching files or execute commands against those files

Syntax:

$ find path expression

Page 4: 11 qlx02vutil

Sample Directory Structure

blue box big

brown circle giant

green phone little

square small

Page 5: 11 qlx02vutil

Using find

• Generally, you want to search a directory structure for files with certain names and list the names found.

$ CD /HOME/JOE $

FIND . -NAME PHONE

./SHAPE/PHONE

./PHONE

On many other UNIX systems, with find you have to tell it specifically to print the names using -print

$ FIND . -NAME PHONE -PRINT ./SHAPE/PHONE./PHONE

Page 6: 11 qlx02vutil

Executing Commands with find

•The -exec option executes a command on each of the file names found.

$ find . -name 'b*' -exec ls -i {} \;

187787 ./color/blue

187788 ./color/brown 187792 ./shape/box

202083 ./size big 132754 ./blues

" { } " IS A PLACEHOLDER FOR EACH FILENAME. THE BACKSLASH ESCAPES THE FOLLOWING SEMICOLON.

Page 7: 11 qlx02vutil

Interactive Command Execution

•The -ok option also causes command execution but on an interactive 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

Page 8: 11 qlx02vutil

Additional find Options

type

size

mtime

perm

user newer

o a

f d +n -n nc+x -x xonum modeuser ref.file

ordinary file directorylarger than n blocks smaller than n blocks equal to n charactersmodified more than x days ago modified less than x days ago modified x days agoaccess permissions match onum

access permissions match mode

finds files owned by userfile was modified more recentlythan ref.fileLogical ORLogical AND

Page 9: 11 qlx02vutil

find Examples

$ find . -perm

777 ./SIZE/LITTLE

File matches expr 1 and expr 2: $ find . -

name 'S*' -type f -a -size +2\ >-exec ls -i

{} \; 187791 ./SHAPE/SQUARE 202086

./SIZE/SMALL

File matches expr 1 or expr 2: $

find . -name big -o -name 'c*'

./COLOR

./SHAPE/CIRCLE ./SIZE/BIG

Page 10: 11 qlx02vutil

locate Command

locate allows you to quickly find a file on the system, 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 •SuSE does not install locate/updatedb by default

Page 11: 11 qlx02vutil

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)

Page 12: 11 qlx02vutil

cut Example (1)

$ cat /etc/passwd

root:x:0:0:Big Brother:/root:/bin/bash

shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown

tux1:x:500:500::/home/tux1:/bin/bash

tux2:x:501:501::/home/tux2:/bin/bash

$ cut -f1,6,7 -d: /etc/passwd

root:/root:/bin/bash

shutdown:/sbin:/sbin/shutdown

tux1:/home/tux1:/bin/bash

tux2:/home/tux2:/bin/bash

Page 13: 11 qlx02vutil

cut Example (2)

$ ps

PID TTY STAT TIME COMMAND374 p0 S 0:00 -bash

460 p0 R 0:00 ps

$ ps cut -c-5,20-

PID COMMAND

374 -bash

471 ps

Page 14: 11 qlx02vutil

The grep Command

• Searches one or more files or standard input for lines matching pattern

• Simple match or Regular Expression Syntaxgrep [options] pattern [filel ...]

Page 15: 11 qlx02vutil

grep Sample Data Files

• Phone 1:

Allet 10300 intern

Judith 20500 internKees 30500 externLeo 40599 externNannie 50599 externPeter 60300 intern

Phone 2:

Allet 1342 intern

Judith 2083 internKees 3139 externLeo 4200 internNannie 5200 internPeter 6342 extern

Page 16: 11 qlx02vutil

Basic grep

$ grep 20 phonel

Judith 20500 intern

$ grep 20 phone*

phonel: Judith 20500 intern

phone2: Judith 2083 intern

phone2: Leo 4200 intern

phone2: Nannie 5200 intern

$ grep -v Judith phone2

Allet 1342 intern

Kees 3139 extern

Leo 4200 intern

Nannie 5200 intern

Peter 6342 extern

Page 17: 11 qlx02vutil

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 preceding character Any ONE of the characters in the range a

through f Any line that starts with a Any line that ends with z

[a-f]

AA

z$

Page 18: 11 qlx02vutil

Regular Expression

1. Display all the processes that belong to tuxl

Using grep, write commands to select the following lines from the phonel file:

2. Select all the lines of the file (blank and non-blank)

grep_phonel

3. Select all the lines that contain an e and end in a 0

grep_phone 1

Page 19: 11 qlx02vutil

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 making

comparisons

-w Do a whole word search

-f <file> Read expressions from file instead of

command line

Page 20: 11 qlx02vutil

Other greps

• fgrep allows only fixed strings (no regular expressions)

• egrep allows for multiple (alternate) patterns

$ egrep ■20500|40599|50599' PHONE1

Judith 20500 intern

Leo 40599 extern

Nannie 50599 extern

•What does the following command do?

$ grep 30 phone1 | grep intern ????????

Page 21: 11 qlx02vutil

The sort Command

•The sort command sorts the lines in the file specified and writes the result to standard output

sort -t(delimiter) +field -options file

$ cat animals

dog.2

cat.4

penguin.10

$ sort animals

cat.4

dog.2

penguin.10

Page 22: 11 qlx02vutil

sort Examples

$ sort +0.1 animals

cat.4

penguin.10

dog.2

$ sort -t. +1 animals

penguin.10

dog .2

cat.4$ sort -t. -n +1 animals

dog. 2

cat.4

penguin.10

Options:

-d sorts in dictionary order. Only letters, digits and spaces are

considered in comparisons -r reverses the

order of the specified sort -n sorts numeric fields in

arithmetic value

Page 23: 11 qlx02vutil

The head and tail Commands

•The head command can be used to view the first few lines of a 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

Page 24: 11 qlx02vutil

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

a shell 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 a command, use whereis

$ whereis find echo

find: /usr/bin/find /usr/man/man1/find.1

echo: /bin/echo /usr/man/man1/echo.1

Page 25: 11 qlx02vutil

The file Command

•With the file command, you can find out what the type of data in the 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

Page 26: 11 qlx02vutil

The gzip, gunzip and zcat Commands

To 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 file1

file1: 89.9% -- replaced with file1.gz

$ ls -l file1.gz

-rw-rw-r-- 1 team01 team01 3265 Apr 6 23:40 file1.gz $ zcat

file1

(output is the same as the output of the cat command with the

uncompressed file) $ gunzip file1 $ ls -l file1

-rw-rw-r-- 1 team01 team01 32031 Apr 6 23:40 file1

Page 27: 11 qlx02vutil

The join and paste Commands

join and paste combine files

$ cat one a apple another b bee

beast $ cat two a ape b broken $

join one two a apple another ape

b bee beast broken $ paste one

two a apple another a ape b bee

beast b broken

Page 28: 11 qlx02vutil

Checkpoint

T/F 1. The command ps -aux | grep tux | grep netscape

lists all Netscape processes of a user named tux.

2. Which command would best be used to locate all files in your

system 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

Page 29: 11 qlx02vutil

Unit Summary

The following commands were considered:

•The find command is used to recursively search directories for files with particular characteristics

•The grep command is used to select entire lines containing a particular pattern

•The head and tail commands are used to view specific lines in a file

•The sort command sorts the contents of a file by the options specified

• Find out where you can find commands with type, where and whereis

•The gzip, zcat and gunzip commands can be used to create and work with compressed files