With the mv command
① Change file (directory name)
② Move file (directory)
You can perform two types of processing.
$ mv <File before change(directory)Name> <File after change(directory)Name>
Example) When renaming the file test.txt
to the file name hoge.txt
$ mv test.txt hoge.txt
In addition, when the command is executed in the above example
If the hoge.txt file already exists, the original hoge.txt will be overwritten as soon as you rename test.txt.
One way to prevent this is to add the -i
option.
This will allow you to be asked "Are you sure you want to overwrite?" When executing the mv command.
$ mv -i test.txt hoge.txt
There are other file overwrite protection options, so I've listed them at the end of this article.
Also, since you can only change the file name one by one with the mv command, use the rename command
when you want to change multiple files at once.
$ mv <File(directory)Name> < 移動先のdirectoryName>
Example) When moving the file sample.txt
to the directory dir
$ mv sample.txt dir
By the way, it is safer to add /.
to the destination directory to make it dir / .
.
This /.
means "directly under the directory".
The reason is that if the dir directory doesn't exist when you run the command "Move the sample.txt file to the dir directory", then the sample.txt
file is simply named __ This is because it behaves as if it is changed to file __.
The syntax for renaming files
$ mv <file (directory) name before change> <file (directory) name after change>
Has been applied, hasn't it?
Therefore, it is safe to type the command as follows.
$ mv sample.txt dir/.
You can also move multiple files with the mv command.
Example) When moving sample01.txt
, sample02.txt
, sample03.txt
together to a directory called dir
$ mv sample01.txt sample02.txt sample03.txt dir/.
$ mv <File(directory)Name> ../
option | Description |
---|---|
-b (--backup) |
If there is a file with the same name in the destination, make a backup and then overwrite it. |
-f (-force) |
If there is a file with the same name in the destination, forcibly overwrite it |
-i (–interactive) |
If there is a file with the same name in the move destination, overwrite it and check if it is OK |
-n (–no-clobber) |
File with the same name in the destination(directory)Do not overwrite if there is |
-v (–verbose) |
Display detailed information when performing move processing |
Recommended Posts