您的位置:首页 > 运维架构 > Linux

Linux之文件管理类命令详解(下)

2016-12-30 02:53 369 查看

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 test1.txt输出:
[root@localhost test]# ll

总计 20drwxr-xr-x 6 root root 4096 10-27 01:58 scf

drwxrwxrwx 2 root root 4096 10-25 17:46 test3

drwxr-xr-x 2 root root 4096 10-25 17:56 test4

drwxr-xr-x 3 root root 4096 10-25 17:56 test5

-rw-r--r-- 1 root root   16 10-28 06:04 test.log

[root@localhost test]# mv test.log test1.txt

[root@localhost test]# ll

总计 20drwxr-xr-x 6 root root 4096 10-27 01:58 scf

-rw-r--r-- 1 root root   16 10-28 06:04 test1.txt

drwxrwxrwx 2 root root 4096 10-25 17:46 test3

drwxr-xr-x 2 root root 4096 10-25 17:56 test4

drwxr-xr-x 3 root root 4096 10-25 17:56 test5
说明:将文件test.log重命名为test1.txt实例二:移动文件命令:mv test1.txt test3输出:
[root@localhost test]# ll

总计 20drwxr-xr-x 6 root root 4096 10-27 01:58 scf

-rw-r--r-- 1 root root   29 10-28 06:05 test1.txt

drwxrwxrwx 2 root root 4096 10-25 17:46 test3

drwxr-xr-x 2 root root 4096 10-25 17:56 test4

drwxr-xr-x 3 root root 4096 10-25 17:56 test5

[root@localhost test]# mv test1.txt test3

[root@localhost test]# ll

总计 16drwxr-xr-x 6 root root 4096 10-27 01:58 scf

drwxrwxrwx 2 root root 4096 10-28 06:09 test3

drwxr-xr-x 2 root root 4096 10-25 17:56 test4

drwxr-xr-x 3 root root 4096 10-25 17:56 test5

[root@localhost test]# cd test3

[root@localhost test3]# ll

总计 4

-rw-r--r-- 1 root root 29 10-28 06:05 test1.txt
说明:将test1.txt文件移到目录test3中实例三:将文件log1.txt,log2.txt,log3.txt移动到目录test3中。命令:mv log1.txt log2.txt log3.txt test3mv -t /opt/soft/test/test4/ log1.txt log2.txt log3.txt输出:
[root@localhost test]# ll

总计 28

-rw-r--r-- 1 root root    8 10-28 06:15 log1.txt

-rw-r--r-- 1 root root   12 10-28 06:15 log2.txt

-rw-r--r-- 1 root root   13 10-28 06:16 log3.txt

drwxrwxrwx 2 root root 4096 10-28 06:09 test3

[root@localhost test]# mv log1.txt log2.txt log3.txt test3

[root@localhost test]# ll

总计 16drwxrwxrwx 2 root root 4096 10-28 06:18 test3

[root@localhost test]# cd test3/

[root@localhost test3]# ll

总计 16

-rw-r--r-- 1 root root  8 10-28 06:15 log1.txt

-rw-r--r-- 1 root root 12 10-28 06:15 log2.txt

-rw-r--r-- 1 root root 13 10-28 06:16 log3.txt

-rw-r--r-- 1 root root 29 10-28 06:05 test1.txt

[root@localhost test3]#

[root@localhost test3]# ll

总计 20

-rw-r--r-- 1 root root    8 10-28 06:15 log1.txt

-rw-r--r-- 1 root root   12 10-28 06:15 log2.txt

-rw-r--r-- 1 root root   13 10-28 06:16 log3.txt

drwxr-xr-x 2 root root 4096 10-28 06:21 logs

-rw-r--r-- 1 root root   29 10-28 06:05 test1.txt

[root@localhost test3]# mv -t /opt/soft/test/test4/ log1.txt log2.tx log3.txt

[root@localhost test3]# cd ..

[root@localhost test]# cd test4/

[root@localhost test4]# ll

总计 12

-rw-r--r-- 1 root root  8 10-28 06:15 log1.txt

-rw-r--r-- 1 root root 12 10-28 06:15 log2.txt

-rw-r--r-- 1 root root 13 10-28 06:16 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输出:
[root@localhost test4]# ll

总计 12

-rw-r--r-- 1 root root  8 10-28 06:15 log1.txt

-rw-r--r-- 1 root root 12 10-28 06:15 log2.txt

-rw-r--r-- 1 root root 13 10-28 06:16 log3.txt

[root@localhost test4]# cat log1.txt

