# Adding Text to First and Last Line of File in Linux

### Hi everyone,

Let’s assume that you have many files to that you wanted to add some text to the first line or end line.

If you are a Linux user, this is easy to do. I am using `sed`  and `tee` commands to add something to the file.

Firstly, I would like to show you how to add some text to the first line of the file. For this purpose, I am going to use the `sed` command.

Before beginning, I created a file that has some text like the one below This is our sample file.

![sample-file](https://cdn.hashnode.com/res/hashnode/image/upload/v1673447883916/0c577bfe-bbc1-4fca-a666-f6b078cbc05c.png align="center")

If you would like to add a test to the first line of your file, you can use the `sed` command like below. The below command will insert the text in the first line, which is defined `1i` .

```bash
sed -i '1i this-text-will-be-adding-to-the-first-line' test-file.txt
```

You can also add the text that you need to more than one file by just changing the file name to the pattern you need. Let’s assume that you have some files that name is `test1.txt - test2.txt, test3.txt` . You just change `test*` .

```bash
sed -i '1i this-text-will-be-adding-to-the-first-line' test-file.txt
```

![append to the first line](https://cdn.hashnode.com/res/hashnode/image/upload/v1673448041432/ad11186c-a19d-463f-a861-1a4f5602cc02.png align="center")

`tee` command will help you more about adding to the end of the line of your files that you would like to change.

```bash
echo 'this-text-will-be-adding-to-the-end-of-the-file' | tee -a test-file.txt
```

You can also add test to more than one file with that command, just you need to change the file name like the pattern you need. Let’s assume that you have some files that name is `test1.txt - test2.txt, test3.txt` . You just change `test*` .

```bash
echo 'this-text-will-be-adding-to-the-end-of-the-file' | tee -a test*
```

![append to the end line](https://cdn.hashnode.com/res/hashnode/image/upload/v1673448097676/2b5b639b-281a-4786-b365-35a02201e1d1.png align="center")

If you are working on many files, these commands help you to do something easily and fastly. For more information about the commands that I used, please use `man sed` and `man tee` .

I hope you enjoy while reading.
