[리눅스 학습] 리눅스 필수 명령어(1)--mv 명령어에 대한 자세한 설명

[리눅스 학습] 리눅스 필수 명령어(1) – mv 명령어에 대한 자세한 설명

1. 명령어에 대한 자세한 설명

mv 명령은 주로 파일이나 디렉터리의 이름을 바꾸거나 이동하는 데 사용됩니다. 사용법: mv old.txt new.txt 일반적으로 사용되는
매개 변수는 아래에 자세히 설명되어 있습니다.

用法:	mv [选项] [-T] 源文件 目标文件;
	或:mv [选项] 源文件... 目录;
	或:mv [选项] -t 目录 源文件;
将源文件重命名为目标文件,或将源文件移动至指定目录。长选项必须使用的参数对于短选项时也是必需使用的。
	--backup				为每个已存在的目标文件创建备份;
-b 							类似--backup 但不接受参数;
-f, --force 				覆盖前不询问;
-i, --interactive 			覆盖前询问;
-n, --no-clobber 			不覆盖已存在文件,如果您指定了-i、-f、-n 中的多个,仅最后一个生效;
	--strip-trailing-slashes 去掉每个源文件参数尾部的斜线;
-S, --suffix=SUFFIX 		替换常用的备份文件后缀;
-t, --target-directory=DIRECTORY 将所有参数指定的源文件或目录移动至指定目录;
-T, --no-target-directory 	将目标文件视作普通文件处理;
-u, --update 				只在源文件文件比目标文件新或目标文件不存在时才进行移动;
-v, --verbose 				详细显示进行的步骤;
	--help 					显示此帮助信息并退出;
	--version				显示版本信息并退出

2. 명령어 예시

파일 이동

파일을 디렉터리로 이동하거나 이름을 바꿉니다.

[root@nie linux]# ls
1.txt  2.txt  a  linux.txt  word
[root@nie linux]# ls a/
[root@nie linux]# mv 1.txt a
[root@nie linux]# ls a/
1.txt
[root@nie linux]# ls
2.txt  a  linux.txt  word
或
[root@nie linux]# ls
a  linux.txt  word
[root@nie linux]# mv linux.txt linux1.txt 
[root@nie linux]# ls
a  linux1.txt  word

확인 후 동일한 이름의 파일을 덮어씁니다.

[root@nie linux]# ls
2.txt  a  linux.txt  word
[root@nie linux]# ls word/
1.txt  2.txt  linux.txt
[root@nie linux]# mv -i 2.txt word/
mv:是否覆盖'word/2.txt'? y
[root@nie linux]# ls
a  linux.txt  word
[root@nie linux]# ls word/
1.txt  2.txt  linux.txt

동일한 이름의 디렉토리는 확인 없이 직접 덮어쓸 수 있습니다.

[root@nie linux]# ls word/
1.txt  2.txt  linux.txt
[root@nie linux]# ls 
a  linux.txt  word
[root@nie linux]# ls a/
1.txt
[root@nie linux]# mv -f a/1.txt word/
[root@nie linux]# ls a/
[root@nie linux]# ls word/
1.txt  2.txt  linux.txt

덮어쓰기 전에 같은 이름의 파일을 백업하세요.

[root@nie linux]# ll
总用量 4
drwxr-xr-x. 2 root root  6 6月   1 12:23 a
-rw-r--r--. 1 root root 18 5月  31 19:57 linux.txt
drwxr-xr-x. 2 root root 49 6月   1 12:23 word
[root@nie linux]# ll word/
总用量 8
-rw-r--r--. 1 root root 4 5月  31 19:56 1.txt
-rw-r--r--. 1 root root 6 5月  31 19:56 2.txt
-rw-r--r--. 1 root root 0 5月  31 19:40 linux.txt
[root@nie linux]# mv -b linux.txt word/
mv:是否覆盖'word/linux.txt'? y
[root@nie linux]# ll
总用量 0
drwxr-xr-x. 2 root root  6 6月   1 12:23 a
drwxr-xr-x. 2 root root 67 6月   1 12:27 word
[root@nie linux]# ll word/
总用量 12
-rw-r--r--. 1 root root  4 5月  31 19:56 1.txt
-rw-r--r--. 1 root root  6 5月  31 19:56 2.txt
-rw-r--r--. 1 root root 18 5月  31 19:57 linux.txt
-rw-r--r--. 1 root root  0 5月  31 19:40 linux.txt~

원본 파일이 대상 파일보다 최신이거나 대상 파일이 존재하지 않는 경우에만 이동합니다.

[root@nie linux]# ll
总用量 4
drwxr-xr-x. 2 root root 23 6月   1 12:58 a
-rw-r--r--. 1  777 root 22 6月   1 13:00 linux.txt
drwxr-xr-x. 2 root root 49 6月   1 12:55 word
[root@nie linux]# ll a/
总用量 0
-rw-r--r--. 1 root root 0 6月   1 12:58 linux.txt
[root@nie linux]# ll word/
总用量 12
-rw-r--r--. 1 root root  4 5月  31 19:56 1.txt
-rw-r--r--. 1 root root  6 5月  31 19:56 2.txt
-rw-r--r--. 1 root root 18 5月  31 19:57 linux.txt
[root@nie linux]# mv -u linux.txt a/
mv:是否覆盖'a/linux.txt'? y
[root@nie linux]# ll
总用量 0
drwxr-xr-x. 2 root root 23 6月   1 13:03 a
drwxr-xr-x. 2 root root 49 6月   1 12:55 word
[root@nie linux]# ll a/
总用量 4
-rw-r--r--. 1 777 root 22 6月   1 13:00 linux.txt
或
[root@nie linux]# mv -u word/linux.txt ./
[root@nie linux]# ll
总用量 4
drwxr-xr-x. 2 root root 23 6月   1 13:03 a
-rw-r--r--. 1 root root 18 5月  31 19:57 linux.txt
drwxr-xr-x. 2 root root 32 6月   1 13:04 word
[root@nie linux]# ll word/
总用量 8
-rw-r--r--. 1 root root 4 5月  31 19:56 1.txt
-rw-r--r--. 1 root root 6 5月  31 19:56 2.txt

디렉토리 이동

대상 디렉터리가 존재하지 않으면 디렉터리를 이동하고 이름을 바꿉니다.

[root@nie linux]# ls
a  linux1.txt  word
[root@nie linux]# mv a word1/
[root@nie linux]# ls
linux1.txt  word  word1

[root@nie linux]# mv word1/ word/
[root@nie linux]# ls
linux1.txt  word
[root@nie linux]# ls word/
1.txt  2.txt  word1

해당 디렉터리의 파일을 현재 디렉터리로 이동

[root@nie linux]# ls 
linux1.txt  word
[root@nie linux]# ls word/
1.txt  2.txt  word1
[root@nie linux]# ls word/word1/
linux.txt
[root@nie linux]# mv word/* ./
[root@nie linux]# ls
1.txt  2.txt  linux1.txt  word  word1
[root@nie linux]# ls word/
[root@nie linux]# ls word1/
linux.txt

추천

출처blog.csdn.net/weixin_43757336/article/details/130985082