Don't fear the command line: Redirecting And Appending
Tags: codenewbie, beginners
Published: 2020-07-21 09:46:07

In my last post I talked briefly about a very important concept when it comes to the command line,Standard Input and Standard Output. If you would like to have a read you can catch up here :
{% link https://dev.to/deniselemonaki/don-t-fear-the-command-line-making-it-echo-30o1
%}

This post is an introduction on the different  ways redirecting input and output can be achieved, what appending is and how it works and in the meantime we will explore ways to check our work during the process and make sure we're on the right path.

![alrighty then](https://media.giphy.com/media/5hc2bkC60heU/giphy.gif)

## Redirection

Through redirection we direct the input and output of a command to and from different files. It essentially reroutes standard input, standard output and standard error to and from different locations.

## How does it work?

To achieve redirection we use `>` which is called the `redirect operator`.Let's take an example:

```console
echo "hello, world" > world.txt
```

Here `hello, world` is entered as the standard input. The `redirect operator` takes the string output from echo which is the `hello, world` printed on the screen, and redirects it to a file called `world.txt`. This way a new file is created containing already text.

## Appending
To add more lines of text to a file that already exists we use `>>` , the `appending operator`.
 
```console

echo "I am learning the command line" >> world.txt

```

The appending operator is a useful addition(see what I did there?) to the normal  redirect operator.It adds the line to the end of the existing file. It can be used when we want to build up a file gradually, adding new parts to the file as we go along.

![more!](https://media.giphy.com/media/3oKIPEh5Lk3RGSIFEI/giphy.gif)

## CAT

To view the contents of files on the screen we use the command `cat`. That use of the command is common, however as the  name is short for `concatenate` , we can also use  it to combine  the contents of multiple files and therefore it takes multiple arguments. In that case the order matters and  we can reverse the order of the files, depending on what content we want to go first.

```console
cat world.txt
``` 
In the above example the contents of the file world.txt are displayed.

```console
 cat world.txt > coding.txt
```

In this case, `cat` takes the contents of the file on the left and redirects it to the file on the right.The redirect operator overwrites any original content that may exist in `coding.txt`. If the file `coding.txt` doesn't exist already, it will be created in the process.The new file will be located in the current working directory and it's contents will be  the exact same as those of `world.txt`


```console
cat world.txt hello.txt > coding.txt
```
In this case the output of `world.txt` goes first in the file `coding.txt` followed by the output of `hello.txt`

### Copy or Cat?

```console
cp world.txt coding.txt
```

can work similar to 

```console
 cat world.txt > coding.txt
```

Both commands overwrite the content of `coding.txt` with that of `world.txt`.The difference between them is `cp` works like `copy and paste`,it copies files and directories. `cat` is more like `cut and paste`,it is transferring files and directories and creating new files in the process of doing that.

![cat and computer](https://media.giphy.com/media/VbnUQpnihPSIgIXuZv/giphy.gif)

### Check yourself

Whenever redirecting or appending contents of files, it's always a good idea to use the `cat` command to make sure that our files have the right content and we haven't made any mistakes.


Thank you for reading! 😃