chapter 3 input output redirection. www.pnj.ac.id 2 linux command category stdout/stdin/stderr,...

Post on 19-Jan-2018

228 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

DESCRIPTION

3 Linux Command Category

TRANSCRIPT

Chapter 3Input Output &

Redirection

Www.pnj.ac.id 2

Linux Command Category Stdout/Stdin/Stderr, Pipe and

Redirection, Wildcards Linux Command Review Question and Exercise

Outline

Www.pnj.ac.id 3

Linux Command Category

Www.pnj.ac.id 4

Communicationssh scp

File/Directory Managementcat cd chmod cp ln ls mkdir more less mv pwd dirs rm

head tail wc Comparisons

diff

Linux Command Category

Www.pnj.ac.id 5

Searchinggrep find locate

Archivingcompress uncompress gzip

gunzip zcat tar Text Processing

cut paste sort sed awk uniq

Linux Command Category Cont’d

Www.pnj.ac.id 6

System Statuschgrp chown date df du env

who w uptime Miscellaneous

bc cal clear man

Linux Command Category Cont’d

Www.pnj.ac.id 7

Stdout/Stdin/StderrPipe and Redirection

Wildcards

Www.pnj.ac.id 8

Output from commands • usually written to the screen• referred to as standard output (stdout)

Input for commands• usually come from the keyboard (if no

arguments are given• referred to as standard input (stdin)

Error messages from processes• usually written to the screen• referred to as standard error (stderr)

stdout stdin stderr

Www.pnj.ac.id 9

Pipe (|): stdout of one command to stdin of another command

Output Redirection (> or 1>): stdout of a command to a file

Output Appending (>>): stdout of a command appending to a file

Input Redirection (< or 0<): stdin of a command from a file

Pipe and Redirection

Www.pnj.ac.id 10

For tcsh&> filename

For bash2>&1 filename

Stderr Redirection

Www.pnj.ac.id 11

Multiple filenames can be specified using special pattern-matching characters. The rules are: • ‘?’ matches any single character in that

position in the filename• ‘*’ matches zero or more characters in the

filename. • ‘[…]’ Characters enclosed in square brackets

match any name that has one of those characters in that position

Note that the UNIX shell performs these expansions before the command is executed.

Wildcards

Www.pnj.ac.id 12

Linux Command Review

Www.pnj.ac.id 13

Log on to remote machine from Linux

ssh cdpoon@killdevil.unc.edussh killdevil.unc.edu –l cdpoon

ssh killdevil

ssh –X kure.unc.edussh –Y kure.unc.edu

ssh

Www.pnj.ac.id 14

Using ssh, login to killdevil.unc.eduTo start ssh using SecureCRT in

Windows, do the following.• Start -> Programs -> Remote Services ->

SecureCRT• Click the Quick Connect icon at the top.• Hostname: killdevil.unc.edu• Login with your ONYEN and password

ssh using SecureCRTin Windows

Www.pnj.ac.id 15

Read one or more files and print the on stdoutcat file1cat file1 file2 file3 > file_all

cat file4 >> file_allAppend file4 to file_all

cat > file5 Create file at stdin, end with EOF (cntl-d normally, use “stty –a” to find

out)

cat > file6 << STOPCreate file at stdin, end with STOP

cat

Www.pnj.ac.id 16

Change directory, build-in shell commandcd /nas02/home/c/d/cdpoon

cd ../../ Change directory to 2 levels upcd .. Change directory to 1 level up

cd ~ Change directory to Home cd Change directory to Home

cd – Change to previous directory

cd

Www.pnj.ac.id 17

Change the access mode of one or more fileschmod u+x file1chmod go-w file2

chmod u=rwx, g=rx, o=x file3chmod 751 file3 Same as above, 7=rwx, 5=rx, 1=x

chmod =r file4chmod 444 file4 Same as above, 4=r, 2=w, 1=x

chmod

Www.pnj.ac.id 18

Copy a file/dir to another file/dircp file1 file2 Copy to the same directory and change filenamecp file1 ../dir/file2 Copy to different directory and change filenamecp file1 ../dir/. Keep the same filename

cp –r dir1 dir2 Copy directory recursivelycp –r dir1 new_dir/dir2

Copy directory recursively to another directory cp –p file3 file4 Preserve the modification time and permission modes

cp

Www.pnj.ac.id 19

Create links for file/dir and allow them to be accessed by different names

ln file1 file2 Hard link for fileln dir1 dir2 Hard link not allowed for directory

ln –s dir1 dir2 Soft link for directory, dir2 -> dir1ln –s file3 file4 Soft link, file4 -> file3ln –s dir/file5 file6 Soft link, file6 -> dir/file5

ln

Www.pnj.ac.id 20

List all files and directories in the current directory

lsls –a List files/directories starting with “.” tools –l Long listing

ls –lh List file sizes in human readable format and long list formatls –F Flag filenames by appending / to directories, * to executables files,

and @ to symbolic links

ls

Www.pnj.ac.id 21

Create one of more directories

mkdir dir1

mkdir –p dir1/dir2/dir3

Create intervening parent directories if they don’t existSame as mkdir dir1; cd dir1; mkdir dir2; cd dir2; mkdir dir3;

cd ../../

mkdir

Www.pnj.ac.id 22

Display files on a terminal, one screen at a timemore file1 Hit space bar for another page, q to quit

more –d file2 Display the prompt “Press space to continue, ‘q’ to quitmore –c file3 Page through the file by clearing each window instead of

scrolling

more

Www.pnj.ac.id 23

Works like “more” but allows backward and forward movement

less file1 Hit space bar for another page, q to quit

Hit b to scroll backward one windowHit /pattern to highlight “pattern” in the textHit Return to scroll one line at a time

less

Www.pnj.ac.id 24

Move files and directories within the same machine and/or rename themmv file1 dir1/file1 Move file1 to dir1, Same as mv file1 dir1/mv file3 file4 Rename file3 to file4mv dir1 dir2 Rename directory dir1 to dir2

mv dir3 dir4/dir5/dir6Rename directory dir3 to dir6 and move

to dir4/dir5 directory

mv

Www.pnj.ac.id 25

Print the full pathname of the current directory

pwd

dirs C shell and bash shell built-in command, works like “pwd”dirs –l Print working directory in long listing

pwd dirs

Www.pnj.ac.id 26

Delete one or more files and directoriesDelete empty directory with “rmdir”

rm file1

rm file* Remove all files with filename starting as “file”

rm –i file* Prompt for y (remove the file) or n (do not remove the file)

rm –r dir1 Delete directory “dir1” and its content

rm

Www.pnj.ac.id 27

Print first/last few lines of one or more fileshead file1 Print the first 10 lines of file “file1”

head –n100 file2 Print the first 100 lines of file “file2”

tail file* Print the last 10 lines of files with filename starting as “file”

tail –f file3 Print the last 10 lines of file “file3” and follow file as it grows

head tail

Www.pnj.ac.id 28

Print a character, word, and line count for files

wc –c file1 Print character count for file “file1”

wc –l file2 Print line count for file “file2”

wc –w file3 Print word count for file “file3”

wc

Www.pnj.ac.id 29

Report lines that differ between file1 and file2, with file1 text flagged by < and file2 by >

diff file1 file2 Show difference between file1 and file2

diff

Www.pnj.ac.id 30

Search for lines that match a regular expression

grep abc file1Print line(s) in file “file1” with “abc”

grep –i abc file2Print line(s) in file “file2” with “abc” ignoring uppercase and lowercase

distinctions

grep

Www.pnj.ac.id 31

Find particular groups of filesfind . –name temp Find file named “temp” in current directory

find /etc –name ‘rc*’ Find file(s) in /etc directory with name starting with “rc”

find /usr/share/man –type d –name ‘man*’

Find directories in /usr/share/man with name starting with “man”

find

Www.pnj.ac.id 32

Find files with matching pattern in database prepared by updatedb, Database needed to be updated daily

locate which Find files named with pattern “which” in the OS

locate –c which Count number of files named with pattern “which” in the OS

locate –i which Find files named with pattern “which” in the OS ignoring case distinctions

locate

Www.pnj.ac.id 33

Reduce or expand the size of one or more files using adaptive Lempel-Ziv coding

Use uncompress to expand data

compress file1 Reduce the size of file1 and create new file named file1.Zcompress –f file2 Force to reduce the size of file2 and create new file named file2.Z

uncompress file3.Z Expand file3.Z and restore file3

compress uncompress

Www.pnj.ac.id 34

Reduce or expand the size of one or more files using Lempel-Ziv coding (LZ77)

Use gunzip to expand data

gzip file1 Reduce the size of file1 and create new file named file1.gz

gzip –f file2 Force to reduce the size of file2 and create new file named file2.gz

gunzip file3.gz Expand file3.gz and restore file3

gzip gunzip

Www.pnj.ac.id 35

Expand the size of one or more files created by compress or gunzip

List file contents to stdout without deleting the .Z or .gz file

zcat file1.Z Expand file1.Z and list the content of file1 in stdout

zcat file2.gz Expand file2.gz and list the content of file2 in stdout

zcat

Www.pnj.ac.id 36

Archive files and directoriesCreate a single file with extension .tar

tar –cvf file123.tar file1 file2 file3Create archive file named file123.tar in verbose mode

with contents, file1, file2, and file3

tar –xvf file123.tar

Expand file123.tar in verbose mode and generate the original files and directories back

tar

Www.pnj.ac.id 37

Remove sections from each line of filescut –d: -f1,5 /etc/passwd

Use field delimiter “:” to locate fields 1 and 5 from file /etc/passwd to extract usernames and real names

cut –c4 file1Take character 4 out from each line of file1 and

display in stdout

cut

Www.pnj.ac.id 38

Merge lines of files

$ cat file112

$ cat file2abc

paste

$ paste file1 file21 a2 b c

$ paste –s file1 file21 2a b c

Www.pnj.ac.id 39

Sort lines of text files

sort –fd file1Alphabetize lines (-d) in file1 and ignore lower and upper cases (-f)

sort –t: -k3 -n /etc/passwdTake column 3 of file /etc/passwd separated by “:” and sort in

arithmetic order

sort

Www.pnj.ac.id 40

Edit one or more files without user interaction using stream editor

sed s/xx/yy/g file1Substitude all occurrences of “xx” in file1 with “yy” and display on

stdout

sed /abc/d file1 Delete all lines containing “abc” in file1

sed /BEGIN/,/END/s/abc/123/g file1Substitute “XYZ” on lines between BEGIN and END with “xyz”

in file1

sed

Www.pnj.ac.id 41

Process files by pattern-matchingawk –F: ‘{print $1}’ /etc/passwd

Extract the 1st field separated by “:” in /etc/passwd and print to stdout

awk ‘/abcde/’ file1Print all lines containing “abcde” in file1

awk ‘/xyz/{++i}; END{print i}’ file2Find pattern “xyz” in file2 and count the number

awk ‘length <= 1’ file3Display lines in file3 with only 1 or no character

awk

Www.pnj.ac.id 42

Report or omit repeated lines

uniq file1Filter adjacent matching lines from file named file1 , writing to stdout

uniq –c file1Prefix lines by the number of occurrences from file named file1

uniq

Www.pnj.ac.id 43

Change the group ownership of one or more files or directories

chgrp employee file1Change group ownership to “employee” for file “file1”

chgrp –R student dir1Change group ownership to “student” for directory “dir1” including

subdirectories recursively

chgrp

Www.pnj.ac.id 44

Change the ownership of one or more files or directories

chown employee file1Change ownership to “employee” for file “file1”

chown –R student dir1Change ownership to “student” for directory “dir1” including

subdirectories recursively

chown

Www.pnj.ac.id 45

Print the current date and time in certain formatSet the current date and time

datePrint the current date and time

date +%DPrint the current date and time in mm/dd/yy format

date 1201160108Set the current date and time to Dec 01 4:01pm 2008

date –d friShow the date of the coming Friday

date

Www.pnj.ac.id 46

Report the number of used and free disk block on all mounted file systemsdf

Print used and free disk block on all mounted file system

df -hPrint used and free disk block in human readable format

df -kPrint used and free disk block in kb

df

Www.pnj.ac.id 47

Print disk usage of directories and its subdirectoriesdu dir1 Print disk usage in kilobyte of directory “dir1”

du –-block-size=1M dir2Print disk usage in megabyte of directory “dir2”

du –hs dir3 Print summarized disk usage in human-readable format of directory “dir3”

du

Www.pnj.ac.id 48

Display the current environment variables or set new values

env Display all of the current environment variables

env

Www.pnj.ac.id 49

Display information about the current status of the system

who Display the names of users currently logged in to the system

who –b Report information about the last reboot

who am I Print the username of the invoking user

who

Www.pnj.ac.id 50

Print summaries of system usage, currently logged-in users, and what they are doing

w Print summaries of system usage, currently logged-in users

w –s Display in short form

w

Www.pnj.ac.id 51

Print the current time, amount of time logged in, and the system load averagesuptime Print a one line display of the current time, how long the system has been running, how many users are currently logged on, and the system load averages for the past 1, 5, 15 minutes

uptime

Www.pnj.ac.id 52

Interactively perform arbitrary-precision arithmetic or convert numbers from one base to another, type “quit” to exitbc Invoke bc1+2 Evaluate an addition5*6/7 Evaluate a multiplication and divisionibase=8 Change to octal input20 Evaluate this octal number16 Output decimal valueibase=10 Change back to decimal input

bc

Www.pnj.ac.id 53

Print calendar of a month or all months in a yearcal Print calendar of the current month

cal 2 2009 Print calendar of February 2009

cal 2009 Print calendar of all months in 2009

cal -3 Display previous/current/next months

cal

Www.pnj.ac.id 54

Clear the terminal display and have the prompt locate at the top of the terminal window

clear Clean up the current terminal display

clear

Www.pnj.ac.id 55

Display information from the online reference manuals

man man Display the manual for the command “man”

man –k link compile Display commands related to linking and compiling using a keyword search

man

Www.pnj.ac.id 56

Tips and Tricks

Www.pnj.ac.id 57

Show files changed on a certain date in all directories

ls –l * | grep ‘Sep 26’

Show long listing of file(s) modified on Sep 26

ls –lt * | grep ‘Dec 18’ | awk ‘{print $9}’

Show only the filename(s) of file(s) modifed on Dec 18

Tips and Tricks #1

Www.pnj.ac.id 58

Sort files and directories from smallest to biggest or the other way around

du –k –s * | sort –n

Sort files and directories from smallest to biggest

du –ks * | sort –nr

Sort files and directories from biggest to smallest

Tips and Tricks #2

Www.pnj.ac.id 59

Change timestamp of a file

touch file1

If file “file1” does not exist, create it, if it does, change the timestamp of it

touch –t 200902111200 file2

Change the time stamp of file “file2” to 2/11/2009 12:00

Tips and Tricks #3

Www.pnj.ac.id 60

Find out what is using memory

ps –ely | awk ‘{print $8,$13}’ | sort –k1 –nr | more

Tips and Tricks #4

Www.pnj.ac.id 61

Remove the content of a file without eliminating it

cat /dev/null > file1

Tips and Tricks #5

Www.pnj.ac.id 62

Backup selective files in a directory

ls –a > backup.filelistCreate a file list

vi backup.filelistAdjust file “backup.filelist” to leave only filenames of the files to be

backuptar –cvf archive.tar `cat backup.filelist`

Create tar archive “archive.tar”, use backtics in the “cat” command

Tips and Tricks #6

Www.pnj.ac.id 63

Get screen shots

xwd –out screen_shot.wd

Invoke X utility “xwd”, click on a window to save the image as “screen_shot.wd”display screen_shot.wd

Use ImageMagick command “display” to view the image “screen_shot.wd”

Right click on the mouse to bring up menu, select “Save” to save the image to other formats, such as jpg.

Tips and Tricks #7

Www.pnj.ac.id 64

Sleep for 5 minutes, then pop up a message “Wake Up”

(sleep 300; xmessage –near Wake Up) &

Tips and Tricks #8

Www.pnj.ac.id 65

Count number of lines in a file

cat /etc/passwd > temp; cat temp | wc –l; rm temp

wc –l /etc/passwd

Tips and Tricks #9

Www.pnj.ac.id 66

Create gzipped tar archive for some files in a directory

find . –name ‘*.txt’ | tar –c –T - | gzip > a.tar.gz

find . –name ‘*.txt’ | tar –cz –T - -f a.tar.gz

Tips and Tricks #10

Www.pnj.ac.id 67

Find name and version of Linux distribution, obtain kernel level

uname -ahead –n1 /etc/issue

Tips and Tricks #11

Www.pnj.ac.id 68

Show system last reboot

last reboot | head –n1

who -b

Tips and Tricks #12

Www.pnj.ac.id 69

Combine multiple text files into a single file

cat file1 file2 file3 > file123

cat file1 file2 file3 >> old_file

cat `find . –name ‘*.out’` > file.all.out

Tips and Tricks #13

Www.pnj.ac.id 70

Create man page in pdf format

man –t man | ps2pdf - > man.pdf

acroread man.pdf

Tips and Tricks #14

Www.pnj.ac.id 71

Remove empty line(s) from a text file

awk ‘NF>0’ < file.txt

Print out the line(s) if the number of fields (NF) in a line in file

“file.txt” is greater than zeroawk ‘NF>0’ < file.txt > new_file.txt

Write out the line(s) to file “new_file.txt if the number of fields (NF)

in a line in file “file.txt” is greater than zero

Tips and Tricks #15

Www.pnj.ac.id 72

Conclusion

Www.pnj.ac.id 73

Many ways to do a certain thing Unlimited possibilities to combine

commands with |, >, <, and >> Even more powerful to put commands in

shell script

Conclusion

Www.pnj.ac.id 74

Questions ?

Exercise

top related