Video summary
The video provides a comprehensive guide on using the `mv` command in Linux, covering renaming files, moving them between directories, and safely handling existing files. The simplest application of this command is renaming a single file by specifying its original name as the source and the desired new name as the destination. For instance, the tutorial demonstrates changing `credit.txt` to `creditcard.txt`, which effectively moves the file within the same directory under a new name. This process can be verified by listing the directory contents or viewing the file's data with the `cat` command to ensure the information remains intact after the rename operation.
A critical safety warning is emphasized regarding what happens when the destination file already exists; in such cases, the existing file is permanently deleted and replaced by the renamed source file without any prior notice. To illustrate this danger, the presenter accidentally attempts to rename a small "new project" file over a large "project" file containing important work, resulting in the irreversible loss of the larger file's contents. To prevent such accidental data destruction, the video introduces the interactive option, often accessed via the `-i` flag, which prompts the user for confirmation before overwriting an existing file. This safety mechanism allows users to explicitly choose whether to proceed with the overwrite or cancel the operation by pressing 'N' or 'Y'.
Beyond renaming, the `mv` command is also used to transfer one or more files into a specific directory, provided that the target directory exists beforehand. If the destination directory does not exist, the command will fail with an error message rather than creating the folder automatically. The tutorial shows how to resolve this by first creating the necessary directory using the `mkdir` command before attempting the move. Additionally, the video explains how moving directories behaves differently depending on the existence of the target location: if the target does not exist, the source directory is simply renamed to match the destination name, whereas if the target exists, the entire source directory and its contents are moved inside it.
In conclusion, mastering the `mv` command involves understanding both its basic syntax for renaming and moving files, as well as the specific behaviors when dealing with directories and existing file paths. The presenter wraps up by demonstrating how to use tab completion to speed up typing and how to recall previous commands using the up arrow key for efficiency. By combining these techniques with the interactive safety flag, users can effectively organize their file systems while minimizing the risk of accidental data loss, making `mv` an essential tool for efficient Linux administration.
Read the full video transcript
In this video about the MV command,
you'll learn how to rename a file,
how to avoid destroying files,
how to move one or more files to a
directory,
and how to move one directory to
another.
Here's the simplest use of MV to rename
a single file.
We give the MV command,
the name of our source, the original
file,
and the destination is the new name that
we want for that file.
Here's our current directory.
Let's see what is in some of these text
files.
In cash.text, we have a cash balance.
In credit.txt,
we have a credit card balance.
And in debit.txt,
we have a debit card amount.
Now, for an accountant, credit and debit
could be used in many different
contexts.
So, let's be more specific about a
couple of these names.
Let's change the name of credit.txt
to a new destination, which is going to
be creditcard.txt.
And you'll see that the name has changed
here.
If we do an ls -l,
we'll also see that it's called
creditcard.txt.
Essentially, what this command has done,
it has moved the file to a new name in
the same directory.
And we can confirm this by showing the
renamed file's contents.
cat creditcard.txt
And there's our information.
Similarly, we can rename
debit.txt
to debitcard.txt
and keep an eye over here at the left
and when I press enter,
you'll see that the name has been
changed.
At this point, I have to give you an
important warning.
If the destination file already exists,
it will be removed and it will be
replaced by the renamed file.
Let's see this in action.
First, let me clear out my terminal.
And here's a project file that has lots
of content.
Presume it's a file that we've been
working on a lot and it's worth 40% of
our grade.
And here's a new project that I've just
started.
And there's not much in it and it's only
worth 5% of the grade.
And in a moment of not thinking very
clearly, I decide that I'm going to move
or rename my new project.txt
to project.txt.
Now, at this point, the
project has 342 bytes, the new project
has only 111.
Watch what happens when I rename new
project to project.
Now, we have project.text, but it's the
shorter version because remember, when
the destination file already exist, it
gets removed. It's gone forever.
And in fact, if I do look at project.txt
now,
oh no, there goes the 40% of my grade.
So, the question is, how do I prevent
this problem?
The answer is by using the {dash} {dash}
interactive option or a short form
{dash}i.
Now, when I try and change a file's
name, the shell will warn me before I
wipe out an existing file.
To show you this in action, let me set
things back the way they were.
Now, you might be thinking, "What? I
thought you just said that file was gone
forever."
Well, yes, it is gone forever unless you
happen to have a backup somewhere,
which I do have.
So, let me do some magic off screen.
And now we're back to our original
setup,
where my big project still exists
and my new smaller project
also still exists.
Now, let's do the interactive move.
I'm going to say move
with the short option,
and I'm going to change the name of
newproject.txt
to project.txt.
And it's going to ask me, "Are you sure
you want to write over project.text?"
And this time, having learned my lesson,
I'll say, "Absolutely no." I press N for
no or Y for yes if I really did want to
do the same thing again.
And now when I cat project.txt,
you'll see that it's still there and the
rename never happened because I said no.
Another task is to move one or more
files to a directory.
I specify one or more files
and then the directory that I would like
them to go to.
When you move files to a directory, the
directory must already exist.
For example, if I were to try and take
these three files, cash, credit card,
and debit card, and move them into a
directory called money,
I'd have a problem.
Let's mv cash.txt
And by the way, to save typing, I'm
pressing the tab key to use automatic
file completion.
And debitcard.txt
into money.
It says, "I'm sorry, there's no such
file or directory to move it into."
Let's create the money directory.
And you'll see it shows up here.
And also by the way, if I were to do an
ls -l, you would see that money now
exists.
And now the move will do what we want.
Rather than retype the command, I can
use the up arrow to go back through the
commands I've already entered,
bring back the command I want, and press
enter.
Now they've disappeared from my main
folder, my main directory, and they have
ended up inside the money folder.
And in fact, if I ls
money,
there they are.
Finally, let's see how to move a
directory to another directory.
Moving directories works differently
depending on whether our destination,
our target directory exists or not.
If the target directory does not exist,
then the effect is to rename the source
directory to be the destination name.
If the target directory does exist, then
the source directory will be moved
inside of the target directory.
Again, let's see this in action.
Here is the unusual mammals directory.
I'll expand it and I'll also list it
here.
ls -l
unusual mammals
And there we have three files.
Okay, these aren't really that unusual.
And I'd like a simpler name for my
directory. So, I will move unusual
mammals
to just the name mammals.
Now, notice this target directory does
not exist yet.
And so, I get a rename as the effect.
If I look at tree.
You'll see that now mammals contains
those three animal names.
I then decide that I might want other
directories for reptiles, birds,
insects, etc.
Well, those are all animals. So, I'm
going to create a directory named
animals.
Now, if I move the mammals directory
to the animals directory,
it's not going to rename it. Instead,
it's going to move the entire folder in
to the animals directory.
Which we can see here.
And we can also see in our terminal
window by saying tree
{dot} The dot standing for our current
directory.
And those are the basics of using MV to
rename and move files and directories.