Creating Files

In Creating and Reading Files, you learned how to use the echo command and redirection to create files. This is one of many ways to create files on the command line. Let’s look at a few alternatives.

If you only need to create an empty file, you can use the touch command. This command is designed to update the timestamp of a file. It’s common for programs to watch a file for changes and then react to those changes by executing a process. For example, you might have a process that runs tests whenever a file changes, but you don’t actually want to open the file and make a change. You can use touch to modify the file without actually changing the contents. This would then trigger the program or process monitoring the file.

However, if the specified file doesn’t exist, the touch command creates the file. This makes touch a very popular tool for creating files quickly.

Test it out. Navigate to your home directory:

 $ ​​cd

Now, use touch to create a new file named file.txt:

 $ ​​touch​​ ​​file.txt

Verify that it exists by using the ls -lh command:

 $ ​​ls​​ ​​-lh​​ ​​file.txt
 -rw-r--r-- 1 brian brian 0 Mar 2 12:50 file.txt

The file doesn’t have any contents, but it was created successfully. This is a handy way to create a blank file that you can then modify elsewhere.

Remember that touch updates a file’s timestamp whenever you run it. Wait a minute and run the touch command on this file again. Then get a new listing:

 $ ​​touch​​ ​​file.txt
 $ ​​ls​​ ​​-alh​​ ​​file.txt
 -rw-r--r-- 1 brian brian 0 Mar 2 12:51 file.txt

You’ll notice that the timestamp has changed.

You can use touch to operate on more than one file at once. All you have to do is provide it with a list of filenames, separated with spaces. Give it a try:

 $ ​​touch​​ ​​index.html​​ ​​about.html​​ ​​style.css

This creates the three files, index.html, about.html, and style.css in the current directory. Verify this with the ls command:

 $ ​​ls​​ ​​file.txt​​ ​​index.html​​ ​​about.html​​ ​​style.css
 about.html file.txt index.html style.css

You’re not limited in where you create the files. You can create a file in the current directory and in the Documents directory as well, all with a single command:

 $ ​​touch​​ ​​this_goes_in_home.txt​​ ​​Documents/this_goes_in_Documents.txt

By specifying a relative path or a full path to a directory, you can create files anywhere on your filesystem.

Creating Files with Content

The touch command creates blank text files, and as you already know, you can capture the output of programs to a text file by redirecting the program’s output to a file. Let’s review this by creating a new text file in the current directory that contains the text “Hello, World”. Use the echo command to print out the text and then redirect it to a file:

 $ ​​echo​​ ​​'Hello, World'​​ ​​>​​ ​​hello.txt

If the file hello.txt doesn’t exist, it gets created. If it does exist, it gets overwritten. So you have to be incredibly careful with this command. You could accidentally erase a file’s contents this way.

To append text to the file instead of overwriting its contents, use >> instead of >. So, to append another line to the file hello.txt, you can do this:

 $ ​​echo​​ ​​'How are you today'​​ ​​>>​​ ​​hello.txt

You can use the > and >> symbols to redirect any program’s output messages to a file. For example, if you wanted to save the list of files in the current directory to a file, it’s as easy as:

 $ ​​ls​​ ​​-alh​​ ​​>​​ ​​files.txt

You can then append the output of another command to the same file using >>:

 $ ​​ls​​ ​​-alh​​ ​​~/Documents​​ ​​>>​​ ​​files.txt

The files.txt file will contain the output of both commands. You’ll dive into how this works in greater detail in Chapter 5, Streams of Text.

Writing Multiple Lines to a File

You can create files from program output, and you can create a new file with a line of text, but you can also create a new file with multiple lines of text right from the command line, without opening a text editor.

In Creating and Reading Files, you used the cat command to view the contents of files. The cat command reads the contents of files and displays them to the screen. However, you can use cat to create new files. Let’s create a simple text file with several lines. Execute this command:

 $ ​​cat​​ ​​>​​ ​​names.txt

After pressing Enter, you’ll see a different prompt and a flashing cursor:

 >

The cat command is waiting for input. Type the following lines, pressing Enter after each one:

 >​​ ​​Homer
 >​​ ​​Marge
 >​​ ​​Bart
 >​​ ​​Lisa
 >​​ ​​Maggie

After the last line of the file, press Enter one more time, then press Ctrl+d. This saves the contents to the file.

Use cat again to view the file to ensure the contents were saved:

 $ ​​cat​​ ​​names.txt
 Homer
 Marge
 Bart
 Lisa
 Maggie

Here’s how that worked. You told cat to start accepting text from the keyboard. Every time you pressed the Enter key, cat saved the line to the file. Pressing Enter after the last line (Maggie) saved that line to the file as well. The sequence Ctrl+d exits the process. If you forget to press Enter after the last line, you won’t save the last line to the file.

