Difference between revisions of "Shell/Files"

From Coder Merlin
imported>Anton-krom
m (Editorial review and minor corrections)
 
(2 intermediate revisions by 2 users not shown)
Line 1: Line 1:
=== Echo to the Screen ===
=== Echo to the Screen ===
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:
The echo command echoes the argument to the output. Let's try it. Type: '''echo "Hello, World!"''' and press {{Key|RETURN}}. The echo command dutifully echoes the argument to the screen. Try it now:


{{ConsoleLine|john-williams@codermerlin:~/Merlin/hello$ |echo "Hello, World!"}}
{{ConsoleLine|john-williams@codermerlin:~/Merlin/hello$ |echo "Hello, World!"}}
Line 6: Line 6:


=== Create an Empty File ===
=== Create an Empty File ===
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:
The touch command creates a new, empty file, if such a file does not already exist. If such a file does exist, the access and modification times are set to the current time. Try it now:


{{ConsoleLine|john-williams@codermerlin:~/Merlin/hello$ |touch newFile.txt}}
{{ConsoleLine|john-williams@codermerlin:~/Merlin/hello$ |touch newFile.txt}}
Line 12: Line 12:
{{ConsoleLine|newFile.txt|}}
{{ConsoleLine|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:
We'll see the '''cat''' command more below, but for now it's enough to understand that it displays the contents of a file. Because this is a new, empty file, nothing is displayed. Try it now:
{{ConsoleLine|john-williams@codermerlin:~/Merlin/hello$ |cat newFile.txt}}
{{ConsoleLine|john-williams@codermerlin:~/Merlin/hello$ |cat newFile.txt}}
{{ConsoleLine|john-williams@codermerlin:~/Merlin/hello$ |}}
{{ConsoleLine|john-williams@codermerlin:~/Merlin/hello$ |}}


=== Redirection ===
=== Redirection ===
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''':
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 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''' and press {{Key|RETURN}}:


{{ConsoleLine|john-williams@codermerlin:~/Merlin/hello$ |echo "Hello, World!" > out.txt}}
{{ConsoleLine|john-williams@codermerlin:~/Merlin/hello$ |echo "Hello, World!" > out.txt}}
Line 23: Line 23:


{{Observe|: Section 6|
{{Observe|: Section 6|
# Was "Hello, World!" printed to the screen as before? If not, why not? What do you think happened to the text?
# Was "Hello, World!" printed to the screen as before? If not, why not? What do you think happened to the text?
}}
}}


Line 33: Line 33:
{{ConsoleLine|newFile.txt out.txt|}}
{{ConsoleLine|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?
We can see that the directory contains one file, the "out.txt" that we created with the echo command. How can we see what's in this file?


=== Quickly View File Contents ===
=== Quickly View File Contents ===
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".
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 the stdout (standard output). (Remember that in our case, this is the screen.) Let's try it. Type "cat out.txt" and press {{Key|RETURN}}.


{{ConsoleLine|john-williams@codermerlin:~/Merlin/hello$ |cat out.txt}}
{{ConsoleLine|john-williams@codermerlin:~/Merlin/hello$ |cat out.txt}}
Line 42: Line 42:


=== Append a Small Amount of Text to an Existing File ===
=== Append a Small Amount of Text to an Existing File ===
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
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 next to each another. Try it now. Type: echo "Hello, again!" >> out.txt


{{ConsoleLine|john-williams@codermerlin:~/Merlin/hello$ |echo "Hello, again!" >> out.txt}}
{{ConsoleLine|john-williams@codermerlin:~/Merlin/hello$ |echo "Hello, again!" >> out.txt}}
{{ConsoleLine|john-williams@codermerlin:~/Merlin/hello$ |}}
{{ConsoleLine|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.
Let's move back up to our home directory. Remember that we can do this by typing "cd" without any arguments.


{{ConsoleLine|john-williams@codermerlin:~/Merlin/hello$ |cd}}
{{ConsoleLine|john-williams@codermerlin:~/Merlin/hello$ |cd}}

Latest revision as of 15:23, 25 August 2023

Within these castle walls be forged Mavens of Computer Science ...
— Merlin, The Coder

Echo to the Screen[edit]

The echo command echoes the argument to the output. Let's try it. Type: echo "Hello, World!" and press RETURN. 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 exist, the access and modification times are 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 displays the contents of a file. Because this is a new, empty file, nothing is 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 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 and press RETURN:

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


ObserveObserveIcon.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 one file, the "out.txt" that we created with the echo command. How can we see what's in 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 the stdout (standard output). (Remember that in our case, this is the screen.) Let's try it. Type "cat out.txt" and press RETURN.

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 next to each 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:~$