欢迎来到Felix的博客

Do the right things! And talk is cheap,show me your code!

linux系列(七):mv命令

1、命令格式:

  mv [选项] 源文件或目录 目标文件或目录

2、命令功能:

  Linux mv命令用来为文件或目录改名、或将文件或目录移入其它位置。

3、命令参数:

-b :若需覆盖文件,则覆盖前先行备份。 
-f :force 强制的意思,如果目标文件已经存在,不会询问而直接覆盖;
-i :若目标文件 (destination) 已经存在时,就会询问是否覆盖!
-u :若目标文件已经存在,且 source 比较新,才会更新(update)
-t  : --target-directory=DIRECTORY move all SOURCE arguments into DIRECTORY,即指定mv的目标目录,该选项适用于移动多个源文件到一个目录的情况,此时目标目录在前,源文件在后。

4、简单实例:

(1)、文件改名

命令:

  mv test.txt ttt.txt

输出:

felix@felix-computer:~/test$ ls
test.txt
felix@felix-computer:~/test$ mv test.txt ttt.txt
felix@felix-computer:~/test$ ls
ttt.txt
felix@felix-computer:~/test$ 

(2)、移动文件

命令:

  mv ttt.txt test3

输出:

felix@felix-computer:~/test$ tree
.
├── test3
└── ttt.txt

1 directory, 1 file
felix@felix-computer:~/test$ mv ttt.txt test3/
felix@felix-computer:~/test$ tree
.
└── test3
    └── ttt.txt

1 directory, 1 file
felix@felix-computer:~/test$ 

(3)、移动多个文件到指定目录

命令:

  mv -t test4/ test3/*

输出:

felix@felix-computer:~/test$ tree
.
├── test3
│   ├── 1.txt
│   ├── 2.txt
│   ├── 3.txt
│   ├── 4.txt
│   └── ttt.txt
└── test4

2 directories, 5 files
felix@felix-computer:~/test$ mv -t test4/ test3/*
felix@felix-computer:~/test$ tree
.
├── test3
└── test4
    ├── 1.txt
    ├── 2.txt
    ├── 3.txt
    ├── 4.txt
    └── ttt.txt

2 directories, 5 files
felix@felix-computer:~/test$ 

(4)、将文件1命名为文件2,如果文件2已存在,询问是否覆盖

命令:

  mv -i 3.txt 1.txt

输出:

felix@felix-computer:~/test/test4$ ls
1.txt  2.txt  3.txt  4.txt  ttt.txt
felix@felix-computer:~/test/test4$ mv -i 3.txt 1.txt 
mv:是否覆盖'1.txt'? y
felix@felix-computer:~/test/test4$ ls
1.txt  2.txt  4.txt  ttt.txt
felix@felix-computer:~/test/test4$ 

(5)、将文件1命名为文件2,如果文件2已存在,直接覆盖

命令:

   mv -f 2.txt 1.txt

输出:

felix@felix-computer:~/test/test4$ mv -f 2.txt 1.txt 
felix@felix-computer:~/test/test4$ ls
1.txt  4.txt  ttt.txt
felix@felix-computer:~/test/test4$ 

(6)、目录移动,如果目录dir2不存在,将目录dir1改名为dir2;否则,将dir1移动到dir2中

命令:

  mv test4 test3

输出:

felix@felix-computer:~/test$ tree
.
├── test3
└── test4
    ├── 1.txt
    ├── 4.txt
    └── ttt.txt

2 directories, 3 files
felix@felix-computer:~/test$ mv test4 test3
felix@felix-computer:~/test$ tree
.
└── test3
    └── test4
        ├── 1.txt
        ├── 4.txt
        └── ttt.txt

2 directories, 3 files
felix@felix-computer:~/test$

(7)、文件被覆盖前做简单备份

命令:

  mv 2.txt -b 1.txt

输出:

felix@felix-computer:~/test/test3/test4$ ls
1.txt  2.txt  3.txt  4.txt  5.txt  6.txt
felix@felix-computer:~/test/test3/test4$ mv 2.txt -b 1.txt 
felix@felix-computer:~/test/test3/test4$ ls
1.txt  1.txt~  3.txt  4.txt  5.txt  6.txt
felix@felix-computer:~/test/test3/test4$ 

 

posted @ 2018-11-06 19:45  寂静的天空  阅读(10103)  评论(0编辑  收藏  举报
个人感悟: 一个人最好的镜子就是自己,你眼中的你和别人眼中的你,不是一回事。有人夸你,别信;有人骂你,别听。一根稻草,扔街上就是垃圾;捆上白菜就是白菜价;捆上大闸蟹就是大闸蟹的价。 一个人,不狂是没有出息的,但一直狂,肯定是没有出息的。雨打残花风卷流云,剑影刀光闪过后,你满脸冷酷的站在珠峰顶端,傲视苍生无比英武,此时我问你:你怎么下去? 改变自己就是改变自己的心态,该沉的时候沉下去,该浮的时候浮上来;不争名夺利,不投机取巧,不尔虞我诈;少说、多听、多行动。人每所谓穷通寿夭为命所系,岂不知造物之报施,全视人之自取。 座佑铭:每一个不曾起舞的日子,都是对生命的辜负。