loading...
Cover image for Don't fear the command line: Redirecting And Appending

Don't fear the command line: Redirecting And Appending

deniselemonaki profile image Dionysia Lemonaki ใƒป3 min read

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

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

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

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

 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

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?

cp world.txt coding.txt

can work similar to

 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

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! ๐Ÿ˜ƒ

Posted on by:

Discussion

pic
Editor guide
 

"The appending operator is a useful addition" ๐Ÿ˜‚

I can't repeat it often enough, I love your way of explaining things! Thank you for bringing up the copy vs cat thing, too, I finally get it and won't have to look them up again! That's so awesome! Thank you for another superb addition to this series! I so much hope you're still planning on more! ๐Ÿคฉ

 

I made my first (tragic) tech joke haha๐Ÿ˜‚

Thank you Meike for always being so supportive with every article I've written, it means so much to me!

 

I love it!!! ๐Ÿ˜‚

Of course! I very selfishly want you to write a whole lot more, because my education truly benefits from it! ๐Ÿ˜

 

Found this one on my feed and turned up reading the other 3 articles of the series too. ๐Ÿ”ฅ
I got really absorbed into reading these succinct but appropriate descriptions for the command line commands. ๐Ÿ‘๐Ÿป

I truly wish to see more of these! ๐ŸŒŸ

 

Thank you so much for your kind words! ๐Ÿ™

 

Nice article! I'm doing basically everything in the shell. It's really fun, and it's easy to automate.

 

Thank you! It really does make certain tasks so much easier!

 

Thank you for this! I'm trying to get more adept at using the command line and your posts are helping. ๐Ÿ˜Ž

 

Thank you so much for your comment,I am so glad you are finding them helpful!

 

Hi, Can I use your CLI contents for putting them as learning materials in twitter?

 

Uum, no I wouldn't want you to use my content.

 

I really love the way you are exploring the CLI

 

The other big difference between using cp src dest and cat src > dest is that dest's file permissions will likely differ between the two operations.

 

Thanks for adding that.

 
 

It's always good to get a refresher on the basics.
Keep up the phenomenal work Dionysia.