in this lecture we'll take a look at
copying moving and renaming files so
right now I'm in my home directory and
for this exercise I've created a new
directory called examples so I'm going
to change into that directory and we're
going to start working from there so the
first thing I want to do is create a
file called original txt on it and
you'll notice that that file exists so
let's I want to make a copy of this file
the command to copy a file is CP you
give the original name of the file and
then the name that you would like to
copy it to and in this case it's going
to copy this file in the current
directory because notice I'm giving two
relative paths and both of these assume
that the original file is in this
directory and that the new file I would
like created would be in this directory
and there you can see that the copy file
was made and if I take it a look at the
long listing you can note that you know
they're the same except for their names
so that would be a copy of the file
let's take a look at how to rename a
file so in the current directory I want
to take the copy file and I want to
rename it to not a copy dot txt notice
that the command to do this is MV which
actually stands for move and the secret
here about renaming a file is that when
you move a file within the same
directory you can move it from an old
name to a new name which is the same
process as renaming a file so if I do
move from the original name copy txt and
give a new name what will happen is this
copy this file will be renamed within
the same directory so let's take a look
at the LS and you'll notice that that
file was renamed so we use the move
command to rename files within the same
directory we could also use the Move
command to move a file so let's move
that not a copy file out of this
directory and I am going to send it to
my
directory if you remember tilde is a
shortcut from my home directory so I
want to move the file called not a copy
txt to my home directory and if I look
it's gone it's currently not in my
examples directory but if I do an LS of
my home directory you'll notice that the
file was moved to there so notice that
you don't necessarily have to copy files
within the same directory so a really
good example would be let's clear this
and let's say I wanted to move that not
a copy TX whew I'm sorry let's say I
want to copy that not a copy dot txt
file back into my current working
directory so what I could say is CP will
use tilde tilde not a copy dot txt and I
want to copy it into my current
directory as a new copy dot txt and if I
really wanted to remember that a single
dot is the current directory if I want
to be explicit about it I can put dot
slash but if you leave that off it's the
same as just saying the name and the
assumption is that it'll put it in your
current working directory so now if I
look at my current working directory I
have my new copy txt file and if I look
at the directory above me notice that
the old not a copy is there basically
the process of moving copying and
renaming files is pretty straightforward
let's take a second to look at moving
and copying directories so right now I'm
currently my working directory is the
examples directory and from the previous
examples you'll note that I have two
files in that directory so I'm going to
do is go back up the directory into my
home directory so I'm currently in my
home directory and you can see in this
directory my examples directory so let's
first of all let's rename it when you
rename directories it's pretty
straightforward you just give the name
of the old directory and the notice I
use the auto-completion in that case I
hit tab to spell out examples and it put
a slash at the end of it I don't need
that
but the slash at the end of the name
examples is yet one more visual
reinforcement that this is a directory
so I'm going to change examples to just
call it renamed examples and if I look
at my directory now nope I didn't also
notice that examples is now called
renamed examples so it's pretty
straightforward to rename a directory
you can also it's pretty straightforward
to move a directory as well so if I
wanted to let's say move my templates
directory into my renamed examples
directory I give the name of the
directory I want to move and the
directory I want to move it to and as
long as I don't want to rename it in the
process I can just give the target
directory I could also give it a new
name in the process
so a quick LS shows me at the templates
directory is gone and a quick LS of the
renamed examples directory shows me that
the template is still there I'm actually
gonna move that back because I don't
really know if I need that for anything
but I think it's just probably for like
OpenOffice or something so I'm going to
take that
templates directory in the renamed
examples directory and move it back to
my current directory you'll notice that
now it's back right there so again if I
wanted to rename this in the process all
I need to do is use add a name if I omit
a new name for the file when I'm moving
it that means leave it the same old name
now let's try to copy it so let's I want
to copy the templates direct
my example starkly let's use that for
example so if I look at LS renamed
examples directory just reminder that
our two files in it so let's try to copy
it let's copy renamed examples to copy
are so I don't want the type that as
quickly notice I get an error and the
reason why is that the renamed examples
directory has files in it and so anytime
you try to copy a directory just like
when you try to delete a directory that
has files in it the OS is going to
require you to do some additional work
to say yes I really want to delete this
directory with files in it when it comes
to copying directories the same rules
apply again it's it's you know because
you're kind of maintaining the system
hierarchy in your brain it can sometimes
be easy to accidentally copy you know a
couple gigabytes of data so they want to
the system is designed to make sure you
really want to do it so in this case if
all I want to do is actually go ahead
and do that that change I just add - r4
recursive and what that'll do is say yes
copy this directory and everything
inside the directory to the new
directory and if I do an LS of both of
these
let's look do LS of both you'll notice
that now they're both exactly the same
and by the way notice if you put
multiple folders on the line with an LS
you'll get output for both folders so
it's a nice way to see if they are the
same so if you want to know more about
options for copy and move it's always
valuable to take a look at the man page
if you have any questions about how
these work