odfdfs

[root@localhost test4]# cat log2.txt

ererwerwer

[root@localhost test4]# mv -i log1.txt log2.txt

mv:是否覆盖“log2.txt”? y

[root@localhost test4]# cat log2.txt

odfdfs
实例五:将文件file1改名为file2,即使file2存在,也是直接覆盖掉。命令:mv -f log3.txt log2.txt输出:
[root@localhost test4]# ll

总计 8

-rw-r--r-- 1 root root  8 10-28 06:15 log2.txt

-rw-r--r-- 1 root root 13 10-28 06:16 log3.txt

[root@localhost test4]# cat log2.txt

odfdfs

[root@localhost test4]# cat log3

cat: log3: 没有那个文件或目录

[root@localhost test4]# ll

总计 8

-rw-r--r-- 1 root root  8 10-28 06:15 log2.txt

-rw-r--r-- 1 root root 13 10-28 06:16 log3.txt

[root@localhost test4]# cat log2.txt

odfdfs

[root@localhost test4]# cat log3.txt

dfosdfsdfdss

[root@localhost test4]# mv -f log3.txt log2.txt

[root@localhost test4]# cat log2.txt

dfosdfsdfdss

[root@localhost test4]# ll

总计 4

-rw-r--r-- 1 root root 13 10-28 06:16 log2.txt
说明:log3.txt的内容直接覆盖了log2.txt内容,-f 这是个危险的选项,使用的时候一定要保持头脑清晰,一般情况下最好不用加上它。实例六:目录的移动命令:mv dir1 dir2输出:
[root@localhost test4]# ll

-rw-r--r-- 1 root root 13 10-28 06:16 log2.txt

[root@localhost test4]# ll

-rw-r--r-- 1 root root 13 10-28 06:16 log2.txt

[root@localhost test4]# cd ..

[root@localhost test]# ll

drwxr-xr-x 6 root root 4096 10-27 01:58 scf

drwxrwxrwx 3 root root 4096 10-28 06:24 test3

drwxr-xr-x 2 root root 4096 10-28 06:48 test4

drwxr-xr-x 3 root root 4096 10-25 17:56 test5

[root@localhost test]# cd test3

[root@localhost test3]# ll

drwxr-xr-x 2 root root 4096 10-28 06:21 logs

-rw-r--r-- 1 root root   29 10-28 06:05 test1.txt

[root@localhost test3]# cd ..

[root@localhost test]# mv test4 test3

[root@localhost test]# ll

drwxr-xr-x 6 root root 4096 10-27 01:58 scf

drwxrwxrwx 4 root root 4096 10-28 06:54 test3

drwxr-xr-x 3 root root 4096 10-25 17:56 test5

[root@localhost test]# cd test3/

[root@localhost test3]# ll

drwxr-xr-x 2 root root 4096 10-28 06:21 logs

-rw-r--r-- 1 root root   29 10-28 06:05 test1.txt

drwxr-xr-x 2 root root 4096 10-28 06:48 test4
说明:如果目录dir2不存在,将目录dir1改名为dir2;否则,将dir1移动到dir2中。实例7:移动当前文件夹下的所有文件到上一级目录命令:mv * ../输出:
[root@localhost test4]# ll

-rw-r--r-- 1 root root 25 10-28 07:02 log1.txt

-rw-r--r-- 1 root root 13 10-28 06:16 log2.txt

[root@localhost test4]# mv * ../

[root@localhost test4]# ll

[root@localhost test4]# cd ..

[root@localhost test3]# ll

-rw-r--r-- 1 root root   25 10-28 07:02 log1.txt

-rw-r--r-- 1 root root   13 10-28 06:16 log2.txt

drwxr-xr-x 2 root root 4096 10-28 06:21 logs

-rw-r--r-- 1 root root   29 10-28 06:05 test1.txt

