Linux常用基本命令( rmdir, rm, mv )

时间:2022-05-10 17:45:50

1,rmdir,一个很鸡肋的命令,只能删除空目录

ghostwu@dev:~/linux/cp$ ls
1.txt  2.txt  a  a2  a3
ghostwu@dev:~/linux/cp$ rmdir a
rmdir: failed to remove 'a': Directory not empty
ghostwu@dev:~/linux/cp$ mkdir b
ghostwu@dev:~/linux/cp$ ls
1.txt  2.txt  a  a2  a3  b
ghostwu@dev:~/linux/cp$ rmdir b
ghostwu@dev:~/linux/cp$ ls
1.txt  2.txt  a  a2  a3

2,mv: 移动文件或者重命名

把1.txt重命名为11.txt

ghostwu@dev:~/linux/cp$ ls
1.txt  2.txt  a  a2  a3
ghostwu@dev:~/linux/cp$ mv 1.txt 11.txt
ghostwu@dev:~/linux/cp$ ls
11.txt  2.txt  a  a2  a3

把11.txt移动到b目录

ghostwu@dev:~/linux/cp$ ls
11.txt  2.txt  a  a2  a3
ghostwu@dev:~/linux/cp$ mkdir b
ghostwu@dev:~/linux/cp$ ls
11.txt  2.txt  a  a2  a3  b
ghostwu@dev:~/linux/cp$ mv 11.txt b
ghostwu@dev:~/linux/cp$ ls
2.txt  a  a2  a3  b
ghostwu@dev:~/linux/cp$ tree b
b
└── 11.txt

-f:如果目标文件存在,不会询问直接覆盖

ghostwu@dev:~/linux/cp$ ls
22.txt  2.txt  a  a2  a3  b
ghostwu@dev:~/linux/cp$ ls b
11.txt
ghostwu@dev:~/linux/cp$ mv -f 2.txt b/11.txt 
ghostwu@dev:~/linux/cp$ ls
22.txt  a  a2  a3  b

-i: 如果目标文件存在,询问是否覆盖

ghostwu@dev:~/linux/cp$ ls
22.txt  2.txt  a  a2  a3  b
ghostwu@dev:~/linux/cp$ ls b
11.txt
ghostwu@dev:~/linux/cp$ mv -i 2.txt b/11.txt
mv: overwrite 'b/11.txt'? n
ghostwu@dev:~/linux/cp$ ls
22.txt  2.txt  a  a2  a3  b
ghostwu@dev:~/linux/cp$ mv -i 2.txt b/11.txt 
mv: overwrite 'b/11.txt'? y
ghostwu@dev:~/linux/cp$ ls
22.txt  a  a2  a3  b

-n: 不覆盖已经存在的文件

ghostwu@dev:~/linux/cp$ ls
22.txt  a  a2  a3  b
ghostwu@dev:~/linux/cp$ ls b
11.txt
ghostwu@dev:~/linux/cp$ mv -n 22.txt b/11.txt
ghostwu@dev:~/linux/cp$ ls
22.txt  a  a2  a3  b

-u: 当文件不存在,或者源文件比目标文件新的时候,才移动

