# Pipe in Linux

-> The output of one commad redirect it to the input of other command

<figure><img src="https://3197044210-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FwGS99FW5vqaPqYTGiSZC%2Fuploads%2Fcyvd4l44lcLf1MmbKSlN%2FScreenshot%202024-01-13%20at%203.51.22%E2%80%AFAM.png?alt=media&#x26;token=13b9d247-7161-47c2-9df3-d7aedf03a38a" alt=""><figcaption></figcaption></figure>

## Pipe syntax

We use '|' symbol to separate two commands. Output of first command passed to second command

```
command1 | command2
```

<figure><img src="https://3197044210-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FwGS99FW5vqaPqYTGiSZC%2Fuploads%2F3byLpXAazgAVhwAHTMEH%2FScreenshot%202024-01-13%20at%203.58.26%E2%80%AFAM.png?alt=media&#x26;token=7d346c2c-32e4-410d-b262-c3eeedbf0a81" alt=""><figcaption></figcaption></figure>

<figure><img src="https://3197044210-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FwGS99FW5vqaPqYTGiSZC%2Fuploads%2FPnL1HxmwX7kBrNO3n2PB%2FScreenshot%202024-01-13%20at%203.56.11%E2%80%AFAM.png?alt=media&#x26;token=c58e0586-fe20-4b12-aa5b-73c560340a3a" alt=""><figcaption></figcaption></figure>

## Q1. Find number of files in a directory

```sh
ls -1 | wc -l
```

### Breakdown of the command used above

```sh
ls -1 
```

The command lists all files in a directory in a single column i.e. a single line represents name of one file.

<figure><img src="https://3197044210-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FwGS99FW5vqaPqYTGiSZC%2Fuploads%2FZuqNI1HT3yQqgkcw8NVa%2FScreenshot%202024-01-13%20at%204.05.39%E2%80%AFAM.png?alt=media&#x26;token=3574b135-a39a-40a0-908f-dd26069e5447" alt=""><figcaption><p>Output of ls command</p></figcaption></figure>

```sh
wc -l
```

This prints the number lines in file. Together the commands will print the number of files in a directory.

<figure><img src="https://3197044210-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FwGS99FW5vqaPqYTGiSZC%2Fuploads%2FrPMnSBIhgPP1wspSpJ9G%2FScreenshot%202024-01-13%20at%204.08.12%E2%80%AFAM.png?alt=media&#x26;token=b90ac68c-79c4-43c3-8405-83405306b011" alt=""><figcaption></figcaption></figure>

## Q2. Combine two files and sort them

Combining 2 files using cat command and then sorting them using pipe

```sh
cat file1.txt file2.txt | sort
```

<figure><img src="https://3197044210-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FwGS99FW5vqaPqYTGiSZC%2Fuploads%2FfVYTc4lZpeFv5PioYnNW%2FScreenshot%202024-01-13%20at%204.14.13%E2%80%AFAM.png?alt=media&#x26;token=92a4f2cd-c568-4ec9-a8fa-5ca3745e8b90" alt=""><figcaption></figcaption></figure>

<figure><img src="https://3197044210-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FwGS99FW5vqaPqYTGiSZC%2Fuploads%2FZpxnv7fpIDEnNhvYcXpw%2FScreenshot%202024-01-13%20at%204.15.21%E2%80%AFAM.png?alt=media&#x26;token=67318f91-20d0-444c-b9f0-00f510ebe744" alt=""><figcaption><p>sorted output of concatenation of two files</p></figcaption></figure>

## Q3. Unique names in a file

{% code fullWidth="true" %}

```sh
cat file3.txt | sort | uniq
```

{% endcode %}

We can only use `uniq` command with sorted data only

<figure><img src="https://3197044210-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FwGS99FW5vqaPqYTGiSZC%2Fuploads%2FpQBc2o9mt459NmUTipFg%2FScreenshot%202024-01-13%20at%204.24.01%E2%80%AFAM.png?alt=media&#x26;token=e26270e6-beb9-4f52-bcd2-5c2cf9f86eb4" alt=""><figcaption></figcaption></figure>

<figure><img src="https://3197044210-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FwGS99FW5vqaPqYTGiSZC%2Fuploads%2FQEs3wm7QXVwSSRpKKn3I%2FScreenshot%202024-01-13%20at%204.22.44%E2%80%AFAM.png?alt=media&#x26;token=2bd7c2e7-8099-4ade-9728-d2909e6b232b" alt=""><figcaption></figcaption></figure>

## Q4. See 30-37 lines in a file

`head -37` to view first 37 lines , `tail -7` to view last 7 lines from the 37 lines, which makes 30-37

<figure><img src="https://3197044210-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FwGS99FW5vqaPqYTGiSZC%2Fuploads%2FpxGCSKZk2lYL9u9ALnFM%2FScreenshot%202024-01-13%20at%204.54.56%E2%80%AFAM.png?alt=media&#x26;token=b5f85e8b-64a8-421f-aa68-99f7d82165eb" alt=""><figcaption></figcaption></figure>

## Tee command -&#x20;

`tee` command reads from ***stdin*** and saves the content to a file and also displays it to ***stdout***

<figure><img src="https://3197044210-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FwGS99FW5vqaPqYTGiSZC%2Fuploads%2Fzr0vV4yD3ov9nta39mhX%2Fimage.png?alt=media&#x26;token=cdeb503c-439f-454b-a7da-bff9839b97b8" alt=""><figcaption></figcaption></figure>

<figure><img src="https://3197044210-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FwGS99FW5vqaPqYTGiSZC%2Fuploads%2FIgbD22XSMBpw5lkcivqo%2FScreenshot%202024-01-13%20at%205.03.51%E2%80%AFAM.png?alt=media&#x26;token=e409ba5c-cfb7-401b-af5f-c6f9e727f1be" alt=""><figcaption><p>writes and displays output of the ls command</p></figcaption></figure>
