This Lesson is for Members

Subscribe today and get access to all lessons! Plus direct HD download for offline use, enhances transcripts, member comment forums, and iTunes "podcast" RSS feed. Level up your skills now!

Unlock This Lesson

Already subscribed? Sign In

Autoplay

    Create and Delete Files and Folders in Bash

    Cameron NokesCameron Nokes

    One of the most useful things you can automate with bash is creating and deleting files and folders. We’ll learn how to create a file with touch and append content to it with echo. Then we’ll create a directory with mkdir. Using rm, we’ll then remove files and folders.

    bashBash
    Code

    Code

    Become a Member to view code

    You must be a Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson
    Transcript

    Transcript

    Instructor: 00:00 To create a file in Bash, we use the touch command and we pass the file name. I'll call mine file.txt. If we list this directory out, we can see it has the file.txt in there. At this point, the file.txt is going to be empty though, it initializes an empty file. Let's see how we would add content to it. For that, we'll use the echo command. Echo is sort of like the console.log of Bash. If I do echo "Hi," it logs out my string there.

    00:26 If we do echo "Hi" and we use the right angle bracket operator, that will direct my string into the file.txt. If we cat file.txt now, we see it has "Hi" for its file of contents. If we do that again, and let's change the string to "Hi" again and run that, we'll cat it again. We can see it has "Hi" again. If we do it multiple times it will overwrite the existing file contents in there.

    00:50 If we want to append to the file, let's append "Hello world" to it. We just used two of those brackets and that will append it to the very end of the file. Also while touch is a common way to initially create a file, you can actually just use plain echo. If we want to create a file and then initialize contents to it immediately, we can just do echo "Hello," and let's name a file here. We'll just do filetwo.txt. Then if we cat that out, we can see that both created our file and initialized it with the string that we passed to echo.

    01:24 To make a directory in Bash, we'll use the make dir command. We just pass the directory name that we want. I'll call mine folder. Let's list it out. We can see we have our two text files and our folder there now. Let's say I want to create several nested folders all at the same time, like I want to do "make dir a/b/c." We try that, it's not going to let us. Make dir by default wants a complete file path to the very end. It wants these A and B directories to exist before it will create a C directory in them.

    01:55 What we can do is if we pass the P flag, it will create each of those intermediary directories as needed. If I do a-b-c now, that will create all the folders we need. Here's the A folder. I list out A, B like that. Then there's our C folder. We use the RM command. Let's get rid of our file.txt. We list it out and we see it's gone.

    02:21 Note that the RM command permanently deletes a file. It doesn't move it to the trash or anything. If we want to remove a folder, we can't just use plain RM. RM by default only removes files. If we pass the R flag that will tell it to recursively remove the folder and everything in it.

    02:39 Oftentimes, you'll see RM used with the R flag and the F flag. The F flag is sort of a nuclear option. It prevents Bash from prompting you for confirmation when you remove a file, as well as erroring out if a file or directory doesn't exist. If we do that on A, that will remove that folder and all of its contents, as well.

    Discuss

    Discuss