ghostwu@dev:~/linux/cp$ ls -l
total 16
-rw-rw-r-- 1 ghostwu ghostwu    0 5月   6 18:28 22.txt
drwxrwxr-x 3 ghostwu ghostwu 4096 5月   6 18:06 a
drwxrwxr-x 3 ghostwu ghostwu 4096 5月   6 18:07 a2
drwxrwxr-x 3 ghostwu ghostwu 4096 5月   6 18:06 a3
drwxrwxr-x 2 ghostwu ghostwu 4096 5月   6 18:31 b
ghostwu@dev:~/linux/cp$ ls -l b
total 0
-rw-rw-r-- 1 ghostwu ghostwu 0 5月   6 18:30 11.txt
ghostwu@dev:~/linux/cp$ mv -u 22.txt b/11.txt
ghostwu@dev:~/linux/cp$ ls -l
total 16
-rw-rw-r-- 1 ghostwu ghostwu    0 5月   6 18:28 22.txt
drwxrwxr-x 3 ghostwu ghostwu 4096 5月   6 18:06 a
drwxrwxr-x 3 ghostwu ghostwu 4096 5月   6 18:07 a2
drwxrwxr-x 3 ghostwu ghostwu 4096 5月   6 18:06 a3
drwxrwxr-x 2 ghostwu ghostwu 4096 5月   6 18:31 b
ghostwu@dev:~/linux/cp$ ls b
11.txt
ghostwu@dev:~/linux/cp$ ls -l b
total 0
-rw-rw-r-- 1 ghostwu ghostwu 0 5月   6 18:30 11.txt
ghostwu@dev:~/linux/cp$ touch 22.txt
ghostwu@dev:~/linux/cp$ ls -l 22.txt
-rw-rw-r-- 1 ghostwu ghostwu 0 5月   6 18:37 22.txt
ghostwu@dev:~/linux/cp$ mv -u 22.txt b/11.txt
ghostwu@dev:~/linux/cp$ ls -l
total 16
drwxrwxr-x 3 ghostwu ghostwu 4096 5月   6 18:06 a
drwxrwxr-x 3 ghostwu ghostwu 4096 5月   6 18:07 a2
drwxrwxr-x 3 ghostwu ghostwu 4096 5月   6 18:06 a3
drwxrwxr-x 2 ghostwu ghostwu 4096 5月   6 18:37 b
ghostwu@dev:~/linux/cp$ ls -l b
total 0
-rw-rw-r-- 1 ghostwu ghostwu 0 5月   6 18:37 11.txt
ghostwu@dev:~/linux/cp$ ls
a  a2  a3  b
ghostwu@dev:~/linux/cp$ touch a.txt
ghostwu@dev:~/linux/cp$ ls
a  a2  a3  a.txt  b
ghostwu@dev:~/linux/cp$ ls -l b
total 0
-rw-rw-r-- 1 ghostwu ghostwu 0 5月   6 18:37 11.txt
ghostwu@dev:~/linux/cp$ mv a.txt b
ghostwu@dev:~/linux/cp$ ls -l
total 16
drwxrwxr-x 3 ghostwu ghostwu 4096 5月   6 18:06 a
drwxrwxr-x 3 ghostwu ghostwu 4096 5月   6 18:07 a2
drwxrwxr-x 3 ghostwu ghostwu 4096 5月   6 18:06 a3
drwxrwxr-x 2 ghostwu ghostwu 4096 5月   6 18:38 b
ghostwu@dev:~/linux/cp$ ls -l b
total 0
-rw-rw-r-- 1 ghostwu ghostwu 0 5月   6 18:37 11.txt
-rw-rw-r-- 1 ghostwu ghostwu 0 5月   6 18:38 a.txt

rm: 删除文件

-i: 带提示

-f:强制删除

-r:递归删除

ghostwu@dev:~/linux/cp$ ls
a  a2  a3  b
ghostwu@dev:~/linux/cp$ touch {a..f}.txt
ghostwu@dev:~/linux/cp$ ls
a  a2  a3  a.txt  b  b.txt  c.txt  d.txt  e.txt  f.txt
ghostwu@dev:~/linux/cp$ rm a.txt 
ghostwu@dev:~/linux/cp$ ls
a  a2  a3  b  b.txt  c.txt  d.txt  e.txt  f.txt
ghostwu@dev:~/linux/cp$ rm -i b.txt
rm: remove regular empty file 'b.txt'? n
ghostwu@dev:~/linux/cp$ ls
a  a2  a3  b  b.txt  c.txt  d.txt  e.txt  f.txt
ghostwu@dev:~/linux/cp$ rm -i b.txt 
rm: remove regular empty file 'b.txt'? y
ghostwu@dev:~/linux/cp$ ls
a  a2  a3  b  c.txt  d.txt  e.txt  f.txt
ghostwu@dev:~/linux/cp$ rm -f c.txt 
ghostwu@dev:~/linux/cp$ ls
a  a2  a3  b  d.txt  e.txt  f.txt
ghostwu@dev:~/linux/cp$ ls
a3  d.txt  e.txt  f.txt
ghostwu@dev:~/linux/cp$ tree a3
a3
├── b
│   ├── 1.txt
│   ├── 2.txt
│   ├── 3.txt
│   ├── 4.txt
│   └── 5.txt
├── c
│   ├── 1.txt
│   ├── 2.txt
│   ├── 3.txt
│   ├── 4.txt
│   └── 5.txt
└── d
    ├── 1.txt
    ├── 2.txt
    ├── 3.txt
    ├── 4.txt
    └── 5.txt

3 directories, 15 files
ghostwu@dev:~/linux/cp$ rm -r a3
ghostwu@dev:~/linux/cp$ ls
d.txt  e.txt  f.txt