Linux下mv命令的用法
2016-01-12 20:21
615 查看
After knowing about copy command, the next command which is related is mv command. When you want to move files from one place to another and you don’t want to duplicate it, then mv command is absolutely right for this task.
What is mv command
mv command is a command that similar with cp, but it does not create a copy / duplicate of files or directories. This command is installed by default on your Linux system, in any kind of Linux you are using. Please take a look of some examples using mv command in day-to-day operation.
1.Moving files
The requirement of moving file is the file source location must be different with the files destination location. Here’s an example. To move file_1.txt from current directory to another directory , for example /home/pungki/office, here’s the syntax :
![](http://blog.linoxide.com/wp-content/uploads/2014/01/mv.png)
As we can see, when we move the file_1.txt, the file_1.txt from previous directory is deleted.
2.Moving multiple files
If we want to move multiple files, we put them in one line separated by space.
![](http://blog.linoxide.com/wp-content/uploads/2014/01/mv_multiple.png)
You can also use pattern if your files have it. For example, to move all files which have .txt extension, we can use this command :
![](http://blog.linoxide.com/wp-content/uploads/2014/01/mv_pattern.png)
3.Moving directory
Different from the copy command, moving directory using mv command is pretty straight forward. To move a directory, you can just to use mv command without any options. Please take a look screenshot below.
![](http://blog.linoxide.com/wp-content/uploads/2014/01/mv_directory.png)
4.Renaming files or directory
We also use mv command to rename files and directory. But in order to do so, the destination location must be the same with the source location. Then the file name must be different.
Let say we are inside /home/pungki/Documents folder and we want to rename file_1.txt into file_2.txt. Then the command will be like :
If we mention the absolute path, then it will look like this :
![](http://blog.linoxide.com/wp-content/uploads/2014/01/ren_file.png)
5.Renaming directory
The above rule is also applied to directory. Take a look this example :
![](http://blog.linoxide.com/wp-content/uploads/2014/01/ren_directory.png)
6.Print what happen
When you are moving or renaming a large number of file / directory, you may want to know does your command works successfully or not without seeing to the destination location. To do this, we can use -v option.
For example we want to move all txt files and want to check it. Then the command will be like this.
![](http://blog.linoxide.com/wp-content/uploads/2014/01/mv_v.png)
The same way is applied to directory.
![](http://blog.linoxide.com/wp-content/uploads/2014/01/mv_v_directory.png)
7.Using interactive mode
When you are moving file into another location, and there is already exist the same file, then by default mv will overwrite it. No pop-up notification for this. To make a notification for overwriting file, we can use -i option.
Let say we want to move file_1.txt to /home/pungki/office. Meanwhile, file_1.txt is already exist in /home/pungki/office directory.
![](http://blog.linoxide.com/wp-content/uploads/2014/01/mv_interactive.png)
This notification will aware us about the existence of file_1.txt in the destination location. If we press “y” then the file will be moved, otherwise, it will not.
8.Using update option
While -i are notify us about overwriting files, then -u option will perform update only if the source is newer than destination file. Let’s take a look example below.
![](http://blog.linoxide.com/wp-content/uploads/2014/01/mv_u.png)
We have file_1.txt and file_2.txt with this attributes :
File_1.txt has 84 bytes file size and it last modified time is 12:00
File_2.txt has 0 bytes file size and it last modified time is 11:59
We want to move them into /home/pungki/office directory. But in the destination location, we already have file_1.txt and file_2.txt.
We move file_1.txt and file_2.txt from current directory into /home/pungki/office using command :
As the result, we see those files are moved. Those file is moved because their last modified time stamp is newer than the files in /home/pungki/office directory.
9.Do not overwrite any existing file
If -i options is asking us about overwriting files, than -n option will not allow us to overwrite any existing files.
Using example on point 8, if we change the option from -u to -n, combine with -v option, then we will see that there are no files moved into /home/pungki/office directory.
![](http://blog.linoxide.com/wp-content/uploads/2014/01/mv_n.png)
10.Create backup when copying
By default, moving files will overwrite the destination files if there are already exist before. But what happen if you are moving wrong files, and the destination files are already overwritten by the new ones? Is there a way to retrieve the old one? Yes there is. We can use -b option. -b option will make a backup of destination file before it overwritten by the new one. Once again, we will use scenario from point 8 above.
![](http://blog.linoxide.com/wp-content/uploads/2014/01/mv_b.png)
As you can see on the screenshot, on the /home/pungki/office directory, we have a file named file_1.txt~ and file_2.txt~ . The tilde sign (~) means that those files are backup. We can see the attribute of them is older than file_1.txt and file_2.txt.
Conclusion
Moving file or directory also one of the basic command in Linux system. As usual you can type man mv or mv –help to display its manual page to explore more detail.
What is mv command
mv command is a command that similar with cp, but it does not create a copy / duplicate of files or directories. This command is installed by default on your Linux system, in any kind of Linux you are using. Please take a look of some examples using mv command in day-to-day operation.
1.Moving files
The requirement of moving file is the file source location must be different with the files destination location. Here’s an example. To move file_1.txt from current directory to another directory , for example /home/pungki/office, here’s the syntax :
$ mv file_1.txt /home/pungki/office
![](http://blog.linoxide.com/wp-content/uploads/2014/01/mv.png)
As we can see, when we move the file_1.txt, the file_1.txt from previous directory is deleted.
2.Moving multiple files
If we want to move multiple files, we put them in one line separated by space.
$ mv file_2.txt file_3.txt file_4.txt /home/pungki/office
![](http://blog.linoxide.com/wp-content/uploads/2014/01/mv_multiple.png)
You can also use pattern if your files have it. For example, to move all files which have .txt extension, we can use this command :
$ mv *.txt /home/pungki/office
![](http://blog.linoxide.com/wp-content/uploads/2014/01/mv_pattern.png)
3.Moving directory
Different from the copy command, moving directory using mv command is pretty straight forward. To move a directory, you can just to use mv command without any options. Please take a look screenshot below.
![](http://blog.linoxide.com/wp-content/uploads/2014/01/mv_directory.png)
4.Renaming files or directory
We also use mv command to rename files and directory. But in order to do so, the destination location must be the same with the source location. Then the file name must be different.
Let say we are inside /home/pungki/Documents folder and we want to rename file_1.txt into file_2.txt. Then the command will be like :
$ mv file_1.txt file_2.txt
If we mention the absolute path, then it will look like this :
$ mv /home/pungki/Documents/file_1.txt /home/pungki/Documents/file_2.txt
![](http://blog.linoxide.com/wp-content/uploads/2014/01/ren_file.png)
5.Renaming directory
The above rule is also applied to directory. Take a look this example :
$ mv directory_1/ directory_2/
![](http://blog.linoxide.com/wp-content/uploads/2014/01/ren_directory.png)
6.Print what happen
When you are moving or renaming a large number of file / directory, you may want to know does your command works successfully or not without seeing to the destination location. To do this, we can use -v option.
For example we want to move all txt files and want to check it. Then the command will be like this.
$ mv -v *.txt /home/pungki/office
![](http://blog.linoxide.com/wp-content/uploads/2014/01/mv_v.png)
The same way is applied to directory.
![](http://blog.linoxide.com/wp-content/uploads/2014/01/mv_v_directory.png)
7.Using interactive mode
When you are moving file into another location, and there is already exist the same file, then by default mv will overwrite it. No pop-up notification for this. To make a notification for overwriting file, we can use -i option.
Let say we want to move file_1.txt to /home/pungki/office. Meanwhile, file_1.txt is already exist in /home/pungki/office directory.
$ mv -i file_1.txt /home/pungki/office
![](http://blog.linoxide.com/wp-content/uploads/2014/01/mv_interactive.png)
This notification will aware us about the existence of file_1.txt in the destination location. If we press “y” then the file will be moved, otherwise, it will not.
8.Using update option
While -i are notify us about overwriting files, then -u option will perform update only if the source is newer than destination file. Let’s take a look example below.
![](http://blog.linoxide.com/wp-content/uploads/2014/01/mv_u.png)
We have file_1.txt and file_2.txt with this attributes :
File_1.txt has 84 bytes file size and it last modified time is 12:00
File_2.txt has 0 bytes file size and it last modified time is 11:59
We want to move them into /home/pungki/office directory. But in the destination location, we already have file_1.txt and file_2.txt.
We move file_1.txt and file_2.txt from current directory into /home/pungki/office using command :
$ mv -uv *.txt /home/pungki/office
As the result, we see those files are moved. Those file is moved because their last modified time stamp is newer than the files in /home/pungki/office directory.
9.Do not overwrite any existing file
If -i options is asking us about overwriting files, than -n option will not allow us to overwrite any existing files.
Using example on point 8, if we change the option from -u to -n, combine with -v option, then we will see that there are no files moved into /home/pungki/office directory.
$ mv -vn *.txt /home/pungki/office
![](http://blog.linoxide.com/wp-content/uploads/2014/01/mv_n.png)
10.Create backup when copying
By default, moving files will overwrite the destination files if there are already exist before. But what happen if you are moving wrong files, and the destination files are already overwritten by the new ones? Is there a way to retrieve the old one? Yes there is. We can use -b option. -b option will make a backup of destination file before it overwritten by the new one. Once again, we will use scenario from point 8 above.
$ mv -bv *.txt /home/pungki/office
![](http://blog.linoxide.com/wp-content/uploads/2014/01/mv_b.png)
As you can see on the screenshot, on the /home/pungki/office directory, we have a file named file_1.txt~ and file_2.txt~ . The tilde sign (~) means that those files are backup. We can see the attribute of them is older than file_1.txt and file_2.txt.
Conclusion
Moving file or directory also one of the basic command in Linux system. As usual you can type man mv or mv –help to display its manual page to explore more detail.
相关文章推荐
- Linux——file命令与magic file
- linux进程高cpu问题分析
- linux使用flock文件锁解决crontab冲突问题
- linux添加用户和修改密码
- 如何更改linux文件的拥有者及用户组(chown和chgrp)
- linuxmint 执行 qmake 报错
- Linux系统下卸载JDK 重新安装新版本
- Linux搭建 nfs 服务器
- windows向linux传送文件
- linux opendir 和 readdir , closedir用法
- Linux 查看位数 getconf LONG_BIT
- win7 VMware player 安装 centos 7
- 搭建linux虚拟环境
- 【Linux驱动】linux内核模块简介
- archlinux简略安装使用syslinux引导
- 为Linux配置apt-get的更新源
- Linux系统基本操作
- Unix/linux信号意义
- 集中管理工具Puppet 实战部署及使用
- 驱动模块加载错误:insmod: can't insert 'xx.ko': invalid module format