To avoid that issue entirely, tell cat to terminate when it sees a specific string on its own line. Try this command:

 $ ​​cat​​ ​​<<​​ ​​'EOF'​​ ​​>​​ ​​names.txt
 >​​ ​​Homer
 >​​ ​​Marge
 >​​ ​​Bart
 >​​ ​​Lisa
 >​​ ​​Maggie
»>​​ ​​EOF

Instead of a blank line at the end, you use the text EOF. Then, when you invoke cat, you tell it to take in keyboard input until it sees the line EOF. The text EOF is short for “end of file,” and it’s mostly a convention; you can use any combination of characters you want, like DONE or END.

This is a great way to create files without switching to your GUI and breaking your flow. You’ll use this throughout the book and explore how it works in Chapter 5, Streams of Text.

Combining Files

The cat command is the go-to tool for looking at the contents of small files. If you send multiple files to the cat command, it will read them all in the order you specified. Try it. Create two files in your current directory:

 $ ​​echo​​ ​​"Hello"​​ ​​>​​ ​​hello.txt
 $ ​​echo​​ ​​"Goodbye"​​ ​​>​​ ​​goodbye.txt

Now, use the cat command to read both files:

 $ ​​cat​​ ​​hello.txt​​ ​​goodbye.txt
 Hello
 Goodbye

As you can see, the output shows the content of both files. The cat command is actually designed to concatenate files. And as you’ve seen already, you can redirect standard output to a file using the > symbol.

Let’s see this in action. Websites often have a common header and footer, and instead of repeating that content in every file, you can store the common contents in templates, and then place the page-specific bits in their own files. Try it out.

First, use cat to create a new file named header.html with the following content:

 <!DOCTYPE html>
 <html lang=​“en-US”​>
  <head>
  <meta charset=​“utf-8”​>
  <title>My Site</title>
  </head>
  <body>
  <div class=​“content”​>
  <header>
  <h1>AwesomeCo</h1>
  </header>

Create it with the following command:

 $ ​​cat​​ ​​<<​​ ​​'EOF'​​ ​​>​​ ​​header.html
 >​​ ​​<!DOCTYPE​​ ​​html>
 >​​ ​​<html​​ ​​lang=“en-US”>
 >​​ ​​<head>
 >​​ ​​<meta​​ ​​charset=“utf-8”>
 >​​ ​​<title>My​​ ​​Site</title>
 >​​ ​​</head>
 >​​ ​​<body>
 >​​ ​​<div​​ ​​class=“content”>
 >​​ ​​<header>
 >​​ ​​<h1>AwesomeCo</h1>
 >​​ ​​</header>
 >​​ ​​EOF

Next, create a file named footer.html with the following content:

  <footer>
  <small>Copyright &copy; 2019 AwesomeCo</small>
  </footer>
  </div>
  </body>
 </html>

Use the same method to create this file too:

 $ ​​cat​​ ​​<<​​ ​​'EOF'​​ ​​>​​ ​​footer.html
 >​​ ​​<footer>
 >​​ ​​<small>Copyright​​ ​​&copy;​​ ​​2019​​ ​​AwesomeCo</small>
 >​​ ​​</footer>
 >​​ ​​</div>
 >​​ ​​</body>
 >​​ ​​</html>
 >​​ ​​EOF

Finally, create a file named main.html that contains this:

 <main>
  <h2>Welcome!</h2>
  <p>This is the main page!</p>
 </main>

Here’s the command:

 $ ​​cat​​ ​​<<​​ ​​'EOF'​​ ​​>​​ ​​main.html
 >​​ ​​<main>
 >​​ ​​<h2>Welcome!</h2>
 >​​ ​​<p>This​​ ​​is​​ ​​the​​ ​​main​​ ​​page!</p>
 >​​ ​​</main>
 >​​ ​​EOF

Now, join the three files to create a new file named index.html using cat:

 $ ​​cat​​ ​​header.html​​ ​​main.html​​ ​​footer.html​​ ​​>​​ ​​index.html

Print out the contents of the new file to verify that it contains the output you expected:

 $ ​​cat​​ ​​index.html
 <!DOCTYPE html>
 <html lang=“en-US”>
  <head>
  <meta charset=“utf-8”>
  <title>My Site</title>
  </head>
  <body>
  <div class=“content”>
  <header>
  <h1>AwesomeCo</h1>
  </header>
 <main>
  <h2>Welcome!</h2>
  <p>This is the main page!</p>
 </main>
 
 
 
  <footer>
  <small>Copyright &copy; 2019 AwesomeCo</small>
  </footer>
  </div>
  </body>
 </html>

The lines in all three files were combined into a single file. The indentation looks off since we didn’t indent the contents of the main.html in this example.

Reading small files is easy. Let’s explore looking at larger ones.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.194.57