This module on Unix structure and commands includes: Introduction Unix File System Put into practice file handling commands Identify command substitution, filters, and pipes. By the end of this module, you should be able to: Discuss the basics of Unix File System Put into practice locating files with find xargs command.
This module on Unix structure and commands includes: Introduction Unix File System Put into practice file handling commands Identify command substitution, filters, and pipes. By the end of this module, you should be able to: Discuss the basics of Unix File System Put into practice locating files with find xargs command.
This module on Unix structure and commands includes: Introduction Unix File System Put into practice file handling commands Identify command substitution, filters, and pipes. By the end of this module, you should be able to: Discuss the basics of Unix File System Put into practice locating files with find xargs command.
IBM Welcome Welcome Unix structure and Commands 1 Copyright IBM Corporation 2011
IBM Global Business Services IBM Unix Structure and Commands Copyright IBM Corporation 2011 Unix structure and Commands 2 Copyright IBM Corporation 2011 IBM Global Business Services IBM Objectives By the end of this module on Unix structure and commands, you should be able to:
Discuss the basics of Unix File System Put into practice file handling commands Define vi Editor basics Identify command substitution, filters, and pipes Illustrate locating files with find Demonstrate the xargs command
Unix structure and Commands 3 Copyright IBM Corporation 2011 IBM Global Business Services IBM Agenda This module on Unix structure and commands includes:
Introduction Unix File System File Handling Commands vi Editor Basics Command substitution, filters and pipes Locating files with find xargs command
Unix structure and Commands 4 IBM Global Business Services Copyright IBM Corporation 2010
Topic 1: Introduction Copyright IBM Corporation 2011 IBM Global Business Services IBM Sub-topics covered The sub-topics covered in this module include:
Operating system History of Unix Features of Unix Architecture of Unix
Unix structure and Commands 6 Copyright IBM Corporation 2011 IBM Global Business Services IBM Operating system The operating system can be defined as follows:
It is the interface between hardware and user.
It is responsible for management and coordination of activities and sharing of resources of computer.
It acts as host for computing applications that run on the machine.
It provides applications access OS services through APIs or System Calls.
It provides two types of interfaces to the users, namely Command Line Interface and Graphical User Interface. Unix structure and Commands 7 Copyright IBM Corporation 2011 IBM Global Business Services IBM History of Unix Here is a timeline that shows how Unix developed over time:
In 1969, UNIX first originated in form of Multics at Bell Laboratories.
In 1974, Thompson and Ritchie both published a paper regarding UNIX in Communications of ACM.
By 1977, several UNIX systems were used in Universities. Between 1977 to 1982, UNIX System III was released by Bell Laboratories.
By the start of 1984, the use of UNIX systems significantly increased.
Linux is a complete rewrite of Unix, developed by Linus Tourvalds, when he was a Finnish undergraduate. Unix structure and Commands 8 Copyright IBM Corporation 2011 IBM Global Business Services IBM Features of Unix The feature of Unix are listed below:
Portable: Unix OS can be installed on any architecture.
Multi-user: Multiple users can access the system and share its resources.
Multi-tasking: Multiple tasks can be initiated and run simultaneously.
Time-sharing: Server shares CPU time between requesting processes.
Hierarchical file organization: / (root) is at the top of hierarchy and the various other file systems are below that.
Unix structure and Commands 9 Copyright IBM Corporation 2011 IBM Global Business Services IBM The architecture of Unix includes:
System components Kernel Shell
The system components are shown in the diagram here.
Architecture of Unix Hardware Kernel Shell Shell sed ps grep ls who tar sort cp X-Window cc Other Compilers Various Text Processors Spread Sheets Browsers Different Databases Other Softwares User 2 User 1 Unix structure and Commands 10 Copyright IBM Corporation 2011 IBM Global Business Services IBM The architecture of Unix, especially kernel, is defined below:
This is the core of Operating System.
It functions as the Hardware interface.
It contains system calls that perform low-level tasks.
It generates inode-numbers for newly created files and maintains inode tables.
It generates process-IDs for newly created processes and maintains process control blocks. Architecture of Unix: Kernel Unix structure and Commands 11 Copyright IBM Corporation 2011 IBM Global Business Services IBM The architecture of Unix, especially shell, is defined below:
User interface: Shell provides an interface to the user, wherein the user could issue his or her commands, and shell displays output and error messages to the user.
Command interpreter: Shell accepts command from user and interprets it to the kernel.
Command processor: Shell parses the command line arguments, expands the special meaning of meta characters, searches for the command, and if the command is found, then transfers control to the command.
Programming language: Shell provides a native programming language.
Architecture of Unix: Shell Unix structure and Commands 12 Copyright IBM Corporation 2011 IBM Global Business Services IBM Questions and answers Unix structure and Commands 13 Copyright IBM Corporation 2011 IBM Global Business Services IBM Knowledge check What is the interface between the hardware and the user?
Who developed Unix?
Enumerate three features of Unix.
Unix structure and Commands 14 IBM Global Business Services Copyright IBM Corporation 2010
Topic 2: UNIX File System Copyright IBM Corporation 2011 IBM Global Business Services IBM Sub-topics covered The sub-topics covered in this topic include:
File system Inode table Directory structure Unix structure and Commands 16 Copyright IBM Corporation 2011 IBM Global Business Services IBM File system is a way of organizing files in the secondary storage device. The Unix file system is partitioned into four partitions:
Boot Block: Programs associated with booting of the system are stored in this block.
Super Block: This stores accounting information about the file system.
Inode Block: This stores inode tables.
Data Block: This stores data.
File system Unix structure and Commands 17 Copyright IBM Corporation 2011 IBM Global Business Services IBM Every file in Unix file system has an inode table, which stores meta data of the file. The Inode table stores the following information about the file:
Inode table
File Type
Permission Mode
User Id
Group Id
File Size Bytes
Modification Time
Access Time
Inode Updation Time
Ptrs To Data Block
Ord.
644
Tom
Ken
85
14-Jun-09 09:11:45
14-Jun-09 09:11:45
18-Jun-09 15:12
Unix structure and Commands 18 Copyright IBM Corporation 2011 IBM Global Business Services IBM Inode table: Block addressing scheme Inode table consists of 13 fields, each containing addresses to a block each. The first 10 address fields contains addresses of a data block each. This is direct addressing. Number of data blocks addressed: 10.
The 11th field contains address of an address block, the addresses of which point to a data block each. This is single indirect addressing. Number of data blocks addressed: 128.
12th field contains address of an address block, the addresses of which point to an address block each, each of the addresses of which point to data blocks; Number of data blocks addressed: 128 * 128.
13th field contains address of an address block, the addresses of which points to an address block each; each of the address of these address blocks point to an address block each, through which, data blocks are pointed. Number of data blocks addressed. 128 * 128 * 128. Unix structure and Commands 19 Copyright IBM Corporation 2011 IBM Global Business Services IBM Directory structure Directory structure in Unix is an inverted tree structure, wherein the root (/) directory is at the top of the hierarchy and the subdirectories are placed as children of the root.
/ etc dev sbin bin usr tmp
Unix structure and Commands 20 Copyright IBM Corporation 2011 IBM Global Business Services IBM There are two means of referencing of files, including:
Absolute referencing: The referencing of a file done with reference to the root directory, / Examples: $ cd /usr/include # changes to /usr/include directory $ ls /dev/tty # lists the file /dev/tty
Relative referencing: The referencing of a file done relative to the working directory. Current directory and parent to current directory are referred by shortcuts . and .. respectively.
Examples: $ cd cppdir # changes to subdirectory cppdir Directory structure: Referencing of files Unix structure and Commands 21 Copyright IBM Corporation 2011 IBM Global Business Services IBM Questions and answers Unix structure and Commands 22 Copyright IBM Corporation 2011 IBM Global Business Services IBM Knowledge check What are the four partitions of the Unix file system?
Enumerate three types of information stored in the Inode table.
What is the referencing of a file done relative to the working directory?
Unix structure and Commands 23 IBM Global Business Services Copyright IBM Corporation 2010
Topic 2: UNIX file system Copyright IBM Corporation 2011 IBM Global Business Services IBM Sub-topics covered The sub-topics covered in this topic include:
File system Inode table Directory structure Unix structure and Commands 25 Copyright IBM Corporation 2011 IBM Global Business Services IBM pwd: Knowing your present working directory
$ pwd <Enter> /home/sachin Unix structure and Commands 26 Copyright IBM Corporation 2011 IBM Global Business Services IBM ls: Listing files and subdirectories in any directory
$ ls <Enter> abc.txt s.sh test.c dir1
$ ls /home/sachin <Enter> pmode.sh fibonacci.c profile.txt
$ ls l <Enter> -rwxr-xr-x 1 sachin football 512 feb 23 12:37 abc.txt -rwxr-xr-x 1 sachin football 20 mar 12 14:16 s.sh -rwxr-xr-x 1 sachin football 134 apr 4 09:11 test.c
Unix structure and Commands 27 Copyright IBM Corporation 2011 IBM Global Business Services IBM ls: Listing files and subdirectories in any directory
$ ls l /home/sachin <Enter> -rwxr-xr-x 1 sachin football 36 mar 3 17:25 pmode.sh -rwxr-xr-x 1 sachin football 114 jan 24 20:12 fibonacci.c -rwxr-xr-x 1 sachin football 50 apr 5 10:20 profile.txt
$ ls -a <Enter>
$ ls -a /usr/sachin<Enter>
$ ls -a | more<Enter>
$ ls -lrt Unix structure and Commands 28 Copyright IBM Corporation 2011 IBM Global Business Services IBM
$ cd <Enter> $ pwd <Enter> /home/sachin
$ cd /usr/lib <Enter> $ pwd <Enter> /usr/lib
$ cd ..<Enter> $ pwd <Enter> /usr
cd: Changing Directory Unix structure and Commands 29 Copyright IBM Corporation 2011 IBM Global Business Services IBM
$ cd bin<Enter> $ pwd <Enter> /usr/bin
$ cd ../..<Enter> $ pwd <Enter> /
$ cd - $ pwd <Enter> (Takes you to the previous directory)
cd: Changing Directory (continued) Unix structure and Commands 30 Copyright IBM Corporation 2011 IBM Global Business Services IBM
$ cd /home/sachin $ mkdir playdt <Enter> $ cd playdt <Enter> $ pwd <Enter> /home/sachin/playdt
$ mkdir /home/sachin/playdt/millnm <Enter> $ cd mar <Enter> $ pwd <Enter> /home/sachin/playdt/millnm
mkdir: Creating new directory or subdirectory Unix structure and Commands 31 Copyright IBM Corporation 2011 IBM Global Business Services IBM
$ mkdir p thisyr/mar/firstprd<Enter> $ cd thisyr/mar/firstprd <Enter> $ pwd <Enter> /home/sachin/playdt/millnm/ thisyr/mar/firstprd
mkdir: Creating new directory or subdirectory (continued) Unix structure and Commands 32 Copyright IBM Corporation 2011 IBM Global Business Services IBM The removing criteria for the directory or subdirectory to be removed is as follows:
It should not be the current directory It should be vacant
$ cd /home/sachin/playdt/millnm/thisyr/mar $ pwd <Enter> /home/sachin/playdt/millnm/thisyr/mar $ rmdir firstprd <Enter> $ cd .. <Enter> $pwd <Enter> /home/sachin/playdt/millnm/thisyr
rmdir: Removing existing directory or subdirectory Unix structure and Commands 33 Copyright IBM Corporation 2011 IBM Global Business Services IBM
$ cd ../.. <Enter> $ pwd <Enter> /home/sachin $ rmdir p /playdt/millnm <Enter>
rmdir: Removing existing directory or subdirectory (continued) Unix structure and Commands 34 Copyright IBM Corporation 2011 IBM Global Business Services IBM Some file-oriented commands include:
cat mv cp rm chmod
Some file oriented commands Unix structure and Commands 35 Copyright IBM Corporation 2011 IBM Global Business Services IBM
$ cat profile.txt <Enter> My company name is IBM. My work location is Kolkata.
$ cat /user/sachin/pmode.sh <Enter> echo The working mode is Learning.
$ cat profile.txt pmode.sh <Enter> My company name is IBM. My work location is Kolkata. echo The working mode is Learning.
cat: Displaying the content of one or more file Unix structure and Commands 36 Copyright IBM Corporation 2011 IBM Global Business Services IBM
$ cd /home/sachin <Enter> $ mv fibo.c fibonacci.c <Enter>
mv: Moving file from one directory or subdirectory to another Unix structure and Commands 37 Copyright IBM Corporation 2011 IBM Global Business Services IBM
$ rm * <Enter> (Use the above command with great caution)
rm: Deleting one or more files Unix structure and Commands 40 Copyright IBM Corporation 2011 IBM Global Business Services IBM Interactive Mode:
$ rm i fb.c <Enter> fb.c: ? y
Force Mode:
$ rm f p.sh <Enter>
Recursive Mode:
$ rm r /home/sachin <Enter> $ rm r * <Enter>(Recursive removal of files )
rm: Several options Unix structure and Commands 41 Copyright IBM Corporation 2011 IBM Global Business Services IBM
$ date <Enter> Mon Apr 23 11:23:34 IST 2007
date: Displaying system date Unix structure and Commands 42 Copyright IBM Corporation 2011 IBM Global Business Services IBM
d =>Day (for example, 23) m =>Month Number (for example, 10) y =>Rightmost Two Digits of Year (for example, 07 for 2007) a =>Short Name of Week Day (for example, Mon) h =>Short Name of Month (for example, Apr) H =>The Hour in 24 Hour Format (for example, 20) M =>Minute (for example, 34) S =>Second (for example, 12)
date: Several format specifiers Unix structure and Commands 43 Copyright IBM Corporation 2011 IBM Global Business Services IBM
$ date +%a <Enter> Mon
$ date +%H %M <Enter> 11 34
$ date +%H : %M <Enter> 11 : 34
date: Use of several format specifies Unix structure and Commands 44 Copyright IBM Corporation 2011 IBM Global Business Services IBM
$ cal 2007 <Enter>
$ cal <Enter>
$ cal apr <Enter>
$ cal 2007 | more <Enter>
cal: Displaying calendar Unix structure and Commands 45 Copyright IBM Corporation 2011 IBM Global Business Services IBM
$ banner IBM KOLKATA <Enter> IBM KOLKATA
$ banner IBM KOL <Enter> IBM KOL
$ banner IBM KOL IT WORK <Enter> IBM KOL IT WORK banner: Displaying posters for any text Output will be much bigger than shown here. Unix structure and Commands 46 Copyright IBM Corporation 2011 IBM Global Business Services IBM Locking without time specification:
lock: Locking the system without logging out Locking for 30 Minutes, if time not specified during command invocation Unix structure and Commands 47 Copyright IBM Corporation 2011 IBM Global Business Services IBM Waits for All:
$ wait <Enter>
Waits for Some Specific One:
$ wait 232 <Enter> ( wait for process id 232 )
wait: Waits for finishing of the system background process Unix structure and Commands 48 Copyright IBM Corporation 2011 IBM Global Business Services IBM
$ echo Delay Invocation; sleep 20; echo Back Again After 20 Sec <Enter> Delay Invocation Back Again After 20 Sec
Delay of 20 Seconds
sleep: Invoking delay Unix structure and Commands 49 Copyright IBM Corporation 2011 IBM Global Business Services IBM
$ who <Enter> root console apr 23 15:23 sachin tty01 apr 23 15:24 sh tty02 apr 23 15:25
who: Aiding in being acquainted with all logged-in users Unix structure and Commands 50 Copyright IBM Corporation 2011 IBM Global Business Services IBM
$ who am I <Enter> sachin tty01 apr 23 15:24
who am I: To know current user, who has issued command Unix structure and Commands 51 Copyright IBM Corporation 2011 IBM Global Business Services IBM
$ uname -n <Enter> sachdev
uname n: Knowing computers name in network Unix structure and Commands 52 Copyright IBM Corporation 2011 IBM Global Business Services IBM
$ tty <Enter> /dev/tty01
tty: To be acquainted with the name of users terminal Unix structure and Commands 53 Copyright IBM Corporation 2011 IBM Global Business Services IBM
$ tput cup 15 28 <Enter> $-
tput cup: Cursor positioning at desired location Unix structure and Commands 54 Copyright IBM Corporation 2011 IBM Global Business Services IBM
$ tput clear <Enter> $-
tput clear: Monitor screen clearing Unix structure and Commands 55 Copyright IBM Corporation 2011 IBM Global Business Services IBM
tput smso and tput rmso: Highlighting displayable text Tutorial for Highlighting Displayable Text Unix structure and Commands 56 Copyright IBM Corporation 2011 IBM Global Business Services IBM
bc: Performing numeric calculation (continued) Unix structure and Commands 58 Copyright IBM Corporation 2011 IBM Global Business Services IBM
$ bc <Enter> r=9 ; s=4 ; <Enter> t=r-s <Enter> t <Enter> 5 <ctrl +d> $_
$ x=3 <Enter> $ y=`echo $x +4 | bc` <Enter> $ echo $y <Enter> 7 bc: Performing numeric calculation (continued) Unix structure and Commands 59 Copyright IBM Corporation 2011 IBM Global Business Services IBM UNIX:
$ spell /home/sachin/profile.txt <Enter>
$ cd /home/sachin <Enter> $ spell profile.txt <Enter>
LINUX:
$ ispell /home/sachin/profile.txt <Enter>
$ cd /home/sachin <Enter> $ ispell profile.txt <Enter> spell / ispell: Checking of spellings Unix structure and Commands 60 Copyright IBM Corporation 2011 IBM Global Business Services IBM The wildcard characters basics include:
? => Must be One Character, Any character
* => Can be Zero or Any Number of Characters, Any Character
[L] where L is a set of characters placed without separation => One character, anyone character from L. Wildcard characters basics Unix structure and Commands 61 Copyright IBM Corporation 2011 IBM Global Business Services IBM
$ ls * <Enter>
$ ls l p*.?? <Enter>
$ cat ?????.sh <Enter>
$ ls p[rm]* <Enter> Wildcard characters: Examples Unix structure and Commands 62 Copyright IBM Corporation 2011 IBM Global Business Services IBM $ chmod u+x profile.txt <Enter>
$ chmod g+rw profile.txt <Enter>
$ chmod o-r profile.txt <Enter>
$ chmod +wx profile.txt <Enter>
$ chmod x profile.txt <Enter>
$ chmod go -w profile.txt <Enter>
$ chmod u=rwx, go=rx profile.txt <Enter> chmod: Altering file access permissions using symbols Several Symbols u => File Owner g => Group Owner o => Other User r => Read w => Write x => Execute Unix structure and Commands 63 Copyright IBM Corporation 2011 IBM Global Business Services IBM
$ chmod 741 profile.txt <Enter>
$ chmod 777 profile.txt <Enter>
$ chmod 751 profile.txt <Enter>
$ chmod 000 profile.txt <Enter> chmod: Altering file access permissions using numbers Several Numbers 0 => No Permission Is Present 1 => Execute Only 2 => Write Only 3 (2+1) => Write & Execute 4 => Read Only 5 (4+1) => Read & Execute 6 (4+2) => Read & Write 7 (4+2+1) => Read, Write & Execute Several Permission Weights Read => 4 Write => 2 Execute => 1 Unix structure and Commands 64 Copyright IBM Corporation 2011 IBM Global Business Services IBM Running processes related to current user at current terminal:
$ ps <Enter> PID TTY TIME CMD 234 tty01 00:00:02 sh 684 tty01 00:00:00 ps
All Running User and System Processes:
UNIX: $ ps -e <Enter>
LINUX: $ ps -ax <Enter> ps: Knowing about running processes of system Unix structure and Commands 65 Copyright IBM Corporation 2011 IBM Global Business Services IBM Details of running processes related to current user at current terminal:
UNIX: $ ps -f <Enter>
LINUX: $ ps -u <Enter>
ps: Knowing about running processes of system (continued) Unix structure and Commands 66 Copyright IBM Corporation 2011 IBM Global Business Services IBM INPUT REDIRECTION:
$ cat pl.txt 2>err.txt<Enter> Redirection: Altering input, output, or error files for a command Unix structure and Commands 67 IBM Global Business Services Copyright IBM Corporation 2010
Topic 4: vi Editor Basics Copyright IBM Corporation 2011 IBM Global Business Services IBM Sub-topics covered The sub-topics covered in this topic include:
vi Editor Basics Mode commands Repeat factor Unix structure and Commands 69 Copyright IBM Corporation 2011 IBM Global Business Services IBM vi Editor is a powerful full-screen editor for UNIX. Texts, numbers, commands, and more can be inserted to any file through this. For file creation, editing, and the like, this is used extensively in UNIX.
vi Editor can be invoked with the following command: $ vi viexmpl.sh <Enter>
Some of the important modes of vi Editor include:
Input Mode Command Mode
vi Editor: Introduction Unix structure and Commands 70 Copyright IBM Corporation 2011 IBM Global Business Services IBM vi Editor: Toggling between important modes and Shell Shell Command Mode Input Mode Vi filename <Enter> <Esc> i, a, I, A etc. :wq, :q!, :x etc. Unix structure and Commands 71 Copyright IBM Corporation 2011 IBM Global Business Services IBM Several Input Mode Commands in vi Editor Some input mode commands in vi Editor include:
i: Inputs text at left of current cursor position. a: Appends text at right of current cursor position. I: Inputs text at starting of current line. A: Appends text at the end position of current line. R: Replaces (overwrites) text from current cursor position to right direction. S: Replaces the full line in which the cursor is.
Unix structure and Commands 72 Copyright IBM Corporation 2011 IBM Global Business Services IBM Several essential command mode commands in vi Editor Some input mode commands in vi Editor include:
:x (or :wq): Saves and Exits from vi Editor. :q!: Quits from vi Editor without Save. :w: Saves current file and dont exit the vi Editor. h: Shifts the cursor to one character left. J: Shifts the cursor to one line down. K: Shifts the cursor to one line up. l: Shifts the cursor to one character right. 4h: Shifts the cursor to four characters left. 5j: Shifts the cursor to five lines down. 6k: Shifts the cursor to six lines up. 7l: Shifts the cursor to seven characters right.
Unix structure and Commands 73 Copyright IBM Corporation 2011 IBM Global Business Services IBM More essential command mode commands in vi Editor Some input mode commands in vi Editor include:
|: Cursor reaches the beginning of current line. $: Cursor reaches the beginning of current line. <ctrl+f>: Scrolls forward one page. <ctrl+b>: Scrolls backward one page. 20G: Cursor reaches 20th line of the current file. 15|: Cursor reaches 15th column of current line. 1G: Cursor reaches the Starting of the current file. G: Cursor reaches the end of the current file. x: Erases the current character. 4x: Erases current character and three next characters. d15G: Erases all from current cursor position to 15th line. Unix structure and Commands 74 Copyright IBM Corporation 2011 IBM Global Business Services IBM Additional essential command mode commands in vi Editor Some input mode commands in vi Editor include:
dd: Erases full current line. 7dd: Erases full current line and next six lines. . : Applies the last editing command used. u: Undoes the last editing operation performed. J: Joins the current line with the next line. 7yy: Copies the current line and next six lines together. p: Pastes the copied content after the current cursor position. ~ : Changes current character case to the reverse one. 15~: Changes current and next 14 character case to the reverse one. 1p: Restores latest erase work performed. 3p: Restores latest three erase work performed. Unix structure and Commands 75 Copyright IBM Corporation 2011 IBM Global Business Services IBM Repeat factor in vi Editor In Command Mode:
10i^<Enter> or 10i^<Esc>
Will enter consecutive 10 ^ that is, ^^^^^^^^^^ Unix structure and Commands 76 Copyright IBM Corporation 2011 IBM Global Business Services IBM Some search commands include:
/IBM: Searches in forward direction for pattern IBM in current file. ?kolkata: Searches in backward direction for pattern kolkata in current file.
Some substitute commands include:
:1,25s/Calcutta/Kolkata/: Replaces first occurrence of Calcutta with Kolkata between lines 1 to 25 in current file. :1,25s/Calcutta/Kolkata/g: Replaces all occurrence of Calcutta with Kolkata between lines 1 to 25 in current file. :.,$s/Calcutta/Kolkata/gc: Replaces Calcutta with Kolkata interactively between current line and end of current file. Commands Unix structure and Commands 77 IBM Global Business Services Copyright IBM Corporation 2010
Topic 5: Command Substitution, Filters, and Pipes Copyright IBM Corporation 2011 IBM Global Business Services IBM Sub-topics covered The sub-topics covered in this topic include:
Command substitution Filters Pipes
Unix structure and Commands 79 Copyright IBM Corporation 2011 IBM Global Business Services IBM Command substitution is the technique of passing a commands output as argument to another command. The command that passes output as argument can be called as inner command. The command that receives the output of another command as its argument, can be called as outer command. Here is an example:
$ echo "Today's date is `date` Today's date is Tue Apr 7 15:14:41 IST 200 $ echo "No. of files in $PWD is $(ls|wc -l) No. of files in /home/murali is 45
Command substitution Explanation: The meta-characters ` `(pair of backticks) and $() are known as command substitution meta-characters. When a command is issued, shell first parses the command line arguments; the shell first expands special meaning of these meta-characters; hence, shell first executes the command specified within `` or $() and places Output of this command as argument to the main command; then, Shell transfers control to the main command. Unix structure and Commands 80 Copyright IBM Corporation 2011 IBM Global Business Services IBM This refers to a command which, by default, expects input from standard input file and sends processed output to standard output file.
Here is a list of some filters:
uniq tr tee pr cut paste bc
Filters Unix structure and Commands 81 Copyright IBM Corporation 2011 IBM Global Business Services IBM It translates consecutive identical entries from the input file, into unique entries. These include:
$ cat domestics dog goat goat goat horse $ uniq domestics dog goat horse
Filters: Unique Unix structure and Commands 82 Copyright IBM Corporation 2011 IBM Global Business Services IBM Here is an example of unique filters:
c option $ cat domestics dog goat goat $ uniq -c domestics 1 dog 2 goat
Filters: Unique (continued) Explanation of code: With c option, uniq command displays number of consecutive occurrences of the identical entries and also the unique entries.
Unix structure and Commands 83 Copyright IBM Corporation 2011 IBM Global Business Services IBM It translates all occurrences of a set of characters in the first expression to corresponding set of characters in second expression, from the input file.
Syntax:
tr exp1 exp2 < file1
Options:
S: This will squeeze multiple consecutive occurrences of a specified set of characters to single occurrence. D: This deletes the specified set of characters from the string. Cd: This complement deletes the specified set of characters from the string.
Filters: tr Unix structure and Commands 84 Copyright IBM Corporation 2011 IBM Global Business Services IBM Here is an example of tr filters:
$ cat >text1 a quick brown fox jumped over the lazy dog $ tr '[a-z]' '[A-Z]' <text1 A QUICK BROWN FOX J UMPED OVER THE LAZY DOG $
Filters: tr (example) Explanation of code: The tr command translates all occurrences of lower-case alphabets to corresponding upper-case alphabets in the file text1; it displays the output. Unix structure and Commands 85 Copyright IBM Corporation 2011 IBM Global Business Services IBM Here is another example of tr filters:
Filters: tr (example) (continued) Explanation of code: The tr command squeezes consecutive multiple occurrences of space character to single occurrence in the input file mydirinfo, and displays output. Unix structure and Commands 86 Copyright IBM Corporation 2011 IBM Global Business Services IBM Here is another example of tr filters using d option:
$ cat >numfile 78343hhdfdh434384 834893849&*&*&dfdhfhdj 4839483jhdjfhdjf hsdjfhdjfhdj4738473 jdfjdhf384738 $ tr -d '[a-z&*]' <numfile 78343434384 834893849 4839483 4738473 384738 Filters: tr (example: d option) Explanation of code: The tr command removes lower-case alphabets, the characters & and * from the input file numfile, and displays output. Unix structure and Commands 87 Copyright IBM Corporation 2011 IBM Global Business Services IBM Here is another example of tr filters using cd option:
$ cat >numfile 78343hhdfdh434384 834893849&*&*&dfdhfhdj 4839483jhdjfhdjf hsdjfhdjfhdj4738473 jdfjdhf384738 $ tr -cd '[0-9\n]' <numfile 78343434384 834893849 4839483 4738473 384738 Filters: tr (example: cd option) Explanation of code: The tr command removes all other characters except digit characters and newline characters from the input file numfile, and displays output. Unix structure and Commands 88 Copyright IBM Corporation 2011 IBM Global Business Services IBM It examines and counts the number of arguments say, n issued to it, creates n+1 copies of the input it receives, and writes n copies to the n files specified to it as arguments, and one copy to the standard output file.
Syntax:
tee file1 file2 file3
Filters: tee Unix structure and Commands 89 Copyright IBM Corporation 2011 IBM Global Business Services IBM It paginates or columnates files for printing.
Syntax:
pr [ file1 file2 file3 ]
Options:
D: This will double space the output. D: This will use format for the header date. L: This sets the page length. W: This sets the page width.
Filters: pr Unix structure and Commands 90 Copyright IBM Corporation 2011 IBM Global Business Services IBM It extracts specified characters or fields from the input file.
Syntax:
cut [<option(s)>] [<file(s)>]
Options:
c: Used to specify the characters, that have to be extracted from every line of input file d: Used to specify field delimiter, based on which, cut would understand the fields f: Used to specify the number of the fields that have to be extracted
Filters: cut Unix structure and Commands 91 Copyright IBM Corporation 2011 IBM Global Business Services IBM It expects two files as input files, joins corresponding lines of the two files, and sends the output to standard output file.
Syntax:
paste [<option(s)>] [<file1>] [<file2>]
Filters: paste Unix structure and Commands 92 Copyright IBM Corporation 2011 IBM Global Business Services IBM Performs calculation on the input expression. Takes input for standard input and sends processed output to standard output.
Syntax:
bc bc calcfile #calcfile contains expression
Examples:
$ bc #issues input interrupt 5^3 #finds 5 raised to power of 3 125 #and displays output on screen quit #terminates bc program $ #and returns control back to shell
Filters: bc Unix structure and Commands 93 Copyright IBM Corporation 2011 IBM Global Business Services IBM A pipe is a communicating process. It receives output of one command and sends it as input to another command.
command1 | command2
Example: ls t | head -15 Output of ls command is passed on to head command as its input, through the pipe.
Pipes Command1 Command2 Unix structure and Commands 94 Copyright IBM Corporation 2011 IBM Global Business Services IBM Questions and answers Unix structure and Commands 95 Copyright IBM Corporation 2011 IBM Global Business Services IBM Knowledge check What is the command that receives the output of another command as its argument?
What filter extracts specified characters or fields from the input file?
What receives output of one command and sends it as input to another command?
Unix structure and Commands 96 IBM Global Business Services Copyright IBM Corporation 2010
Topic 6: Locating Files with find Copyright IBM Corporation 2011 IBM Global Business Services IBM Sub-topics covered The sub-topics covered in this topic include:
find find example find example (2) find example (3) Unix structure and Commands 98 Copyright IBM Corporation 2011 IBM Global Business Services IBM It searches for the files with specified criteria, recursively starting from the specified directory, and takes action on those files. The default action is print.
find Unix structure and Commands 99 Copyright IBM Corporation 2011 IBM Global Business Services IBM List of operators and their purpose:
find -name : search for files matching specific names; find supports wild cards -mtime n : File was last modified n*24 hours ago -atime n : File was last accessed n*24 hours ago -amin n : File was last accessed n minutes ago -cmin n : Files status last changed n minutes ago -mmin n : Files data was last modified n mins ago -newer rfile : Files that are modified more recently than rfile -inum n : File has inode number n. -links n : File has n links -user uname : File is owned by user uname (numeric user ID allowed). Unix structure and Commands 100 Copyright IBM Corporation 2011 IBM Global Business Services IBM List of operators and their purpose:
find -size n[cwbkMG]: File uses n units of space. The following suffixes can be used: b : for 512 bytes blocks (this is default if no suffix is used) C : for bytes w : for two-byte words k : for kilobytes (units of 1024 bytes) M : for megabytes (units of 1048576 bytes) G : for Gigabytes (units of 1073741824 bytes) Unix structure and Commands 101 Copyright IBM Corporation 2011 IBM Global Business Services IBM List of operators and their purpose:
find -type c : file is of type c b : block special c : character special d directory p named pipe (FIFO) f regular file l symbolic link s socket Unix structure and Commands 102 Copyright IBM Corporation 2011 IBM Global Business Services IBM
find: example (1) Explanation of code: find command recursively searches for files under the directory /tmp which are not modified within past 30 days and prints them. Unix structure and Commands 103 Copyright IBM Corporation 2011 IBM Global Business Services IBM List of operators and their purpose:
find -print: print the full file name on the standard output, followed by a newline.
-exec: executes a unix command placing files output by find, one, for each execution
-ok: executes a unix command placing files output by find, one, for each execution non-interactively Unix structure and Commands 104 Copyright IBM Corporation 2011 IBM Global Business Services IBM
find: example (1) Explanation of code: find searches recursively under home directory of user, for regular files whose link count is more than one, that is, hard linked files. Unix structure and Commands 105 Copyright IBM Corporation 2011 IBM Global Business Services IBM
find: example (2) Explanation of code: find searches recursively under directory /, whose file size is zero bytes and pipes output to xargs and xargs places. The list of files as arguments to ls command; ls command redirects output to the file ZeroByteFiles and errors are redirected to /dev/null; this command is run in background. Unix structure and Commands 106 Copyright IBM Corporation 2011 IBM Global Business Services IBM $ find / -perm 777 -type f 2>/dev/null | xargs ls -l -rwxrwxrwx 1 user20 user20 11688 2009-06-25 15:41 /home/user20/.bash_history -rwxrwxrwx 1 user20 user20 33 2009-03-06 15:43 /home/user20/.bash_logout -rwxrwxrwx 1 user20 user20 181 2009-05-11 12:57 /home/user20/.bash_profile -rwxrwxrwx 1 user20 user20 124 2009-03-06 15:43 /home/user20/.bashrc find: example (2) (continued) Explanation of code: The find command recursively lists all regular files under the directory / whose octal permission mode is 777; errors are discarded; xargs receives list of files output by find through pipe and places this list as arguments to ls command, and transfers control to ls command; ls command displays long-listing of these files. Unix structure and Commands 107 Copyright IBM Corporation 2011 IBM Global Business Services IBM
find: example (3) Explanation of code: The find command recursively lists all files under the home directory of user, files whose modification timestamp is later than the file, myreffile; find displays output and sends errors to /dev/null. Unix structure and Commands 108 Copyright IBM Corporation 2011 IBM Global Business Services IBM
$ find . -mtime +365 -type f -exec ls -l {}\; #-exec executes a shell cmd #on the output of find command -rw-r--r-- 1 murali murali 658 2007-10-11 19:46 ./.zshrc -rw-r--r-- 1 murali murali 33 2007-08-31 19:50 ./.bash_logout $ find . -size 0 -type f -ok rm {}\; #-ok interactively confirms from user <rm ... ./usrs_at_now >? Y #before executing shell command <rm ... ./no_access_tmp >? Y #on every argument <rm ... ./mynewerrfile >? n <rm ... ./users >? y <rm ... ./myperlscripts/scores_hash.pl >? n <rm ... ./myperlscripts/mysubstitute.pl >? n <rm ... ./cobolprogs >? y find: example (3) (continued) Unix structure and Commands 109 Copyright IBM Corporation 2011 IBM Global Business Services IBM find: example (3) (continued) Explanation of code: The find command recursively outputs all regular files under working directory of user, which are not modified since the past 365 days; exec action of find command places the files one by one as argument to ls command and executes ls command. Unix structure and Commands 110 Copyright IBM Corporation 2011 IBM Global Business Services IBM Questions and answers Unix structure and Commands 111 Copyright IBM Corporation 2011 IBM Global Business Services IBM Knowledge check find / -type f size +32 print
Output of the above command is:
a) Regular files that are greater than 32 bytes in size b) Regular files that are greater than 32 MB in size c) Regular files that are greater than 32 blocks in size d) Regular files that are exactly 32 blocks in size
Unix structure and Commands 112 IBM Global Business Services Copyright IBM Corporation 2010
Topic 6: The xargs command Copyright IBM Corporation 2011 IBM Global Business Services IBM Sub-topics covered The sub-topics covered in this topic include:
xargs xargs commands Unix structure and Commands 114 Copyright IBM Corporation 2011 IBM Global Business Services IBM It reads arguments from standard input, place theses as arguments to the shell command, and runs the command on these arguments.
Syntax:
xargs <shell_command>
Example:
$ xargs grep "cout" autodemo.c++ mynewprog.c++ autodemo.c++: cout<<++a<<endl; autodemo.c++:// cout<<a<<endl; mynewprog.c++: cout<<(10>5)<<"\t"<<(10<5)<<endl; xargs Unix structure and Commands 115 Copyright IBM Corporation 2011 IBM Global Business Services IBM Here is another example:
Example:
$ ls *.c++ | xargs tar -cvf cppArchive.tar autodemo.c++ inheritance.c++ methOverriding.c++ mynewprog.c++ scope.c++ test.c++ virtualFunction.c++
$ ls -l cppArchive.tar -rw-rw-r-- 1 murali murali 10240 2009-07-07 12:34 cppArchive.tar xargs Unix structure and Commands 116 Copyright IBM Corporation 2011 IBM Global Business Services IBM Another example can be illustrated as follows:
Example:
$ find . -mtime +180 -type f -name "*.txt" | xargs -I {} -t mv {} {}.old mv ./logFile.txt ./logFile.txt.old mv ./links.txt ./links.txt.old mv ./link.txt ./link.txt.old xargs Unix structure and Commands 117 Copyright IBM Corporation 2011 IBM Global Business Services IBM Some xargs commands include:
-I replace-str: It replaces occurrences of replace-str in the initial-arguments with names read from standard input. -t: It prints the command line on the standard error output before executing it. - diff filename1.txt filename2.txt: It reports the difference between the files - compress filename.txt: It compresses the filename filename.txt tail f filename.txt: It lists the last entry of the file filenname.txt run time mailx -s test email emailid < test.out: It sends an e-mail to emailid with the subject test email with the content of the file test.out passwd: It allows the user to change his or her password on the server man command: It shows the manual page for the command
xargs commands Unix structure and Commands 118 Copyright IBM Corporation 2011 IBM Global Business Services IBM Questions and answers Unix structure and Commands 119 Copyright IBM Corporation 2011 IBM Global Business Services IBM Knowledge check User types the command xargs at the $ prompt, and presses <enter> key. What is the response?
User types the following command at the $ prompt; what is the response? $ find / 2> /dev/null | xargs grep l DISPLAY > outfiles 2> /dev/null & tar tf shellprogs.tar | xargs cat | more
Unix structure and Commands 120 Copyright IBM Corporation 2011 IBM Global Business Services IBM Tying the ends By the end of this module on Unix Structure and Commands, you are expected to:
Paraphrase the basics of Unix File System List file handling commands Summarize vi editor basics Analyze command substitution, filters, and pipes Illustrate locating files with find Interpret xargs command
Unix structure and Commands 121 Copyright IBM Corporation 2011