Date post: | 25-Dec-2015 |
Category: |
Documents |
Upload: | dominic-hampton |
View: | 216 times |
Download: | 2 times |
BILKENT UNIVERSITYDEPARTMENT OF COMPUTER TECHNOLOGY AND
INFORMATION SYSTEMS
CTIS156 INFORMATION TECHNOLOGIES II
CHAPTER 10:
ADVANCED FILE PROCESSING
CH.10 ADVANCED FILE PROCESSING
● Objectives:– To explain how file compression is performed in Linux
– To discuss searching for commands and files in Linux file structure
– To describe searching files for expressions, strings, and patterns
– To explain command history
– To describe input/output redirection
– To explain Linux pipes
2 CTIS156 INFORMATION TECHNOLOGIES II
CH.10 ADVANCED FILE PROCESSING
● 10.3 Compressing Files– Reducing the size of files:
● To save disk space● To save time when transmitting over the internet● To save time during file save
– Linux provides several commands to compress/decompress files:
● gzip command is used to compress files● gunzip command is used to decompress files
3 CTIS156 INFORMATION TECHNOLOGIES II
CH.10 ADVANCED FILE PROCESSING
● 10.3 Compressing Files (cont)– gzip [options] [file-list]
● It saves the compressed file with a .gz extension● -f option forces compression even though .gz file
already exists● -r option recursively compresses files in the
subdirectories● -l option displays a list of compressed files,
compressed and uncompressed file sizes, compression ratio, etc.
4 CTIS156 INFORMATION TECHNOLOGIES II
CH.10 ADVANCED FILE PROCESSING
● 10.3 Compressing Files (cont)– gunzip [options] filename
● Uncompresses the compressed files.
5 CTIS156 INFORMATION TECHNOLOGIES II
CH.10 ADVANCED FILE PROCESSING
6 CTIS156 INFORMATION TECHNOLOGIES II
● 10.4 Sorting Files– Order set of items according to some criteria.– The sorting process is based on using a field, or portion
of each item, known as sort key.– The Linux sort utility can be used to sort items in text
(ASCII) files.– By default, the sort utility takes each line, starting with
the first column to be the key.
CH.10 ADVANCED FILE PROCESSING
7 CTIS156 INFORMATION TECHNOLOGIES II
● 10.4 Sorting Files (cont)
[vural@vpdesktop vural]$ cat studentsJohn Johnsen [email protected] 503.555.1111Hassaan Sarwar [email protected] 503.444.2132David Kendal [email protected] 229.111.2013Kelly Kimberly [email protected] 555.123.9999Maham Sarwar [email protected] 713.888.0000
CH.10 ADVANCED FILE PROCESSING
8 CTIS156 INFORMATION TECHNOLOGIES II
● 10.4 Sorting Files (cont)
[vural@vpdesktop vural]$ sort studentsDavid Kendal [email protected] 229.111.2013Hassaan Sarwar [email protected] 503.444.2132John Johnsen [email protected] 503.555.1111Kelly Kimberly [email protected] 555.123.9999Maham Sarwar [email protected] 713.888.0000
CH.10 ADVANCED FILE PROCESSING
9 CTIS156 INFORMATION TECHNOLOGIES II
● 10.4 Sorting Files (cont)
[vural@vpdesktop vural]$ sort +1 studentsJohn Johnsen [email protected] 503.555.1111David Kendal [email protected] 229.111.2013Kelly Kimberly [email protected] 555.123.9999Hassaan Sarwar [email protected] 503.444.2132Maham Sarwar [email protected] 713.888.0000
CH.10 ADVANCED FILE PROCESSING
10 CTIS156 INFORMATION TECHNOLOGIES II
● 10.4 Sorting Files (cont)
[vural@vpdesktop vural]$ sort +3 studentsDavid Kendal [email protected] 229.111.2013Hassaan Sarwar [email protected] 503.444.2132John Johnsen [email protected] 503.555.1111Kelly Kimberly [email protected] 555.123.9999Maham Sarwar [email protected] 713.888.0000
CH.10 ADVANCED FILE PROCESSING
11 CTIS156 INFORMATION TECHNOLOGIES II
● 10.4 Sorting Files (cont)
[vural@vpdesktop vural]$ sort -r studentsMaham Sarwar [email protected] 713.888.0000Kelly Kimberly [email protected] 555.123.9999John Johnsen [email protected] 503.555.1111Hassaan Sarwar [email protected] 503.444.2132David Kendal [email protected] 229.111.2013
CH.10 ADVANCED FILE PROCESSING
● Searching For Commands and Files:– find directory-list expression
● Searches for the files that match the criteria defined by the
expression in the directory-list
● Searches the directories recursively
● Expression examples:
– -name pattern (for files that match pattern)
– -newer file (for files newer than the file)
– -user username (for files owned by a specific user)
– -print (displays the pathnames of the files found)
12 CTIS156 INFORMATION TECHNOLOGIES II
CH.10 ADVANCED FILE PROCESSING
● Searching For Commands and Files (cont):– Examples of find command:
● find ~ -name vural.c –print● find / -name profile –print● find /usr –name socket.h –print● find . –user ctis -print
13 CTIS156 INFORMATION TECHNOLOGIES II
CH.10 ADVANCED FILE PROCESSING
● Searching For Commands and Files (cont):– whereis [options] [file-list]
● It searches and displays the location of a command’s files (executable, source and man pages)
● Example:
– whereis ls
– which [command-list]● If there are more than one version of a command file, then it
displays the absolute pathname of the command executed by the shell.
14 CTIS156 INFORMATION TECHNOLOGIES II
CH.10 ADVANCED FILE PROCESSING
● Searching a text in the text files:– grep [options] pattern [file-list]
● It searches the pattern (a string) inside the text files in the file-list and if finds, displays the lines that has
● -n (display the line numbers in the output)
● -l (display only the filenames that contain the string)
● Examples:
– grep umask /etc/profile
– grep include *.c
15 CTIS156 INFORMATION TECHNOLOGIES II
CH.10 ADVANCED FILE PROCESSING
● Command History:– history [options] [filename]
● Displays the command history
● The command history is stored in a file ~/.bash_history
● If a number is specified as an option, the command displays only the last number of commands
● !! (executes the last command in the history list)
● !n (executes the command in line n of the history list)
16 CTIS156 INFORMATION TECHNOLOGIES II
CH.12 REDIRECTION AND PIPING
● 12.1 Introduction:– Most commands and programs perform the following
operations:● Input● Processing● Output
– Questions:● Where does a program take it's input?● Where does it send it's output● Where are the error messages displayed?
17 CTIS156 INFORMATION TECHNOLOGIES II
CH.12 REDIRECTION AND PIPING
● 12.2 Standard Files:– By default, three standard files are opened automatically by
the kernel for every command or program executed:● stdin (keyboard): Where it takes input● stdout (screen): Where it sends output● stderr (screen): Where it sends the error messages
18 CTIS156 INFORMATION TECHNOLOGIES II
CH.12 REDIRECTION AND PIPING
● 12.3 Input Redirection:– The < (less than) symbol is used to change the standard
input. The syntax is:
– command < input-file– The command takes its input from another file, instead of stdin (keyboard)
19 CTIS156 INFORMATION TECHNOLOGIES II
CH.12 REDIRECTION AND PIPING
● Input Redirection example:
- bc is a calculator program in Unix, takes the input form the keyboard by default.
- We can write the input to a file and use input redirection$ bc2+24$ cat bc_input2+2$ bc < bc_input4
20 CTIS156 INFORMATION TECHNOLOGIES II
CH.12 REDIRECTION AND PIPING
● 12.4 Output Redirection:– The > (greater than) symbol is used to change the standard
output. The syntax is:
– command > output-file– The command write its output to an external file instead of
the standard output (screen)
21 CTIS156 INFORMATION TECHNOLOGIES II
CH.12 REDIRECTION AND PIPING
● Output Redirection example:$ ls -l > ls.file$ cat ls.filetotal 30drwx--x--x 8 vpolat ctisac 1536 Apr 20 10:55 .drwxr-xr-x 29 root other 512 Feb 2 12:07 ..-rw-r--r-- 1 vpolat ctisac 53 Feb 28 10:57 .profile-rw------- 1 vpolat ctisac 2748 Apr 20 10:55 .sh_historydrwxr-xr-x 2 vpolat ctisac 512 Apr 16 2004 _fpclassdrwxr-xr-x 3 vpolat ctisac 512 Sep 23 2004 _privatedrwxr-xr-x 3 vpolat ctisac 512 Apr 16 2004 _themes-rw-r--r-- 1 vpolat ctisac 4 Apr 20 10:44 bc_inputdrwxr-xr-x 2 vpolat ctisac 512 Sep 12 2005 images-rw-r--r-- 1 vpolat ctisac 0 Apr 20 10:55 ls.filedrwx------ 2 vpolat ctisac 512 Jul 20 2004 maildrwxr-xr-x 7 vpolat ctisac 1536 Mar 23 09:30 public_html
22 CTIS156 INFORMATION TECHNOLOGIES II
CH.12 REDIRECTION AND PIPING
● 12.5 Combining Input and Output Redirection:
Command < input-file > output-fileCommand > output-file < input-file
Example:
23 CTIS156 INFORMATION TECHNOLOGIES II
$ cat bc_input2+2$ bc < bc_input > bc_output$ cat bc_output4
CH.12 REDIRECTION AND PIPING
● 12.7 Redirecting Standard Error:– The 2> (2 greater than) symbol is used to change the
standard error file. The syntax is:
– command 2> error-file– The error messages are written into an external file instead
of the stderr (screen)
24 CTIS156 INFORMATION TECHNOLOGIES II
CH.12 REDIRECTION AND PIPING
● 12.9 Redirecting stdin stdout and stderr in one command:
● 0< standard input● 1> standard output● 2> standard error
Example:● $sort 0< students 1> students.sorted 2> sort.error
25 CTIS156 INFORMATION TECHNOLOGIES II
CH.12 REDIRECTION AND PIPING
● 12.11 Linux Pipes:– In Linux stdout of a command can be connected to the stdin of another command.
– | (pipe) character is used between the commands
– Syntax:● command1 | command2 |...| commandN
26 CTIS156 INFORMATION TECHNOLOGIES II
CH.12 REDIRECTION AND PIPING
● Linux Pipes (cont):– Linux pipe example:
● $ls -l | more
– The output of ls -l command is fed to more command as input
● $history | tail -7
– The output of history command is fed to tail -7 command as input
27 CTIS156 INFORMATION TECHNOLOGIES II
CH.12 REDIRECTION AND PIPING
● References:● LINUX: The Textbook. Sarwar, Koretsky, Sarwar.
Addison Wesley. 2002.
28 CTIS156 INFORMATION TECHNOLOGIES II