DEV Community

Cover image for Don't fear the command line: Redirecting And Appending
Dionysia Lemonaki
Dionysia Lemonaki

Posted on

Don't fear the command line: Redirecting And Appending

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

Top comments (17)

Collapse
 
schlenges profile image
Meike

"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! 🤩

Collapse
 
deniselemonaki profile image
Dionysia Lemonaki

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!

Collapse
 
schlenges profile image
Meike

I love it!!! 😂

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

Collapse
 
chttrjeankr profile image
Ankur Chattopadhyay • Edited

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

Collapse
 
deniselemonaki profile image
Dionysia Lemonaki

Thank you so much for your kind words! 🙏

Collapse
 
phantas0s profile image
Matthieu Cneude • Edited

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

Collapse
 
deniselemonaki profile image
Dionysia Lemonaki

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

Collapse
 
chriswilkinsoncodes profile image
chris wilkinson

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

Collapse
 
deniselemonaki profile image
Dionysia Lemonaki

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

Collapse
 
shravan20 profile image
Shravan Kumar B

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

Collapse
 
deniselemonaki profile image
Dionysia Lemonaki

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

Collapse
 
shravan20 profile image
Shravan Kumar B

I really love the way you are exploring the CLI

Collapse
 
moopet profile image
Ben Sinclair

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.

Collapse
 
deniselemonaki profile image
Dionysia Lemonaki

Thanks for adding that.

Collapse
 
tilakmaddy_68 profile image
Tilak Madichetti

😏

Collapse
 
functional_js profile image
Functional Javascript

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

Collapse
 
deniselemonaki profile image
Dionysia Lemonaki

Thanks!