Now look at how to create delete, copy and move files and directories in Linux.
Before we dive into this lesson, we need to understand a few basic things.
What is a file system tree?
What is an absolute path and what is a relative path
to list all files and directories? Even the ones beginning with a dot We use LS dash L A
and the dash A comes from the word all of
course to list files and directories from a different location.
We just type the directory path at the end of LS like
LS slash var slash log or LS slash L slash var slash log
to list files and directories in a different format called a long listing format,
which shows us more details for each entry
like the permissions for a file or directory,
what user group owns it
or when it was last modified,
it doesn't matter which order you put the flags and you don't
have to put a dash in front of each of them.
However, the last form is preferred as it's faster to write it.
There's also a command-line option,
dash H that shows sizes in human readable format.
Such as bytes, kilobytes, megabytes, and so on.
Now, this must be combined with a dash L option.
So if we want to use three options, we could use LS dash A LH
Linux organizes files and directories in what it calls a file system tree.
Now, why is it called the file system tree?
Because just like a tree we'd see in nature, this also has a root branches and leaves
except Linux
file system tree is inverted.
The root is at the top and its branches and leaves grow downward.
The root directory is a front slash.
This is the top level directory and there can be no other directories above it
under slash.
There are a few subdirectories like home, var, et cetera and so on.
Now, these subdirectories might also contain other subdirectories themselves.
Now, to access a file or directory on our command-line,
we must specify its file path or its directory path.
This path can be written in two different ways.
The absolute paths always start out with the
root directory represented by a front slash.
Then we specify the subdirectories we wanted to send into and in this case,
first home, then errand, then documents
and we can see the subdirectory names are separated by a front slash.
And then finally, we get to the file, we want to access invoice dot PDF.
Now to understand a relative path,
we first must explore what the current directory means.
And this also means the working directory.
Now, to see our working directory, we can type in PWD,
which stands for print working directory.
When we're working at the command-line, we're always inside a directory.
For example, if we log into the user, Aaron on some server,
our starting current directory.
For example, if we log in as the user, Aaron on some server,
our starting current directory might be slash home slash Aaron.
Every user starts in its home directory when they log in.
Now, Jane might have it at slash home slash Jane
and the root super user administrator has it at slash root.
Now, to change our current directory, we use the CD command
and CD stands for change directory.
So CD slash bar slash log would change our current directory to slash bar slash log.
We used an absolute path here,
but we can also change the directory this way.
Here's another absolute path CD slash home slash Aaron,
but we can also change directory this way CD dot dot
That will take us one directory up
if we were to CD in the slash home slash Aaron
running CD dot dot would take us into
slash home which becomes the new current directory
dot dot Always refers to the parent directory of our current directory.
And this is an example of using a very simple relative path. Let's dive deeper.
Let's imagine our current directory is slash home slash Aaron
with relative paths. We can refer to other places in one of three main ways
locations under our current directory.
For example, documents slash invoice dot PDF, since we're in slash home slash erin,
typing a path like documents slash invoice dot PDF is just like
typing in slash home slash erin slash documents slash invoice dot PDF.
Our relative path gets added to our current directory and we get to our PDF file.
Another way is locations in our current directory.
Typing invoice dot PDF will access the file at slash home slash
slash invoice dot PDF
locations above our current directory by typing in dot dot slash invoice.
PDF that points to the file at slash home slash invoice dot PDF.
Since we use dot dot slash, we basically said go one directory up
and we can use dot dot slash multiple times.
So dot dot slash dot dot slash invoice dot PDF
points to the file at invoice PDF at the root.
So
the first move the relative path at home and the next moved it all the way up to root.
Now, here are some extra tips.
If you're in slash bar slash log and you move to slash,
you could run the command CD front slash and it will take you to the root directory.
You can return to your previous working directory with the CD dash command.
It will take you back to slash var slash log.
Now, if you're in slash var slash
log and you want to return to your home directory in our case slash home slash Aaron
just use CD
CD without any options or paths will always take you back to the home directory.
Now,
let's assume we're in our home directory and we want to create a new file to do this.
We can use touch,
for example, to create a file named receipt dot PDF.
We would type in touch receipt dot PDF.
This will create it inside the current directory
to create it at another location.
We could use touch slash home slash Jin receipt PDF.
Since we're in slash home slash,
we could also use the relative path to create the file in
slash home slash chain by typing in touch dot dot jin.
Receipt dot PDF,
both commands would work the same because all the
commands will discuss except both absolute and relative paths.
So we won't mention these alternatives for each one.
Just know that after the command, you can use any kind of path. You want
to create a new directory, use MKDIR, for example, MKDIR, receipts
and make di
which is another way to say it comes from make directory.
Now to copy a file, we use the CP command which is short for copy
CP is followed by the path to the file. We want to copy source
and then the path to the destination where we want to copy it.
So it's CP
source destination
now to copy receipt dot PDF to the receipts directory. We'd use CP, receipt dot PDF.
Receipts,
notice how we terminated the path to the receipts
directory with a front slash to make it receipt slash
without the slash would have worked too.
But it's good practice to end your directories with a front slash.
This way you'll form a healthy habit and
get a visual indicator that tells you when receipts
without a front slash,
might be a file and receipts with a front slash might be a directory
to copy receipt PDF to the receipts directory, but also choose a new name for it.
We could use CP receipt dot PDF.
Then receipts front slash receipt,
copy dot PDF
to copy a directory and all its contents to another directory,
run the CP command as before.
But with the dash R option,
the dash R is a command-line option also called a command-line flag
that tells CP to copy recursively,
that means the directory itself,
but also descend into the directory and copy everything else.
It contains files and other subdirectories it may have and so on.
For example, say I have a lot of directories,
subdirectories and files under the receipts directory.
And I'd like to back up all the contents
into a backup directory named backup of receipts.
I would run the command CP dash R receipts,
backup of receipts.
This copies all the subdirectories and files from the
receipts folder into the backup of receipts folder.
Now the name you choose for your clone directory must not exist at your destination.
For example, if we already had a directory at slash home slash
slash backup of receipts,
this will just remove receipts there and it would end
up at document slash backup of receipts slash receipts.
So we saw that the copy operation copies a file from one place to another,
resulting in two copies of files,
the original one and the new one in the new location.
But what if we want to move a file from one location to another so that
the file is not present in the original
location but is only present in the new location
for this. We use the MB command
MB stands for move,
run the command MB. Receipt dot PDF.
Receipts front slash to move the file from receipt dot PDF to the receipts folder,
the file there is moved and only one copy of the file is available
now to rename a file. We can use MD receipt dot PDF, old receipt dot PDF
to rename a directory. We can use the new name as the destination
such as MB, receipts, old receipts.
Notice that we did not have to use the dash
R flag with MB to recursively work with directories.
MB. Takes care of that for us
to delete a file. We can use the RM command
and RM comes from remove
to delete the file invoice dot PDF. We can use RM invoice dot PDF.
Now to delete a file like the invoices directory, we would use RM
R invoices.
Once again,
the dash R option was used to do this recursively
deleting the directory along with the subdirectories and files.
When you copy or delete directories, remember to always use the dash R option.