Introduction to Bash, VIM & Regex

Writing and Reading Files

Introduction to Bash, VIM & Regex

Check out a free preview of the full Introduction to Bash, VIM & Regex course

The "Writing and Reading Files" Lesson is part of the full, Introduction to Bash, VIM & Regex course featured in this preview video. Here's what you'd learn in this lesson:

James introduces file redirects. Redirects can write the output of a command to a file, overwrite a file with new contents if it already exists.


Transcript from the "Writing and Reading Files" Lesson

>> James Halliday: Okay so we already saw the echo command a little bit. This is gonna be important now for piping,
>> James Halliday: And redirects. So one of the first things that we can experiment with is if we have a command, it's gonna print by default to standard output. So you'll be able to see it and read it on your terminal.

So if I have a command, just say a simple echo commands, echo hello there, it's gonna print a message. If I wanna save that output to a file, I can use a greater than. And then specify a file name where that output is going to be saved. So I'll call it hello.txt.

>> James Halliday: Now I don't see the output on my standard out anymore. But if I cat out the contents of that file, then the file hello.txt contains the text 'hello there'. That's an example of output redirect. So this works for any kind of command. So, for example, we can use the output of the ls command.

We can write it to a file called list.txt. You might notice that sometimes the output that you see on the screen, like for the ls command, is gonna be different than the output that you see in the file. This is because sometimes the programs know whether they're on it's called a TTY or not.

Which means whether they're being run interactively or whether they're being run in this kind of scripted way. So, commands like ls, when they're being run not on a TTY in this scripted fashion by using redirects or pipes, will output the same as if you'd done ls -1. In this case which just prints out the files line by line.

Cuz that's generally more useful for programs to read than something with columns in it. So < redirects standard out to a file.
>> James Halliday: There's also another kind of redirect which does appending. So if you put two greater thans side by side, then it will add the standard out from whatever you're piping in to the end of the file.

So if I want to add another message to my hello.txt, which, I guess, can turn into 90s style CGI guest book. I can make another message. So I'll say, whatever and double greater than, then hello.txt. Now if I cat hello.txt, I see the original message and then the new message.

>> James Halliday: And a third message, there we go. So now, we can, sort of, accumulate contents. So maybe if you have a little program that you wanna run periodically, and you wanna log the contents of, like, whatever that program outputs to a log file, the append redirectives is handy to do that.

>> Speaker 2: How about to the beginning of the file, I just tried appending to the beginning with a single greater than just over on everything.
>> James Halliday: Right, well, so, a single greater than overwrites the file. If you wanna append to the beginning, it's a little bit complicated. You'd have to compose some different kinds of operations.

I guess if I wanted to append, I would probably use a subshell to do that. So put my message at the beginning and then do cat hello.txt. And then overwrite to a different file that's not called hello.txt. And then move hello.txt{_,} to be hello.txt. That's how I would do pre-pending, it's more complicated.

>> Speaker 2: So what do you mean to say? So I won't do that. [LAUGH]
>> James Halliday: You can do that, but maybe once we get to these concepts, you can do it. Just, make sure it works here. There we go, at the beginning. Yeah, so putting stuff at the beginning of the file, a lot harder than putting it at the end of the file.

This also has to do with how,
>> James Halliday: The operating system works, it's generally cheaper to put things at the end of a file than at the beginning. Because if you put something in the beginning, you have to smush everything else down. You have to push it down and reallocate a new file.

But if you put things at the end, the operating system will just allocate a new block or write to the end of an existing block. Because things are done in blocks. Sort of a linked structure internally is what's going on. Okay, so those are the two kinds of output redirects.

Greater than and double greater than to append. There's also a kind of input redirect, which is a less than, yeah. So the input redirect takes the contents of a file and sort of types it into another file, as if you'd typed in the contents of that file on a keyboard for yourself.

>> James Halliday: So, if, for example, we wanna, so suppose that the wc commands didn't have an option to pass in a file name. So imagine if we couldn't do things like wc -c hello.txt. We can only do things like wc- c and it would read for standard in. So if we have programs like that you can use an input reader direct with less than.

And you can give the name of the file. And then it will write the contents of the file hello.txt into the standard input of the wc command. In this case we know that hello.txt is 52 bytes. If you have these kind of commands where they maybe don't know how to read files, but they know how to read from standard in, you can still copy files into the standard in.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now