Creating and Deleting Files and Folders in Linux

In this video we’re going to review how
to create and delete directories, create files, and also make working at the
command line a little bit easier. Let’s start by looking at what is in my
current directory so if I do a “pwd” I find out that I’m at my home directory and
I’ll do an “ls” to list what’s in my directory and what I think i’m going to
do is instead of creating some new folders in here I’m going to change into
my desktop directory which is what the GUI in Ubuntu really puts anything
I put on my desktop…drops into that folder. So it’s going to that folder. Now
notice I could type the word Desktop if I want but that’s a lot of typing.
So one of things you can do with bash is there’s this thing called command
completion so once you’ve typed enough of the folder to let the shell know what
you mean you can hit “tab” and notice that when I hit tab the word Desktop was
auto-completed for me. What would happen if i go back and just type capital D and
hit tab? Well if I’m persistent and hit tab a few times what’s going to happen is the shell is going to be like “hey, you know there are
several things in this directory named that begin with a capital D” but if i
give it one more letter – De – for Desktop and hit tab it autocompletes. So, it’s
really cool as you can save a lot of typing by learning to just start typing
the name of the file you want and then use tab to do auto-completion. Now if we
look I’m currently located in my Desktop directory. What’s here? Well, I have a little
shortcut folder here for my terminal so that it’s easier for me to launch but
let’s start to add some stuff to this directory and play around with it. The
first command we want to do is we want to make a directory so I’m going to make
a directory and the command to do that is “mkdir” and it pretty much translates
in your brain to make directory and i’m going to call it a directory called “top” and hit “enter” and then I’ll do “ls” and you’ll notice that it created a new directory
called top and now i’m going to cd into that directory. Type “t” and hit tab to autocomplete it
and notice there’s nothing in there. I’ll make another directory called
middle and you’ll see that that exists. Last time I created…when i created this
middle directory I did a cd into the top directory but you don’t need to do that,
right, I can be in the current directory and, again, I’m currently in the top
directory and I can see the middle directory so what I want to do is I want
to make another directory called bottom inside the middle directory. So I want to
say mkdir. I’m going to give the name of the middle directory, hit tab,
autocomplete that and then say bottom. So, again you don’t always need to be in the
directory in which you’re working. With that said I can say that it’s a pretty
good habit to get into to be in the directory that you’re working so that
you always know where you’re creating files or deleting files from. And now if
I look, notice there’s no bottom directory in my current directory but if
I do an ls of the middle directory, there’s the bottom directory. So, we just
essentially set up this pretty basic file hierarchy. I’m currently in the
top directory and I just installed a command into Ubuntu called “tree” and
so if I type the tree command and give it the current directory you’ll notice
that it shows me a file hierarchy and it says well I’m in whatever the name of my
current directory is and inside the middle is a directory called bottom. If i
go back up to Desktop just to verify that I’m in desktop and I type tree
notice that it shows me that in the current desktop directory there’s a file
called terminal, ignore what this means. There’s a directory called top and in
that directory is a directory called middle and in that directory is a
directory called bottom. So tree is a useful command. It is not installed by default
on Ubuntu 12 LTS so you know one of the things you might need to do is if you
try to use tree Ubuntu is pretty good about giving you some instructions on
how to install it. We will go over installing applications at a later date. So there you can kind of see the file
hierarchy that i have created. So now I want to go in and I am going to cd into
top. I’m going to cd into middle and now i’m going to be in the directory
called bottom. What I want to do is I want to delete that directory. So to delete a directory the command is “rmdir” which stands for remove directory
and then the name of the directory that I want to remove and you’ll notice that
I’m still in the middle directory but the file for the directory bottom is now
gone so rmdir will remove a directory. But, there’s a caveat! I’m going to clear the screen and again I’m in the middle directory. I’m going to go back up to the top directory. Alright, what’s in here is middle…well
let me go up one more to the desktop so now I’m in the Desktop and again there’s top and terminal. I’m going to do rmdir top and try and get rid of top and middle
together and what you’re going to notice, rmdir is a pretty smart utility in that
it will not let you delete a directory unless that directory is empty and it’s
really just a safeguard because what you don’t want to do is accidentally delete,
say, you know, your entire home directory or an entire website, or you know,
anything that…If rmdir didn’t stop you when you wanted to delete
something that wasn’t empty then that would be a problem. The way I would resolve this is I
would delete the middle directory and then I could delete top. If we take a
look at the man page for rmdir it says, “how can I delete non-empty directories” well, what you’ll notice is that rmdir will only remove
empty directories. We’ll talk about how to remove full directories too but once you
have that power it could be a little dangerous so right
now we can only delete empty directories. Ok, so next up clear the screen and I’m going to go back in to top and I’m going to put some files in here. We’ll talk about using
editors next week, specifically VI. We can touch a little bit on using emacs but
what I want to do is show you just how to create empty text files and at this
point creating empty text files it’s just a simple command that you
might think “well, why would I ever want to create an empty file?” but there’s a
bunch of reasons why you might want to do that and we’ll talk about that a
little bit later when we get into scripting. The command I’m gong to use is called touch and I’m going to create a file called test.txt. You don’t need file extensions
on Unix but it’s sometimes nice to have them. So, there’s test.txt. If I look at
some information about that file what we’ll notice is that this is a
regular file because there’s a dash here at the beginning. We looked at that when
we did the ls lecture. You’ll notice that this file has 0 bytes in it so it’s
definitely an empty file. The touch command is a useful command for creating empty files, it’s also a good command for updating modification dates on files so
you can touch a file so that it looks like it’s been accessed more recently
than it may have and there are reasons why you might want to do that. Again
we’re going to use touch here just to kind of make some files that we can
delete them. So, next up we want to go ahead and
delete that file. We had rmdir for removing a directory and I’m going to
use “rm” to remove a file. So, if I type rm test.txt and then do an ls the file is
gone. The RM command allows us to remove files. Let’s take a look at the man page for rm because it comes with a lot of nice features. The ones I’m going topoint out that I think are really useful. Start with command “i” which means
every time it goes to remove a file it will prompt you and say do you really
want to do this. Unix systems tend to default to being
very quiet so they’re not going to say anything they’re going to assume that you want to
do what you want to do so if you like that feedback that you are used to getting from a GUI the command “i” option can be helpful. A rather dangerous option but
one that’s important to know about is dash f (-f) which stands stands for force and what that will do is no matter what it will delete the file. So that can be a
little dangerous and we’ll talk about why in a second. There’s also one other command U
think is important to take a look at right now, dash “r” (-r) which stands for
recursive. Earlier I said you could not delete a directory unless it was
empty using rmdir so I’m going to create a file in touch and i’m going to call…with touch…in the middle directory called deleteme.txt and if I do ls
middle you know that there’s a file in there and, again, I’m in the top directory
so I see the middle directory so if I do rmdir on
middle you’ll notice that it says fail. The directory is not empty and that’s because I just put the deleteme.txt file in but if I do rm -r and I do
middle it deleted it. So, rm can be used to delete directories and -r for
recursive can be used to delete basically every folder inside of that
folder but it’s a little dangerous right? Because, you want to make sure that you
don’t lose data. That’s why it’s always a good idea to use rmdir. In this case maybe if we added rm -ri for interactive it would prompt me on
everything I wanted to delete but, you know, if you got a lot of files to delete…so again you just need to be really careful what you’re doing. One of the things that I will mention
that’s a bad thing. The worst thing…I think there used to be
joke in Windows everybody would tell people to fix a problem by doing “format
c:” which formats your hard drive. You know, it’s kind of hard to do this
but the command “rm -rf /” means to forcefully and recursively delete
everything underneath the root directory. So this is basically a way that you
would, you know, if you have the correct privileges (and we haven’t talked about
privileges yet) but if you’re running this command as the root user which has
ultimate privileges on the machine this would go ahead and pretty much
start wiping everything out on your machine so just be careful and if
anybody ever tell you do rm -rf / don’t listen to them and it’s just a good
thing to know. So, we looked at making directories, we looked at removing
directories, we looked at using rm to delete files, we looked at touch to
create files.On other thing I’ll show you real quick is that you can do a chain of command using semicolon. So let’s say I want to make a folder called middle, again, and I
want to then immediately put a file in there you can put a semicolon between
separate commands on the command line and it will work. Then I’ll say, make a new file in here called iamnew.txt. What you’ll notice is that
those two command execute immediately so if I do ls there’s middle and if I look at
what’s in the middle directory there is the file that I created, so you can do
file chaining simply by putting a semicolon between
commands and again we’ll see where this is useful a lot later. It’s really
helpful when compiling programs especially the kernel. You can just put
all the commands on one line and let it go. If you need to execute multiple
commands just separate them with a semicolon.

Daniel Ostrander

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *