0% found this document useful (0 votes)
104 views

Q Explain Tee Command. A: Sybca (Sem:2) Prepared by Amit Patel

The document provides explanations and examples of several Linux commands used for comparing files and directories: 1. The tee command copies standard input to standard output and also copies it to one or more specified files. It can be used to save command output to a file while also displaying it. 2. The cmp command compares two files byte-by-byte and reports any differences. It exits with a status code to indicate whether the files are identical. 3. The diff command compares two files line-by-line and outputs the differences between them in a format to show what changes would be needed to make one file match the other. 4. The comm command compares sorted files and outputs three columns showing unique

Uploaded by

Amit Patel
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
104 views

Q Explain Tee Command. A: Sybca (Sem:2) Prepared by Amit Patel

The document provides explanations and examples of several Linux commands used for comparing files and directories: 1. The tee command copies standard input to standard output and also copies it to one or more specified files. It can be used to save command output to a file while also displaying it. 2. The cmp command compares two files byte-by-byte and reports any differences. It exits with a status code to indicate whether the files are identical. 3. The diff command compares two files line-by-line and outputs the differences between them in a format to show what changes would be needed to make one file match the other. 4. The comm command compares sorted files and outputs three columns showing unique

Uploaded by

Amit Patel
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 12

VIDYABHARTI TRUST COLLEGE OF BBA & BCA.

UMRAKH
SUB: PROGRAMMING LANGUAGE - I
SHORT EXTERNAL QUESTIONS
Q

Explain tee command.

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

Example 2: Print number of user currently logged in.


$ who | tee file1 | wc -l
2
Here tee is used as a standard output and we can pipe its output to another command.
Example 3: Count the number of files in current directory. Print and save output in a file.
$ ls l | wc l | tee line.txt
In the above example, the ls command lists all files in the current directory that have the
filename extension .txt, one file per line; this output is piped to wc, which counts the lines and
outputs the number; this output is piped to tee, which writes the output to the terminal, and
writes the same information to the file count.txt. If count.txt already exists, it is overwritten.
Example 4: Display number of currently logged in user and its list.
$ who | tee /dev/tty | wc -l
Kumar

13bca01

Nayak 13bca02

May 10 9:32:00

May 10 9:35:24

SYBCA (SEM:2)

PREPARED BY AMIT PATEL

Page 1

VIDYABHARTI TRUST COLLEGE OF BBA & BCA. UMRAKH


SUB: PROGRAMMING LANGUAGE - I
SHORT EXTERNAL QUESTIONS
Here tee is used as a standard output and we can pipe its output to another command.

Explain cmp command

cmp means Comparing.


Used to comparing two files.
We may often require to know whether two files are identical so that one of them can be
deleted.
It require two filenames as a arguments.
It examine two files byte by byte.
Action taken by it depends on the option code used.
OPTION

DESRIPTIONS

-l

List option gives detailed list of byte number.

-s

Suppress list.

Example 1: Create two files and compare it.


$ cat file1
123456
789
$ cat file2
123456
789
$ cmp file1 file2
Here both files are same so it doesnt displayed any message

Example 2: Create two files and compare it.


$ cat file1
123456

SYBCA (SEM:2)

PREPARED BY AMIT PATEL

Page 2

VIDYABHARTI TRUST COLLEGE OF BBA & BCA. UMRAKH


SUB: PROGRAMMING LANGUAGE - I
SHORT EXTERNAL QUESTIONS
789
$ cat file2
123456
abcd
$ cmp file1 file2
file1 file2 differ: char 8, line 2
Here files are different, so the location of the first difference is displayed.
Example 3: Compare two files and display detailed output with all differences.
$ cmp -l file1 file2
8

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.

$ cmp -s file1 file2


$ echo $?
1
If exit status is 1, means two files are not identical and if it is 0 then files

are

identical

Explain diff command.

SYBCA (SEM:2)

PREPARED BY AMIT PATEL

Page 3

VIDYABHARTI TRUST COLLEGE OF BBA & BCA. UMRAKH


SUB: PROGRAMMING LANGUAGE - I
SHORT EXTERNAL QUESTIONS
A

diff means Difference.

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.

It require two filenames as a arguments.

It examine two files byte by byte.

Action taken by it depends on the option code used.


OPTION

DESRIPTIONS

-l

List option gives detailed list of byte number.

-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.

Each difference display in following format


range1 action range2
< text from file1.
--> text from file2

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.

The action can be change( c ), append ( a ) and delete ( d ).

Action

SYBCA (SEM:2)

Meaning

PREPARED BY AMIT PATEL

Page 4

VIDYABHARTI TRUST COLLEGE OF BBA & BCA. UMRAKH


SUB: PROGRAMMING LANGUAGE - I
SHORT EXTERNAL QUESTIONS
Change( c )

Indicates what action should be taken to make file1 the same as


file2. The line in range1 is replaced by line in range2

Append( a )

Indicates what lines need to be added to make file1 the same as


file2. Append can take place only at the end of file1: they only occur
when file1 is shorter than file2.

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

Change: Replace line 6 in file1 ( Hello) with line 6 in file2

< Hello

(Good Morning).

-->Good Morning
25a26,27

Append: At the end of file1(after line 25) insert lines 26 and


27 from file2.

>bye
>Good Night
78,79d77

