Introduction to Bash, VIM & Regex

Deleting Files and Counting Words

Introduction to Bash, VIM & Regex

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

The "Deleting Files and Counting Words" 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 the rm command file to remove files and directories. Then James shows how to use the wc command to count the number of lines, words, and bytes in a file.


Transcript from the "Deleting Files and Counting Words" Lesson

>> James Halliday: So another fun command. Hopefully, you've all by now made a lot of directories [LAUGH] that you wanna get rid of, so you can use the rm command to do that. Be very careful with the rm command because it deletes files.
>> James Halliday: So if we wanna delete a file.

So like I say, I wanna delete boop.txt, you can do rm boop.txt. Now that file is gone.
>> James Halliday: If you wanna delete a directory, there is a few things that you have to know. So if I try to delete this directory called good, the rm command won't let me do that because good is a directory.

There's also an rmdir command, which will also fail because the directory is not empty. So if you have an empty directory, like here, I'll just make a new one. So I can rmdir that directory. Or another way to do that, if I create that directory again, it's to do rm -r, which is recursively remove something.

>> James Halliday: If I need to delete, so suppose I wanna delete this directory called good and everything inside of it, then we could do rm-r good, and then,
>> James Halliday: Everything is gone. Be very careful with -r and -rf, because if you accidentally do something like recursively delete root directory or your home directory or something with a lot important files that you didn't wanna get rid of, there is no undo.

>> James Halliday: A lot of these tools, this also goes for things like the move command and the copy command. If you actually overwrite some file, if you move over some file, not really much you can do aside from some kind of data forensic's tools to get back your files.

So just be careful.
>> James Halliday: These tools are very, another to word to describe them, but they're unforgiving. They can be very unforgiving at times.
>> James Halliday: Okay, so that's the rm command, not a lot to it. There's also the wc command. So the wc command is a good example of a command that can both read from files and read from standard input.

So if I ran wc on a file with lots of lines,
>> James Halliday: So wc is short for word count. And then there's three columns. I never can remember which column is which, but one is the number of characters. I think that's the last one. Another one is the number of words.

And another one is the number of lines. And there are different switches that you can pass, or flags, to do that. So- l is short for the number of lines. So I can count the number of lines in this big text file. Also if you wanna copy of this particular text file, there's a link to this one, it's just Moby Dick off Project Gutenberg in the notes.

>> James Halliday: So you can do wc -l, counts the number of lines, wc -c is the number of bytes in the file. So it's about 1.2 megabytes.
>> James Halliday: Also, if you want the word count, like maybe you have to write an essay, and you're back in high school and it has to be 800 words or so.

So I did, back in high school, when I was writing essays, I was writing them in vim, and I would use the wc -w commands to do the word count. [LAUGH] So here this one is,
>> James Halliday: About 215,000 words.
>> James Halliday: So the other thing is, if you just type wc, then it's going to sit there.

And what the wc command is waiting for, is it's waiting for you to type things in on the keyboard. So I can do hello world, hello minnesota, whatever, and I can keep typing things. And when you wanna tell the program that, all right, I'm done typing things, you can type Ctrl+D.

And this sends the end-of-file signal to this program, so Ctrl+D. And now, I get the awesome numbers back. I get the number of lines, which is 3, get the number of words which is 5, does that make sense? One, two, three, four, five, yep. And I get the number of characters, or bytes, which is 37.

All right, if you just want one of those things, if you read from standard in, you can pass in -l or -w or -c. So,
>> James Halliday: If I just want to count the number of lines, Ctrl+D and I get 3 lines.

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