Contact for queries :
 

I/O direction commands in Linux

There are also a few characters you can use to direct or redirect output of commands.
These characters are

  • > Directs output to a file or device (overwrites if the file exists)
  • < Directs input from the file or device
  • >> Appends output or text to a file (creates if the file doesn’t exist)
  • | Pipes the output of one command to another
  • && Combines commands

1.  Use the echo command to output some text to a file:

# echo “This is some sample text” > file_example

Normally, the echo command just displays the text you have given it back to the screen, but because you are using the output direction character,  the output is pushed to the file specified instead.
2. Verify that the text was output correctly by viewing the contents of the file:

# cat file_example
This is some sample text

3. Display the third field of the text using the space as a delimiter:

# cut -d “ “ -f3 file_example

4. Combine the two commands into a single line:

# cat file_example | cut -d “ “ -f3 file_example

5. Execute one command and then another:

# echo “This is some more text” > file_example && cut -d “ “ -f3 file_example

Check the current contents of the sample file:

# cat file_example
This is some more text

6. Output some more text to this file:

# echo “Different text” > file_example

8. Verify the contents of the file again:

# cat file_example
Different text

What happened to the original text? When you use the >, the output is sent to a file or device. However, it always overwrites what is in the current file or device. If you want to append text, you can use the same
character twice.
9. Append text to the file instead of overwriting it:

# echo “My original text” >> file_example

10. Verify the contents of the file:

# cat file_example
Different text
My original text

11. Run the same exact command again and view the contents of the file:

# echo “My original text” >> file_example && cat file_example
Different text
My original text
My original text

Notice there are now two lines with the same text. What if this was a config file for a service with duplicate data? In that case, you can use the uniq command to pull only unique lines from a file, making sure that
there are no duplicates.
Step 7. View only unique lines in the sample file, create a new file based on the output, and view the contents of this new file:

# uniq file_example > uniq_file && cat uniq_file
Different text
My original text

 
 

November 14, 2015

0 responses on "I/O direction commands in Linux"

Leave a Message

Your email address will not be published. Required fields are marked *

About iGURKUL

IGURKUL I.T. Training Hub offering various Career Certification courses in Computer Networking, Unix, Linux, Cloud Computing and DevOps Technologies. With its rich experience in IT training service sector, iGURKUL has been able to set Industry best practices in IT Training for the past five years.

In Past five years, more than 5000 professionals have been trained by iGURKUL for System administration, Cloud Computing and DevOps Skill set through our Online Training portal www.unixadminschool.com. And , each day , more than 10000 working professionals from all over the globe visiting our knowledge base www.unixadminschool.com/blog for the best practices and Knowledge learning.

Popular Tags

Who’s Online

There are no users currently online
top
Template Design © igurkul I.T. solutions. All rights reserved.
X