Shell/Files

From Coder Merlin

Files[edit]

Echo to the Screen[edit]

The echo command echoes the argument to the output. Let’s try it. Type: echo "Hello, World!" The echo command dutifully echoes the argument to the screen. Try it now:

john-williams@codermerlin:~/Merlin/hello$  echo "Hello, World!"
Hello, World! 

Create an Empty File[edit]

The touch command creates a new, empty file, if such a file does not already exist. If such a file does already exist, the access and modification times will be set to the current time. Try it now:

john-williams@codermerlin:~/Merlin/hello$  touch newFile.txt
john-williams@codermerlin:~/Merlin/hello$  ls
newFile.txt 

We'll see the cat command more below, but for now it's enough to understand that it will display the contents of a file. Because this is a new, empty file, nothing will be displayed. Try it now:

john-williams@codermerlin:~/Merlin/hello$  cat newFile.txt
john-williams@codermerlin:~/Merlin/hello$  

Redirection[edit]

Remember that we said that almost everything in Unix is either a file or a process and that the screen could be considered as a file. We can redirect the output from echo to another file, one that we'll store in the file system. We do this with the > (greater-than) symbol. Normally, commands will send their output to a device called stdout. This is what the echo command did when we used it earlier. In our case, stdout is printed to our terminal. Let’s try to redirect our output to a file. Type: echo "Hello, World!" > out.txt:

john-williams@codermerlin:~/Merlin/hello$  echo "Hello, World!" > out.txt


ObserveIcon.png
Observe, Ponder, and Journal : Section 6
  1. Was "Hello, World!" printed to the screen as before? If not, why not? What do you think happened to the text?


No output was displayed; we just received a new prompt. Let’s use the ls command to take a look:

john-williams@codermerlin:~/Merlin/hello$  ls
newFile.txt out.txt 

We can see that the directory contains a single file, "out.txt" which was created by the echo command. How can we see what's contained within this file?

Quickly View File Contents[edit]

To quickly view the contents of a file we can use the cat command. The cat command concatenates files together and then sends the output to stdout. (Remember that in our case this is the screen.) Let’s try it. Type "cat out.txt".

john-williams@codermerlin:~/Merlin/hello$  cat out.txt
Hello, World! 

Append a Small Amount of Text to an Existing File[edit]

If we already have an existing file with some text we can append (add text to the end) of this file by using echo and redirection as before but with two "greater-than" symbols adjacent to one another. Try it now. Type: echo "Hello, again!" >> out.txt

john-williams@codermerlin:~/Merlin/hello$  echo "Hello, again!" >> out.txt
john-williams@codermerlin:~/Merlin/hello$  

Let’s move back up to our home directory. Remember that we can do this by typing "cd" without any arguments.

john-williams@codermerlin:~/Merlin/hello$  cd
john-williams@codermerlin:~$  



CoderMerlin™ proudly recommends:
HintIcon.png

Your purchase via these links helps keep  Coder Merlin™ 's orbuculum ablaze. As an Amazon Associate we earn from qualifying purchases. Thank you for your support!


Designed with pride in Silicon Valley, CA, USA