[MUSIC]
>> Working with directories and files is easy in
Bash after you learned that these six commands.
First and foremost is the MKDIR command,
which stands for make directory.
We can see that if we use the man command to
get some help on this particular command, MKDIR.
It says that it makes directories.
To use it, we can provide some options if we want,
but we really wouldn't wants is just the name of the directory.
For clear the screen and we create a new folder or
directory called Project MKDIR, project01.
Then we use the ls command.
We can see that we've created
a new directory and that's highlighted
by the green background over that
indicating that it's a directory instead of a file.
All the other items in this listing are files.
You can also create multiple directories at once.
You don't have to do it one line at a time.
You could do MKDIR
project02 space project03 in
that space denotes that it's going to
be a different directory that's created.
If you run ls again,
we now have three.
But what if you wanted to create
a sub-directory underneath the directory that didn't exist yet.
Well, if we look at the help for the MKDIR command,
there is an option that we can use to do that, which is parents.
We can see under here and the description
of the different options,
dash P or dash,
dash parents will make
the parent directories as
needed and that will get us around that air.
If we did MKDIR dash P and then maybe projects01,
and then we will do a subdirectory.
If we just did sub,
that would work without the P because that's the next layer.
But if we were to do sub2,
so there'll be a sub-directory underneath that
and one doesn't exist yet. We'll get an air.
Just to show you that,
let's remove the dash P to see the air.
It's going to say subdirectory doesn't exist yet.
It's talking about sub1 not existing.
But if we hit the up arrow
and go back a little bit and we add the dash P back in there.
It's going to also create
the parent directories for this particular path.
Now if we run ls on project01,
we should see that we have sub1 and if we
look inside sub1, we have sub2.
Now we have folders all the way down.
That's how you create directories with the MKDIR command.
I'll look at using the touch command to create files.
Touches an even simpler command.
All you do is type touch,
and then the name of the file that you want to create.
Let's create a file underneath project01,
and we'll just name it file.txt.
MKDIR was making directories and touches for making files.
Now all touch does is it creates a file
and if you use it on an existing file, it will update.
The time stands for the date modified.
If we ls project 01 now we should see
that it has the subdirectory folder and the file.txt.
Now we can use a different command.
Now that we have a file that's created to
move one item to another location.
That command is the MV command.
If we look at the help for MV,
it just is for moving
files and there's a number of different options.
We can force move and stuff like that.
But to use this will just put MV projects01.
We want to move the file txt into project 2.
We'll just put the next destination so it's
move source to destination so project02,
and we don't need to put the extension is going to
take file.txt and move there.
It's also important to note that we can use
wildcards with any of these commands.
I could put files Asterix
and it's going to move anything that matches file,
followed by anything else ended the projects too.
But we'll keep the full file name there for now.
I'm going to move that. Then to confirm that it worked,
we'll use the ls command underneath project02.
Now we have file txt.
Now what we could have done instead
of moving it as we could have copied it,
and that is another command that you can use, which is CP.
CP so if we look at the help for cp
it is copy files and directories so you can copy files,
which is what we're going to do in this case,
or the entire directory.
To copy file.txt back into project01 will use the cp command.
We're going to copy from
project02 and we're going to copy file.txt.
Again, this could be using wildcards so you can
copy the entire folder or anything that matches.
Then I'm going to copy it into project01.
I could leave this like that,
so it's going to copy them there or I can
define what is going to be copied into,
which would be file.txt to here.
Or I could rename it, so maybe it'll be file
01.txt. If I do that.
Then I ls project01,
you'll see that it copied whatever the contents of file.txt was,
which was nothing in this case because I didn't add
any text into the file is going to be
copied from projects01 back
into project02 but has a different name.
If I didn't add that file 01 here,
it will copy it just as file.txt.
I can see that if I just do another ls
on project01 now it copy the name as it was,
so that's how you can use CP2 there,
copy the existing file with
the current name or change the name during the copy process.
Lastly, now that we've moved a bunch of stuff around,
it would be time to clean up all this stuff.
The first command for doing some of
this cleanup is the RM command,
which stands for remove.
If you look at the directory structure
here we have three project boulders.
If we change into project01 will
see that we have a number of files and to use the RM command,
you just type RM followed by
the name of the file you want to delete.
Again, this also accepts wild cards,
so while I could put file 01.txt and file.txt,
and that would remove both.
I can also just put in Asterix in there and it will delete it.
Now this will delete all files that match file.
If it has five-fold by anything,
it's going to try and delete that now because
I haven't used any other switches or options with this,
it's only going to work in the current working directory.
I could make this recursive if I added
the dash R and we can see that in a help if we wanted to.
But for now, I know that there
are only two files in here that will match this.
I'm going to assume that it's
safe enough to use wildcards removed.
If a RM file asterix,
I'm going to remove all of
the text files in here that had file and the name.
Now we have a subdirectory here that is still not cleaned up,
which is sub1,
and then sub1 has sub2,
if you remember from just a couple minutes ago.
What we need to do here is remove
or use a different command which is RMDIR,
which stands for remove directory.
We do sub1.
Now sub1 isn't the lowest level of this hierarchy.
There's actually a sub2,
so I need to delete the lowest path if I'm going to
use the MKDIR command without any options.
If I remove that directory and do an ls, I still have sub1,
but now I can actually remove
the sub1 directory because sub2 no longer exists and
there's nothing underneath the hierarchy or path for
sub1 and so now I don't have any directories underneath here.
I mentioned before that we can also use wildcards with this.
If I go back into
my root directory of this workspace and I do an ls,
I still have project 1,
project 2, and project 3.
Now if we look inside project02, it does have a file.
If I were to use a wildcard here,
it wouldn't work because there is content inside a project 1.
I'll just use the remove command and get rid of project01,
or project02, file txt quickly.
Now I could use RMDIR and do project 1,
project 2, project 3.
Or I can use the wildcard now that each of
these directories don't have anything inside them.
I could do RMDIR, project star.
That will remove all of the directories with project.
If I do an ls, we see that I'm back to the original state.
I don't have any directories are files,
and those are the six commands that you
need to know to successfully
inefficiently manage and work with files and folders and bash.