Q Explain Tee Command. A: Sybca (Sem:2) Prepared by Amit Patel
Q Explain Tee Command. A: Sybca (Sem:2) Prepared by Amit Patel
UMRAKH
SUB: PROGRAMMING LANGUAGE - I
SHORT EXTERNAL QUESTIONS
Q
Used: The tee command copies standard input to standard output and at the same time
copies it into one or more files.
The first copy goes to the standard output ( Monitor ) and same time it sent to the optional
file specified in the argument list.
It create the output files if they dont exist and overwrite them if they already exist.
Instead of using the keyboard, we can feed the tee command through a pipe.
We placed this command anywhere in pipeline.
Example 1: To save the currently logged in user list into file also display it.
$ who | tee file1
Kumar
13bca01
Nayak 13bca02
May 10 9:32:00
May 10 9:35:24
13bca01
Nayak 13bca02
May 10 9:32:00
May 10 9:35:24
SYBCA (SEM:2)
Page 1
DESRIPTIONS
-l
-s
Suppress list.
SYBCA (SEM:2)
Page 2
67
141
70
163
11
60
265
Find difference at character 8,9 and 11 which is shown in first column, second and third
column display the octal value of the two character which is differ. In character 8, both file
contain 7 (octal value is 70) and a (octal value is 141).
Example 4: Compare two file using suppress option.
-s option is same like default except that no output is displayed.
It is generally used when writing script.
When no output is displayed, the output is determined by testing the exit
status.
are
identical
SYBCA (SEM:2)
Page 3
Used to comparing two files and find the difference between two files.
Show line by line differences between two files. Essentially, it outputs a set of
instructions for how to change one file in order to make it identical to the second file.
DESRIPTIONS
-l
-s
Suppress list.
The argument can be two files, file and a directory, or two directory.
When one file and one directory are specified, the utility looks for a file with the same
name in the specified directory.
If two directories are provided, all the files with matching name in each directory are
used.
The first line defines what should be done at range1 in file1 to make it match the line at
range2 in file2.
If the range span multiple lines, there will be a text entry, for each line in the specified
range.
Action
SYBCA (SEM:2)
Meaning
Page 4
Append( a )
Delete ( d )
Indicates what lines must be deleted from file1 to make it the same
as file2. Delete can occur only if file1 is larger than file2.
Action
Meaning
6c6
< Hello
(Good Morning).
-->Good Morning
25a26,27
>bye
>Good Night
78,79d77
SYBCA (SEM:2)
Page 5
In our output above, "2,4c2,4" means: "Lines 2 through 4 in the first file need to be
changed in order to match lines 2 through 4 in the second file." It then tells us what
those lines are in each file:
Lines preceded by a < are lines from the first file; lines preceded by > are lines from the
second file.
The three dashes ("---") merely separate the lines of file 1 and file 2.
Example 2:
SYBCA (SEM:2)
Page 6
Here, the output is telling us "After line 2 in the first file, a line needs to be added: line 3
from the second file." It then shows us what that line is.
Example 3: Now let's see what it looks like when diff tells us we need to delete a line.
file1:
I need to go to the store.
I need to buy some apples.
When I get home, I'll wash the dog.
I promise.
file2:
I need to go to the store.
I need to buy some apples.
When I get home, I'll wash the dog.
$ diff file1.txt file2.txt
SYBCA (SEM:2)
Page 7
Here, the output is telling us "You need to delete line 4 in the first file so that both files
sync up at line 3." It then shows us the contents of the line that needs to be deleted.
SYBCA (SEM:2)
Page 8
Compare each line of the first file with its corresponding line in the second file.
$ cat file2
Barun sengupta
C.k.Shukla
Anul Aggarwal
Lalit chowdury
S.n.dagupta
SYBCA (SEM:2)
Page 9
Find can be used in variety of conditions like you can find files
by permissions, users,groups, file type, date, size and other possible criteria.
Syntax:
find
[pathnames] [conditions]
Find file name using name: Find all the files whose name is tecmint.txt in a current
working directory.
$ find . -name tecmint.txt
./tecmint.txt
-atime n
-mtime n
-size n
-type c
-fstype typ
-name nam
-user usr
-group grp
-perm p
SYBCA (SEM:2)
Page 10
Find all the files under /home directory with name tecmint.txt.
$ find /home -name tecmint.txt
/home/tecmint.txt
Find Files Using Name and Ignoring Case: Find all the files whose name is tecmint.txt and
contains both capital and small letters in /home directory.
$ find /home -iname tecmint.txt
./tecmint.txt
./Tecmint.txt
Find Directories Using Name: Find all directories whose name is Tecmint in / directory
$ find / -type d -name Tecmint
/Tecmint
Find PHP Files Using Name: Find all php files whose name is tecmint.php in a current
working directory.
$ find . -type f -name tecmint.php
./tecmint.php
Find all PHP Files in Directory: Find all php files in a directory.
$ find . -type f -name "*.php"
./tecmint.php
./login.php
Find Files With 777 Permissions: Find all the files whose permissions are 777.
$ find . -type f -perm 0777
Use perm option with type
Find Files Without 777 Permissions: Find all the files without permission 777.
SYBCA (SEM:2)
Page 11
Find all Empty Files: To file all empty files under certain path.
$ find /tmp -type f empty
Find all Empty Directories: To file all empty directories under certain path.
$ find /tmp -type d empty
File all Hidden Files: To find all hidden files, use below command.
$ find /tmp -type f -name ".*"
SYBCA (SEM:2)
Page 12