Delete: The extra line at the end of file1 should be deleted.

<move one new


< topic

Let's say we have two files, file1.txt and file2.txt.


$ cat file1.txt
I need to buy apples.
I need to run the laundry.
I need to wash the dog.
I need to get the car detailed.

SYBCA (SEM:2)

PREPARED BY AMIT PATEL

Page 5

VIDYABHARTI TRUST COLLEGE OF BBA & BCA. UMRAKH


SUB: PROGRAMMING LANGUAGE - I
SHORT EXTERNAL QUESTIONS
$ cat file2.txt
I need to buy apples.
I need to do the laundry.
I need to wash the car.
I need to get the dog detailed.

$ diff file1.txt file2.txt


2,4c2,4
< I need to run the laundry.
< I need to wash the dog.
< I need to get the car detailed.
-->I need to do the laundry.
>I need to wash the car.
>I need to get the dog detailed.

The first line of the diff output will contain:

Line numbers corresponding to the first file,

Letter (a for add, c for change, or d for delete), and

Line numbers corresponding to the second file.

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)

PREPARED BY AMIT PATEL

Page 6

VIDYABHARTI TRUST COLLEGE OF BBA & BCA. UMRAKH


SUB: PROGRAMMING LANGUAGE - I
SHORT EXTERNAL QUESTIONS
file1.txt:
I need to go to the store.
I need to buy some apples.
When I get home, I'll wash the dog.
file2.txt:
I need to go to the store.
I need to buy some apples.
Oh yeah, I also need to buy grated cheese.
When I get home, I'll wash the dog.

$ diff file1.txt file2.txt


2a3

Oh yeah, I also need to buy grated cheese.

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)

PREPARED BY AMIT PATEL

Page 7

VIDYABHARTI TRUST COLLEGE OF BBA & BCA. UMRAKH


SUB: PROGRAMMING LANGUAGE - I
SHORT EXTERNAL QUESTIONS
4d3
< I promise.

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.

Example 4: Compare two directories dir1 and dir2 .


$ comm dir1 dir2
When we compare two directories, it finds difference between files with the same name in both
directories and displayed.
diff dir1/file1 dir2/file2
2c2
>Hello everybody
---< Hello, Every one
diff dir1/file2 dir2/file2
3a4
> Thanks

Explain comm command.

SYBCA (SEM:2)

PREPARED BY AMIT PATEL

Page 8

VIDYABHARTI TRUST COLLEGE OF BBA & BCA. UMRAKH


SUB: PROGRAMMING LANGUAGE - I
SHORT EXTERNAL QUESTIONS
A

comm means Common.

Used to comparing two sorted files.

Compare each line of the first file with its corresponding line in the second file.

It display the result in three columns.

The left column contains the unique lines in file1.

The center column contains the unique lines in file2.

Right column contains the lines found in both files.


$ cat file1
c.k.Shukla
Chanchal singhvi
S.n.dasgupta
Sumit chakrobarty

$ cat file2
Barun sengupta
C.k.Shukla
Anul Aggarwal
Lalit chowdury
S.n.dagupta

$ comm file2 file1


Barun sengupta
c.k.Shukla
Anil Aggarwal
Chanchal singhvi

SYBCA (SEM:2)

PREPARED BY AMIT PATEL

Page 9

VIDYABHARTI TRUST COLLEGE OF BBA & BCA. UMRAKH


SUB: PROGRAMMING LANGUAGE - I
SHORT EXTERNAL QUESTIONS
Lalit chowdury
s.n. dasgupta
Sumit chakrobarty

Explain find command.


Find command used to search and locate list of files and directories based on

conditions you specify for files that match the arguments.

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

Use name to find file using its name

-atime n

File was accessed n days ago

-mtime n

File was modified n days ago

-size n

File is n blocks big (a block is 512 bytes)

-type c

Specifies file type: f=plain text, d=directory

-fstype typ

Specifies file system type: 4.2 or nfs

-name nam

The filename is nam

-user usr

The file's owner is usr

-group grp

The file's group owner is grp

-perm p

The file's access mode is p (where p is an integer)

SYBCA (SEM:2)

PREPARED BY AMIT PATEL

Page 10

VIDYABHARTI TRUST COLLEGE OF BBA & BCA. UMRAKH


SUB: PROGRAMMING LANGUAGE - I
SHORT EXTERNAL QUESTIONS

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

For ignoring case use I option

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)

PREPARED BY AMIT PATEL

Page 11

VIDYABHARTI TRUST COLLEGE OF BBA & BCA. UMRAKH


SUB: PROGRAMMING LANGUAGE - I
SHORT EXTERNAL QUESTIONS
$ find / -type f ! -perm 777

Use perm option with ! Mark which point neglation .

Find Read Only Files: Find all Read Only files.


$ find / -perm /u=r

Find Executable Files: Find all Executable files.


$ find / -perm /a=x

Find all Empty Files: To file all empty files under certain path.
$ find /tmp -type f empty

Use f to find file with type option

Find all Empty Directories: To file all empty directories under certain path.
$ find /tmp -type d empty

Use d to find directory with type option.

File all Hidden Files: To find all hidden files, use below command.
$ find /tmp -type f -name ".*"

SYBCA (SEM:2)

PREPARED BY AMIT PATEL

Page 12

You might also like