Don't fear the command line: Redirecting And Appending

Dionysia Lemonaki - Jul 21 '20 - - Dev Community

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 :

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

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:

echo "hello, world" > world.txt
Enter fullscreen mode Exit fullscreen mode

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.


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

Enter fullscreen mode Exit fullscreen mode

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!

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.

cat world.txt
Enter fullscreen mode Exit fullscreen mode

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

 cat world.txt > coding.txt
Enter fullscreen mode Exit fullscreen mode

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

cat world.txt hello.txt > coding.txt
Enter fullscreen mode Exit fullscreen mode

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?

cp world.txt coding.txt
Enter fullscreen mode Exit fullscreen mode

can work similar to

 cat world.txt > coding.txt
Enter fullscreen mode Exit fullscreen mode

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

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! 😃

. . . . . . . . .
Terabox Video Player