drwxr-xr-x 2 root root 4096 10-28 07:02 test4
实例八:把当前目录的一个子目录里的文件移动到另一个子目录里命令:mv test3/*.txt test5输出:
[root@localhost test]# ll

drwxr-xr-x 6 root root 4096 10-27 01:58 scf

drwxrwxrwx 4 root root 4096 10-28 07:02 test3

drwxr-xr-x 3 root root 4096 10-25 17:56 test5

[root@localhost test]# cd test3

[root@localhost test3]# ll

-rw-r--r-- 1 root root   25 10-28 07:02 log1.txt

-rw-r--r-- 1 root root   13 10-28 06:16 log2.txt

drwxr-xr-x 2 root root 4096 10-28 06:21 logs

-rw-r--r-- 1 root root   29 10-28 06:05 test1.txt

drwxr-xr-x 2 root root 4096 10-28 07:02 test4

[root@localhost test3]# cd ..

[root@localhost test]# mv test3/*.txt test5

[root@localhost test]# cd test5

[root@localhost test5]# ll

-rw-r--r-- 1 root root   25 10-28 07:02 log1.txt

-rw-r--r-- 1 root root   13 10-28 06:16 log2.txt

-rw-r--r-- 1 root root   29 10-28 06:05 test1.txt

drwxr-xr-x 2 root root 4096 10-25 17:56 test5-1

[root@localhost test5]#     cd ..

[root@localhost test]# cd test3/

[root@localhost test3]# ll

drwxr-xr-x 2 root root 4096 10-28 06:21 logs

drwxr-xr-x 2 root root 4096 10-28 07:02 test4
实例九:文件被覆盖前做简单备份,前面加参数-b命令:mv log1.txt -b log2.txt输出:
[root@localhost test5]# ll

-rw-r--r-- 1 root root   25 10-28 07:02 log1.txt

-rw-r--r-- 1 root root   13 10-28 06:16 log2.txt

-rw-r--r-- 1 root root   29 10-28 06:05 test1.txt

drwxr-xr-x 2 root root 4096 10-25 17:56 test5-1

[root@localhost test5]# mv log1.txt -b log2.txt

mv:是否覆盖“log2.txt”? y

[root@localhost test5]# ll

-rw-r--r-- 1 root root   25 10-28 07:02 log2.txt

-rw-r--r-- 1 root root   13 10-28 06:16 log2.txt~

-rw-r--r-- 1 root root   29 10-28 06:05 test1.txt

drwxr-xr-x 2 root root 4096 10-25 17:56 test5-1

[root@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:使用简单备份:在被覆盖前进行了简单备份,简单备份只能有一份,再次被覆盖时,简单备份也会被覆盖。

rm命令:

rm是一个危险的命令,使用的时候要特别当心,尤其对于新手,否则整个系统就会毁在这个命令(比如在/(根目录)下执行rm * -rf)。所以,我们在执行rm之前最好先确认一下在哪个目录,到底要删除什么东西,操作时保持高度清醒的头脑。1.命令格式:rm [选项] 文件…2.命令功能:删除一个目录中的一个或多个文件或目录,如果没有使用- r选项,则rm不会删除目录。如果使用 rm 来删除文件,通常仍可以将该文件恢复原状。3.命令参数:
-f, --force    忽略不存在的文件,从不给出提示。

-i, --interactive 进行交互式删除

-r, -R, --recursive   指示rm将参数中列出的全部目录和子目录均递归地删除。

-v, --verbose    详细显示进行的步骤

--help     显示此帮助信息并退出

--version  输出版本信息并退出
4.命令实例:实例一:删除文件file,系统会先询问是否删除。命令:rm 文件名输出:
[root@localhost test1]# ll

总计 4

-rw-r--r-- 1 root root 56 10-26 14:31 log.log

root@localhost test1]# rm log.log

rm:是否删除 一般文件 “log.log”? y

root@localhost test1]# ll
总计 0说明:输入rm log.log命令后,系统会询问是否删除,输入y后就会删除文件,不想删除则数据n。实例二:强行删除file,系统不再提示。命令:rm -f log1.log输出:
[root@localhost test1]# ll

总计 4

-rw-r--r-- 1 root root 23 10-26 14:40 log1.log

[root@localhost test1]# rm -f log1.log

[root@localhost test1]# ll

总计 0
**实例三:删除任何.log文件;删除前逐一询问确认 **命令:rm -i *.log输出:
[root@localhost test1]# ll

总计 8

-rw-r--r-- 1 root root 11 10-26 14:45 log1.log

-rw-r--r-- 1 root root 24 10-26 14:45 log2.log

[root@localhost test1]# rm -i *.log

rm:是否删除 一般文件 “log1.log”? y

rm:是否删除 一般文件 “log2.log”? y

[root@localhost test1]# ll

总计 0
实例四:将 test1子目录及子目录中所有档案删除命令:rm -r test1输出:
[root@localhost test]# ll

总计 24drwxr-xr-x 7 root root 4096 10-25 18:07 scf

drwxr-xr-x 2 root root 4096 10-26 14:51 test1

drwxr-xr-x 3 root root 4096 10-25 17:44 test2

drwxrwxrwx 2 root root 4096 10-25 17:46 test3

drwxr-xr-x 2 root root 4096 10-25 17:56 test4

drwxr-xr-x 3 root root 4096 10-25 17:56 test5

[root@localhost test]# rm -r test1

rm:是否进入目录 “test1”? y

rm:是否删除 一般文件 “test1/log3.log”? y

rm:是否删除 目录 “test1”? y

[root@localhost test]# ll

总计 20drwxr-xr-x 7 root root 4096 10-25 18:07 scf

drwxr-xr-x 3 root root 4096 10-25 17:44 test2

drwxrwxrwx 2 root root 4096 10-25 17:46 test3

drwxr-xr-x 2 root root 4096 10-25 17:56 test4

drwxr-xr-x 3 root root 4096 10-25 17:56 test5
实例五:rm -rf test2命令会将 test2 子目录及子目录中所有档案删除,并且不用一一确认命令:rm -rf test2输出:
[root@localhost test]# rm -rf test2

[root@localhost test]# ll

总计 16drwxr-xr-x 7 root root 4096 10-25 18:07 scf

drwxrwxrwx 2 root root 4096 10-25 17:46 test3

drwxr-xr-x 2 root root 4096 10-25 17:56 test4

drwxr-xr-x 3 root root 4096 10-25 17:56 test5
实例六:删除以 -f 开头的文件命令:rm -- -f输出:
[root@localhost test]# touch -- -f

[root@localhost test]# ls -- -f

-f
[root@localhost test]# rm -- -f

rm:是否删除 一般空文件 “-f”? y

[root@localhost test]# ls -- -f

ls: -f: 没有那个文件或目录
也可以使用下面的操作步骤:
[root@localhost test]# touch ./-f

[root@localhost test]# ls ./-f

./-f[root@localhost test]# rm ./-f

rm:是否删除 一般空文件 “./-f”? y

[root@localhost test]#
实例七:自定义回收站功能命令:myrm(){ D=/tmp/$(date +%Y%m%d%H%M%S); mkdir -p $D; mv "$@" $D && echo "moved to $D ok"; }输出:
[root@localhost test]# myrm(){ D=/tmp/$(date +%Y%m%d%H%M%S); mkdir -p $D;   mv "$@" $D && echo "moved to $D ok"; }

[root@localhost test]# alias rm='myrm'

[root@localhost test]# touch 1.log 2.log 3.log

[root@localhost test]# ll

总计 16

-rw-r--r-- 1 root root    0 10-26 15:08 1.log

-rw-r--r-- 1 root root    0 10-26 15:08 2.log

-rw-r--r-- 1 root root    0 10-26 15:08 3.log

drwxr-xr-x 7 root root 4096 10-25 18:07 scf

drwxrwxrwx 2 root root 4096 10-25 17:46 test3

drwxr-xr-x 2 root root 4096 10-25 17:56 test4

drwxr-xr-x 3 root root 4096 10-25 17:56 test5

[root@localhost test]# rm [123].log

moved to /tmp/20121026150901 ok

[root@localhost test]# ll

总计 16drwxr-xr-x 7 root root 4096 10-25 18:07 scf

drwxrwxrwx 2 root root 4096 10-25 17:46 test3

drwxr-xr-x 2 root root 4096 10-25 17:56 test4

drwxr-xr-x 3 root root 4096 10-25 17:56 test5

[root@localhost test]# ls /tmp/20121026150901/

1.log  2.log  3.log
说明:上面的操作过程模拟了回收站的效果,即删除文件的时候只是把文件放到一个临时目录中,这样在需要的时候还可以恢复过来。

rmdir命令:

rmdir命令。rmdir是常用的命令,该命令的功能是删除空目录,一个目录被删除之前必须是空的。(注意,rm - r dir命令可代替rmdir,但是有很大危险性。)删除某目录时也必须具有对父目录的写权限。1.命令格式:rmdir [选项]... 目录...2.命令功能:该命令从一个目录中删除一个或多个子目录项,删除某目录时也必须具有对父目录的写权限。3.命令参数:
- p 递归删除目录dirname,当子目录删除后其父目录为空时,也一同被删除。如果整个路径被删除或者由于某种原因保留部分路径,则系统在标准输出上显示相应的信息。

-v, --verbose  显示指令执行过程
4.命令实例:实例一:rmdir 不能删除非空目录命令:rmdir doc输出:
[root@localhost scf]# tree

.

|-- bin

|-- doc

|   |-- info

|   `-- product

|-- lib

|-- logs

|   |-- info

|   `-- product

`-- service

`-- deploy

|-- info

`-- product

12 directories, 0 files

[root@localhost scf]# rmdir doc

rmdir: doc: 目录非空

[root@localhost scf]# rmdir doc/info

[root@localhost scf]# rmdir doc/product

[root@localhost scf]# tree

.

|-- bin

|-- doc

|-- lib

|-- logs

|   |-- info

|   `-- product

`-- service

`-- deploy

|-- info

`-- product

10 directories, 0 files
说明:rmdir 目录名 命令不能直接删除非空目录实例2:rmdir -p 当子目录被删除后使它也成为空目录的话,则顺便一并删除命令:rmdir -p logs输出:
[root@localhost scf]# tree

.

|-- bin

|-- doc

|-- lib

|-- logs

|   `-- product

`-- service

`-- deploy

|-- info

`-- product

10 directories, 0 files

[root@localhost scf]# rmdir -p logs

rmdir: logs: 目录非空

[root@localhost scf]# tree

.

|-- bin

|-- doc

|-- lib

|-- logs

|   `-- product

`-- service

`-- deploy

|-- info

`-- product

9 directories, 0 files

[root@localhost scf]# rmdir -p logs/product

[root@localhost scf]# tree

.

|-- bin

|-- doc

|-- lib

`-- service

`-- deploy

|-- info

`-- product

7 directories, 0 files

tree命令:

tree 显示目录树
-d: 只显示目录
-L level:指定显示的层级数目
-P pattern: 只显示由指定pattern匹配到的路径
实例1:显示目录与文件
[root@bash etc]# cd /var/log
[root@bash log]# ls
anaconda  cron       glusterfs  ppp                spooler             yum.log
audit     cups       lastlog    sa                 tallylog
boot.log  dmesg      maillog    samba              tuned
btmp      dmesg.old  mariadb    secure             wpa_supplicant.log
chrony    gdm        messages   speech-dispatcher  wtmp
[root@bash log]# tree
.
├── anaconda
│   ├── anaconda.log
│   ├── ifcfg.log
│   ├── journal.log
│   ├── ks-script-LLsAAH.log
│   ├── ks-script-wr1UCo.log
│   ├── packaging.log
│   ├── program.log
│   ├── storage.log
│   └── syslog
├── audit
│   └── audit.log
├── boot.log
├── btmp
├── chrony
├── cron
├── cups
│   ├── access_log
│   ├── error_log
│   └── page_log
├── dmesg
├── dmesg.old
├── gdm
├── glusterfs
├── lastlog
├── maillog
├── mariadb
│   └── mariadb.log
├── messages
├── ppp
├── sa
│   ├── sa25
│   ├── sa26
│   ├── sa27
│   ├── sa28
│   ├── sa29
│   ├── sa30
│   ├── sar25
│   ├── sar27
│   ├── sar28
│   └── sar29
├── samba
│   └── old
├── secure
├── speech-dispatcher
├── spooler
├── tallylog
├── tuned
│   └── tuned.log
├── wpa_supplicant.log
├── wtmp
└── yum.log

13 directories, 39 files
实例2:只显示目录
[root@bash log]# tree -d
.
├── anaconda
├── audit
├── chrony
├── cups
├── gdm
├── glusterfs
├── mariadb
├── ppp
├── sa
├── samba
│   └── old
├── speech-dispatcher
└── tuned
实例3:制定层级显示
[root@bash ~]# tree -L 2 /var/log
/var/log
├── anaconda
│   ├── anaconda.log
│   ├── ifcfg.log
│   ├── journal.log
│   ├── ks-script-LLsAAH.log
│   ├── ks-script-wr1UCo.log
│   ├── packaging.log
│   ├── program.log
│   ├── storage.log
│   └── syslog
├── audit
│   └── audit.log
├── boot.log
├── btmp
├── chrony
├── cron
├── cups
│   ├── access_log
│   ├── error_log
│   └── page_log
├── dmesg
├── dmesg.old
├── gdm
├── glusterfs
├── lastlog
├── maillog
├── mariadb
│   └── mariadb.log
├── messages
├── ppp
├── sa
│   ├── sa25
│   ├── sa26
│   ├── sa27
│   ├── sa28
│   ├── sa29
│   ├── sa30
│   ├── sar25
│   ├── sar27
│   ├── sar28
│   └── sar29
├── samba
│   └── old
├── secure
├── speech-dispatcher
├── spooler
├── tallylog
├── tuned
│   └── tuned.log
├── wpa_supplicant.log
├── wtmp
└── yum.log

13 directories, 39 files
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Liunx 文件管理