P. 1
UNIX Shell Scripting talk

UNIX Shell Scripting talk

|Views: 142|Likes:
Published by tcdnetsoc
UNIX Shell Scripting talk
UNIX Shell Scripting talk

More info:

Published by: tcdnetsoc on Oct 28, 2011
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as PDF, TXT or read online from Scribd
See more
See less

08/12/2015

pdf

text

original

• tar - tape archive

1 $ tar -cf homebackup.tar /home/stesh # archive my home →
directory
2 $ tar -czf homebackup.tar /home/stesh # same, but with →
compression
3 $ tar -xf homebackup.tar # restore from an archive
4 $ gzip access.log # compress a file
5 $ gzip -9 access.log # highest compression level (between →
1 and 9)
6 $ gunzip access.log.gz # decompress
7 $ zcat access.log.gz # decompress and output to stdout

pv

• ”Pipe viewer”

• Just like cat except it draws a progress bar on stderr

• Monitor the flow of data through a pipe:

1 $ pv backup.tgz | tar x
2 0O 0:00:05 [ 0B/s] [<=>

]

sed and tr

• sed - Stream editor

• modify input line-by-line

• a silly example: replace all the colons in /etc/passwd with
hyphens:

1 $ cat /etc/passwd | sed "s/:/-/g"

• tr - Transliterator

• modify input character-by-character

1 $ cat ls /home | tr '\n' ' ' # replace newlines with →
spaces
2 $ finger stephen | tr -s ' ' # 'squeeze' multiple spaces →
into one

head and tail

• Output the first and last few lines of a file

1 $ man ssh | head
2 $ head -n 5 /etc/shadow # first 5 lines
3 $ last | tail -n 10 # last 10 lines
4 $ tail -f /var/log/userweb.log # watch for new writes

sort

• Sort lines of input

1 $ who | sort
2 $ sort -g myfile # sort numerically
3 $ sort -r myfile # reverse order
4 $ sort -u myfile # don't print duplicates
5 $ df -h | sort -h # sort human-readable quantities (1G, 2K→
, etc.)

shuf

• Shuffle lines of input

1 $ who | sort
2 $ shuf /etc/passwd | head -n 1 | cut -d ':' -f 1 | # a →
random user
3 $ shuf /usr/share/dict/words | head -n 1 # a random word →
from the dictionary

cut

• Tokenize lines of data on a given delimiter

• modify input character-by-character

1 $ cut -d ':' -f 1 /etc/passwd # list the usernames in /etc→
/passwd
2 $ ps -ef | cut -d ' ' -f 2,3,4 # the second, third, and →
forth space-delimited tokens
3 $ cut -c 100 ~/.plan # the first 100 characters

comm, diff, uniq

• comm prints lines common to two files

• diff shows the differences between two files

• uniq shows the unique lines in a file

1 $ ps -ef | cut -d ' ' -f 1 | sort | uniq
2 $ comm /etc/ssh/ssh_config ~/.ssh/config
3 $ diff myfile.txt myfile.txt.old

• comm and diff work on adjacent lines only

• You get unexpected results if the input lines are not sorted

perl

• Perl is a general-purpose, interpreted programming language

• It is used a lot in text processing and system administration

• Very powerful regular expressions

You're Reading a Free Preview

Download
scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->