- ps命令
- watch命令
- at命令
- crontab命令(Linux定时任务)
- 文件目录操作命令
- 文件打包上传和下载
- 文件查找命令
- linux文件权限设置
- 磁盘存储相关
- 性能监控和优化命令
- 网络命令
- 其他命令
mv命令
mv命令是move
的缩写,可以用来移动文件或者将文件改名(move (rename) files),是Linux系统下常用的命令,经常用来备份文件或者目录。
1.命令格式
mv [选项] 源文件或目录 目标文件或目录
2.命令功能
mv
命令中第二个参数类型的不同(是目标文件还是目标目录),mv
命令将文件重命名或将其移至一个新的目录中。当第二个参数类型是文件时,mv
命令完成文件重命名,此时,源文件只能有一个(也可以是源目录名),它将所给的源文件或目录重命名为给定的目标文件名。当第二个参数是已存在的目录名称时,源文件或目录参数可以有多个,mv
命令将各参数指定的源文件均移至目标目录中。在跨文件系统移动文件时,mv
先拷贝,再将原有文件删除,而链至该文件的链接也将丢失。
3.命令参数
-b
:若需覆盖文件,则覆盖前先行备份。-f
:force 强制的意思,如果目标文件已经存在,不会询问而直接覆盖;-i
:若目标文件 (destination) 已经存在时,就会询问是否覆盖!-u
:若目标文件已经存在,且 source 比较新,才会更新(update)-t
: —target-directory=DIRECTORY move all SOURCE arguments into DIRECTORY,即指定mv的目标目录,该选项适用于移动多个源文件到一个目录的情况,此时目标目录在前,源文件在后。
4.命令实例
实例一
文件改名
命令:
mv test.log new-test.log
输出:
zyiz@ubuntu:~/mkdir/test$ ll total 12 drwxrwxr-x 2 zyiz zyiz 4096 Feb 12 17:56 ./ drwxrwxr-x 7 zyiz zyiz 4096 Feb 12 17:28 ../ -rw-rw-r-- 1 zyiz zyiz 13 Feb 12 17:56 test.log zyiz@ubuntu:~/mkdir/test$ zyiz@ubuntu:~/mkdir/test$ mv test.log new-text.log zyiz@ubuntu:~/mkdir/test$ ll total 12 drwxrwxr-x 2 zyiz zyiz 4096 Feb 12 17:57 ./ drwxrwxr-x 7 zyiz zyiz 4096 Feb 12 17:28 ../ -rw-rw-r-- 1 zyiz zyiz 13 Feb 12 17:56 new-text.log zyiz@ubuntu:~/mkdir/test$
说明: 将文件
text.log
重命名为new-text.log
实例二
移动文件
命令:
mv test1.txt test3
输出:
zyiz@ubuntu:~/mkdir/test$ ll total 16 drwxrwxr-x 2 zyiz zyiz 4096 Feb 12 17:59 ./ drwxrwxr-x 7 zyiz zyiz 4096 Feb 12 17:28 ../ -rw-rw-r-- 1 zyiz zyiz 13 Feb 12 17:56 new-text.log -rw-rw-r-- 1 zyiz zyiz 17 Feb 12 18:00 test.txt zyiz@ubuntu:~/mkdir/test$ mv test.txt /home/zyiz/mv-text.txt zyiz@ubuntu:~/mkdir/test$ ls /home/zyiz/ Desktop Downloads mkdir mv-text.txt Public Videos Documents examples.desktop Music Pictures Templates zyiz@ubuntu:~/mkdir/test$
说明:将
test.txt
文件移到目录/home/zyiz/
中,并重命名为:mv-text.txt
。
实例三
将test
目录下的文件log1.txt
,log2.txt
,log3.tx
t移动到目录test3
中。
命令:
mv log1.txt log2.txt log3.txt /home/zyiz/test3 ## or mv -t /home/zyiz/test3/ log1.txt log2.txt log3.txt
输出:
[zyiz@localhost test]$ pwd /home/zyiz/test [zyiz@localhost test]$ mv log1.txt log2.txt log3.txt /home/zyiz/test3 [zyiz@localhost test]$ ll total 0 [zyiz@localhost test]$ ll /home/zyiz/test3 total 0 -rw-rw-r--. 1 zyiz zyiz 0 Feb 12 22:33 log1.txt -rw-rw-r--. 1 zyiz zyiz 0 Feb 12 22:33 log2.txt -rw-rw-r--. 1 zyiz zyiz 0 Feb 12 22:33 log3.txt
说明:mv log1.txt log2.txt log3.txt test3
这个命令将log1.txt
,log2.txt
, log3.txt
三个文件移到 test3
目录中去,mv -t /opt/soft/test/test4/ log1.txt log2.txt log3.txt
命令又将三个文件移动到test4
目录中去。
实例四
将文件file1
改名为file2
,如果file2
已经存在,则询问是否覆盖
命令:
mv -i log1.txt log2.txt
输出:
[zyiz@localhost test]$ ll total 8 -rw-rw-r--. 1 zyiz zyiz 20 Feb 12 22:39 log1.txt -rw-rw-r--. 1 zyiz zyiz 30 Feb 12 22:39 log2.txt -rw-rw-r--. 1 zyiz zyiz 0 Feb 12 22:38 log3.txt [zyiz@localhost test]$ cat log1.txt just a test content [zyiz@localhost test]$ cat log2.txt this is file:log2.txt content [zyiz@localhost test]$ mv -i log1.txt log2.txt mv: overwrite ‘log2.txt’? y [zyiz@localhost test]$ cat log2.txt just a test content [zyiz@localhost test]$
实例五
将文件file1
改名为file2
,即使file2
存在,也是直接覆盖掉。
命令:
mv -f log3.txt log2.txt
输出:
[zyiz@localhost test]$ ll total 4 -rw-rw-r--. 1 zyiz zyiz 20 Feb 12 22:39 log2.txt -rw-rw-r--. 1 zyiz zyiz 0 Feb 12 22:38 log3.txt [zyiz@localhost test]$ cat log2.txt just a test content [zyiz@localhost test]$ cat log3.txt [zyiz@localhost test]$ echo 'this is log3.txt content' >> log3.txt [zyiz@localhost test]$ [zyiz@localhost test]$ ll total 8 -rw-rw-r--. 1 zyiz zyiz 20 Feb 12 22:39 log2.txt -rw-rw-r--. 1 zyiz zyiz 25 Feb 12 22:43 log3.txt [zyiz@localhost test]$ cat log2.txt just a test content [zyiz@localhost test]$ cat log3.txt this is log3.txt content [zyiz@localhost test]$ mv -f log3.txt log2.txt [zyiz@localhost test]$ cat log2.txt this is log3.txt content [zyiz@localhost test]$ ll total 4 -rw-rw-r--. 1 zyiz zyiz 25 Feb 12 22:43 log2.txt [zyiz@localhost test]$
说明:
log3.txt
的内容直接覆盖了log2.txt
内容,-f
这是个危险的选项,使用的时候一定要保持头脑清晰,一般情况下最好不用加上它,因为可能没有任何提示直接就执行了。
实例六:目录的移动
命令:
mv dir1 dir2
输出:
[zyiz@localhost test4]$ ls test41 [zyiz@localhost test4]$ cd .. [zyiz@localhost ~]$ ll total 0 drwxrwxr-x. 2 zyiz zyiz 38 Feb 12 22:44 test drwxrwxr-x. 2 zyiz zyiz 6 Feb 12 22:33 test1 drwxrwxr-x. 2 zyiz zyiz 6 Feb 12 22:33 test2 drwxrwxr-x. 2 zyiz zyiz 54 Feb 12 22:35 test3 drwxrwxr-x. 3 zyiz zyiz 20 Feb 12 22:46 test4 [zyiz@localhost ~]$ [zyiz@localhost ~]$ [zyiz@localhost ~]$ [zyiz@localhost ~]$ ll total 0 drwxrwxr-x. 2 zyiz zyiz 38 Feb 12 22:44 test drwxrwxr-x. 2 zyiz zyiz 6 Feb 12 22:33 test1 drwxrwxr-x. 2 zyiz zyiz 6 Feb 12 22:33 test2 drwxrwxr-x. 2 zyiz zyiz 54 Feb 12 22:35 test3 drwxrwxr-x. 3 zyiz zyiz 20 Feb 12 22:46 test4 [zyiz@localhost ~]$ cd test3 [zyiz@localhost test3]$ ll total 0 -rw-rw-r--. 1 zyiz zyiz 0 Feb 12 22:33 log1.txt -rw-rw-r--. 1 zyiz zyiz 0 Feb 12 22:33 log2.txt -rw-rw-r--. 1 zyiz zyiz 0 Feb 12 22:33 log3.txt [zyiz@localhost test3]$ cd .. [zyiz@localhost ~]$ cd test4/ [zyiz@localhost test4]$ ls test41 [zyiz@localhost test4]$ cd .. [zyiz@localhost ~]$ mv test4 test3 [zyiz@localhost ~]$ ll total 0 drwxrwxr-x. 2 zyiz zyiz 38 Feb 12 22:44 test drwxrwxr-x. 2 zyiz zyiz 6 Feb 12 22:33 test1 drwxrwxr-x. 2 zyiz zyiz 6 Feb 12 22:33 test2 drwxrwxr-x. 3 zyiz zyiz 67 Feb 12 22:48 test3 [zyiz@localhost ~]$ ll test3/ total 0 -rw-rw-r--. 1 zyiz zyiz 0 Feb 12 22:33 log1.txt -rw-rw-r--. 1 zyiz zyiz 0 Feb 12 22:33 log2.txt -rw-rw-r--. 1 zyiz zyiz 0 Feb 12 22:33 log3.txt drwxrwxr-x. 3 zyiz zyiz 20 Feb 12 22:46 test4 [zyiz@localhost ~]$
说明:如果目录
dir2
不存在,将目录dir1
改名为dir2
;否则,将dir1
移动到dir2
中。
实例7
移动当前文件夹下的所有文件到上一级目录。
命令:
mv * ../
输出:
[zyiz@localhost ~]$ cd test [zyiz@localhost test]$ ls log2.txt log3.txt [zyiz@localhost test]$ [zyiz@localhost test]$ [zyiz@localhost test]$ ll total 4 -rw-rw-r--. 1 zyiz zyiz 25 Feb 12 22:43 log2.txt -rw-rw-r--. 1 zyiz zyiz 0 Feb 12 22:44 log3.txt [zyiz@localhost test]$ cd .. [zyiz@localhost ~]$ [zyiz@localhost ~]$ [zyiz@localhost ~]$ pwd /home/zyiz [zyiz@localhost ~]$ cd test [zyiz@localhost test]$ ll total 4 -rw-rw-r--. 1 zyiz zyiz 25 Feb 12 22:43 log2.txt -rw-rw-r--. 1 zyiz zyiz 0 Feb 12 22:44 log3.txt [zyiz@localhost test]$ mv * ../ [zyiz@localhost test]$ cd .. [zyiz@localhost ~]$ ll total 4 -rw-rw-r--. 1 zyiz zyiz 25 Feb 12 22:43 log2.txt -rw-rw-r--. 1 zyiz zyiz 0 Feb 12 22:44 log3.txt drwxrwxr-x. 2 zyiz zyiz 6 Feb 12 22:55 test drwxrwxr-x. 2 zyiz zyiz 6 Feb 12 22:33 test1 drwxrwxr-x. 2 zyiz zyiz 6 Feb 12 22:33 test2 drwxrwxr-x. 3 zyiz zyiz 67 Feb 12 22:48 test3
实例八
把当前目录的一个子目录里的文件移动到另一个子目录中。
命令:
mv test3/*.txt test5
输出:
[zyiz@localhost test3]$ ll total 0 -rw-rw-r--. 1 zyiz zyiz 0 Feb 12 22:33 log1.txt -rw-rw-r--. 1 zyiz zyiz 0 Feb 12 22:33 log2.txt -rw-rw-r--. 1 zyiz zyiz 0 Feb 12 22:33 log3.txt drwxrwxr-x. 3 zyiz zyiz 20 Feb 12 22:46 test4 [zyiz@localhost test3]$ [zyiz@localhost test3]$ [zyiz@localhost test3]$ ll total 0 -rw-rw-r--. 1 zyiz zyiz 0 Feb 12 22:33 log1.txt -rw-rw-r--. 1 zyiz zyiz 0 Feb 12 22:33 log2.txt -rw-rw-r--. 1 zyiz zyiz 0 Feb 12 22:33 log3.txt drwxrwxr-x. 3 zyiz zyiz 20 Feb 12 22:46 test4 [zyiz@localhost test3]$ cd .. [zyiz@localhost ~]$ mv test3/*.txt test5/ [zyiz@localhost ~]$ ll test5/ total 0 -rw-rw-r--. 1 zyiz zyiz 0 Feb 12 22:33 log1.txt -rw-rw-r--. 1 zyiz zyiz 0 Feb 12 22:33 log2.txt -rw-rw-r--. 1 zyiz zyiz 0 Feb 12 22:33 log3.txt [zyiz@localhost ~]$ ll test3/ total 0 drwxrwxr-x. 3 zyiz zyiz 20 Feb 12 22:46 test4 [zyiz@localhost ~]$
实例九
文件被覆盖前做简单备份,前面加参数-b
命令:
mv log1.txt -b log2.txt
输出:
[zyiz@localhost test5]$ ll total 0 -rw-rw-r--. 1 zyiz zyiz 0 Feb 12 22:33 log1.txt -rw-rw-r--. 1 zyiz zyiz 0 Feb 12 22:33 log2.txt -rw-rw-r--. 1 zyiz zyiz 0 Feb 12 22:33 log3.txt [zyiz@localhost test5]$ mv log1.txt -b log2.txt [zyiz@localhost test5]$ ll total 0 -rw-rw-r--. 1 zyiz zyiz 0 Feb 12 22:33 log2.txt -rw-rw-r--. 1 zyiz zyiz 0 Feb 12 22:33 log2.txt~ -rw-rw-r--. 1 zyiz zyiz 0 Feb 12 22:33 log3.txt [zyiz@localhost test5]$
一些补充说明:
-b
不接受参数,mv会去读取环境变量VERSION_CONTROL来作为备份策略。- —backup该选项指定如果目标文件存在时的动作,共有四种备份策略:
- 1.
CONTROL=none
或off
: 不备份。 - 2.
CONTROL=numbered
或t:数字编号的备份 - 3.
CONTROL=existing
或nil:如果存在以数字编号的备份,则继续编号备份m+1…n:
执行mv操作前已存在以数字编号的文件log2.txt.~1~,那么再次执行将产生log2.txt~2~,以次类推。如果之前没有以数字编号的文件,则使用下面讲到的简单备份。 - 4.
CONTROL=simple
或never
:使用简单备份:在被覆盖前进行了简单备份,简单备份只能有一份,再次被覆盖时,简单备份也会被覆盖。
- 1.
扫描二维码
程序员编程王