In this video, I'll be showing you how to move and rename files and directories in Linux. We'll talk 1
about the mv command. 2
The mv command is a command line utility that moves one or more file directories from one place to another. 3
This command has two distinct functions. 4
It moves files and directories or it renames files and directories; yes, there is no mistake, you can 5
use mv both for moving and for renaming files. Before starting with an example 6
I will delete all the files and directories that I've created in the last lecture in the user's home 7
directory. 8
Now I'm creating a new directory tree structure for this lab: mkdir -p dir1 and dir2 inside. 9
And I'll create a file in each directory, stouch dir1/a.txt dir1/dir2/ 10
b.txt c.txt in the current directly. 11
Let's move a file into a directory. I'm moving b.txt, which is is in dir2, to dir1: 12
so mv the path to the file dir1/dir2/b.txt dir1/and the destination path, the destination 13
directory. 14
1 15
The file was moved. Let's see how to move multiple files into a directory; for this I'm going to specify 16
all the files I want to move as arguments and 17
the last argument will be the destination directory. 18
mv c.txt in the current working directory dir1/a.txt dir1/b.txt and the destination 19
will be dir1/dir2/ 20
So in fact, I'm moving all these three files into a directory called dir2, into this one. 21
Sorry, tree dir1 22
The files were moved. 23
You can also use a pattern matching like this mv dir1/dir2/ 24
*.txt dir1 25
I'm moving all the files which end in .txt from dir2 to dir1. 26
And if you want to move a directory, you move it as you would any other file. 27
I'm creating a new director mkdir dir1/dir2/dir3 28
And I'm moving dir3 into dir1 so mv, the source dir1/dir2/dir 3 and the destination 29
dir3 was moved. 30
Now it's in dir1. 31
Now, let's see what happens if the destination file already exists. 32
I'm creating a new file called a.txt in dir2: touch dir1/dir2/ 33
a.txt this is a new file. 34
And I'll move it to dir1, so mv dir1/dir2/ a.txt dir1 35
a.txt will be moved from dir2 to dir1 36
And the file was moved and the destination was overwritten without prompting; to prompt before overwriting 37
a file, the -a option can be used; -a cmes from interactive. 38
I'm creating a file called a.txt in dir2 again. 39
And I'm moving it. 40
And it's prompting. 41
I'm pressing control +c 42
I'm aborting the operation. 43
To prevent an existing file from being overwritten, use the -n option. 44
This causes the mv command to ignore anything that would override an existing file. 45
I'm creating a file called a.txt again. 46
And now I'm running mv -n dir1/dir2/a.txt dir1/ 47
It didn't move the file because the destination already existed and it would have overwritten the file. 48
Another useful option is -u or --update 49
When using this option, it will move only when the source file is newer than the destination file or 50
when the destination file is missing. 51
This guarantees that only newer files will be moved. 52
This is especially useful in Shell scripts. 53
In dir2 I'll create a new file called b.txt touch dir1/dir2 b.txt and mv-u 54
dir1 b.txt dir1/dir2 I want to move the file b.txt from dir1 55
to dir2 56
And there's already a file called b.txt in dir2 57
I've just created it. 58
What do you think? Has it moved the file? 59
Let's check it. 60
And b.txt was not moved. 61
It's still in dir1. That's because b.txt is not newer 62
then the destination. 63
As I said at the beginning of this lecture, mv can be used to both move and rename files. If 64
the source file is in the same directory as the destination file 65
then mv will rename it. 66
I'm renaming a.txt from dir1 to abc.txt, so mv dir1 a.txt and the destination 67
dir1 abc.txt. The file will be named! The source and the destination directory is the same. 68
It was renamed and you can rename a directory in the same way. 69
I have renamed dir1 to dir10. 70
And a last example, you can even move the file to a different directory and rename it at the same time. 71
I'm moving c.txt from dir10 to dir2 renaming it at the same time: mv the source dir10 c.txt 72
and the destination dir10/dir2 and the name of the file like say 73
cc.txt 74
The file was moved and renamed at the same time. That's all about mv command. 75
Thank you.