Linux基本指令(上)

文章目录

  • 目录
    • [1. Linux的介绍](#1. Linux的介绍)
    • [2. 基本指令](#2. 基本指令)
      • [2.1 ls 指令](#2.1 ls 指令)
      • [2.2 pwd 命令](#2.2 pwd 命令)
      • [2.3 cd 指令](#2.3 cd 指令)
      • [2.4 touch 指令](#2.4 touch 指令)
      • [2.5 mkdir 指令](#2.5 mkdir 指令)
      • [2.6 rmdir 指令 && rm 指令](#2.6 rmdir 指令 && rm 指令)
      • [2.7 man 指令](#2.7 man 指令)
      • [2.8 cp 指令](#2.8 cp 指令)

目录

  • Linux的介绍
  • 基本指令

1. Linux的介绍


2. 基本指令

2.1 ls 指令

语法: ls [选项] [目录或文件]
功能: 对于目录,该命令列出该目录下的所有子目录与文件;对于文件,将列出文件名以及其他信息。
常用选项:

  • -l 列出文件的详细信息
c 复制代码
[root@VM-4-3-centos lesson2]# ls
dir  dir2
[root@VM-4-3-centos lesson2]# ls -l
total 8
drwxr-xr-x 2 root root 4096 Jan 29 15:35 dir
drwxr-xr-x 2 root root 4096 Jan 29 15:36 dir2
  • -a 列出目录下的所有文件,包括以 . 开头的隐含文件(Linux中,以 . 开头的文件,我们叫做隐藏文件)
c 复制代码
[root@VM-4-3-centos lesson2]# ls -a
.  ..  dir  dir2
c 复制代码
[root@VM-4-3-centos lesson2]# ls -l
total 8
drwxr-xr-x 2 root root 4096 Jan 29 15:35 dir
drwxr-xr-x 2 root root 4096 Jan 29 15:36 dir2
[root@VM-4-3-centos lesson2]# mkdir .seeme
[root@VM-4-3-centos lesson2]# ls
dir  dir2
[root@VM-4-3-centos lesson2]# ls -l
total 8
drwxr-xr-x 2 root root 4096 Jan 29 15:35 dir
drwxr-xr-x 2 root root 4096 Jan 29 15:36 dir2
[root@VM-4-3-centos lesson2]# ls -a
.  ..  dir  dir2  .seeme

命令中的选项,一次可以传递多个

c 复制代码
[root@VM-4-3-centos lesson2]# ls -a -l
total 16
drwxr-xr-x 4 root root 4096 Jan 29 15:36 .
drwxr-xr-x 3 root root 4096 Jan 29 15:27 ..
drwxr-xr-x 2 root root 4096 Jan 29 15:35 dir
drwxr-xr-x 2 root root 4096 Jan 29 15:36 dir2

选项也可以拼接在一起,顺序可以随意

c 复制代码
[root@VM-4-3-centos lesson2]# ls -al
total 16
drwxr-xr-x 4 root root 4096 Jan 29 15:36 .
drwxr-xr-x 3 root root 4096 Jan 29 15:27 ..
drwxr-xr-x 2 root root 4096 Jan 29 15:35 dir
drwxr-xr-x 2 root root 4096 Jan 29 15:36 dir2
[root@VM-4-3-centos lesson2]# ls -la
total 16
drwxr-xr-x 4 root root 4096 Jan 29 15:36 .
drwxr-xr-x 3 root root 4096 Jan 29 15:27 ..
drwxr-xr-x 2 root root 4096 Jan 29 15:35 dir
drwxr-xr-x 2 root root 4096 Jan 29 15:36 dir2

补充

c 复制代码
[root@VM-4-3-centos lesson2]# touch test.c
[root@VM-4-3-centos lesson2]# ls -al
total 20
drwxr-xr-x 5 root root 4096 Jan 29 16:09 .
drwxr-xr-x 3 root root 4096 Jan 29 15:27 ..
drwxr-xr-x 2 root root 4096 Jan 29 15:35 dir
drwxr-xr-x 2 root root 4096 Jan 29 15:36 dir2
drwxr-xr-x 2 root root 4096 Jan 29 16:01 .seeme
-rw-r--r-- 1 root root    0 Jan 29 16:09 test.c
[root@VM-4-3-centos lesson2]# touch .hello
[root@VM-4-3-centos lesson2]# ls -l
total 8
drwxr-xr-x 2 root root 4096 Jan 29 15:35 dir
drwxr-xr-x 2 root root 4096 Jan 29 15:36 dir2
-rw-r--r-- 1 root root    0 Jan 29 16:09 test.c
[root@VM-4-3-centos lesson2]# ls -al
total 20
drwxr-xr-x 5 root root 4096 Jan 29 16:48 .
drwxr-xr-x 3 root root 4096 Jan 29 15:27 ..
drwxr-xr-x 2 root root 4096 Jan 29 15:35 dir
drwxr-xr-x 2 root root 4096 Jan 29 15:36 dir2
-rw-r--r-- 1 root root    0 Jan 29 16:48 .hello
drwxr-xr-x 2 root root 4096 Jan 29 16:01 .seeme
-rw-r--r-- 1 root root    0 Jan 29 16:09 test.c
  • -d 将目录像文件一样显示,而不是显示其下的文件
c 复制代码
[root@VM-4-3-centos /]# ls /root/112/lesson2
dir  dir2  test.c
[root@VM-4-3-centos /]# ls -al /root/112/lesson2
total 20
drwxr-xr-x 5 root root 4096 Jan 29 16:48 .
drwxr-xr-x 3 root root 4096 Jan 29 15:27 ..
drwxr-xr-x 2 root root 4096 Jan 29 15:35 dir
drwxr-xr-x 2 root root 4096 Jan 29 15:36 dir2
-rw-r--r-- 1 root root    0 Jan 29 16:48 .hello
drwxr-xr-x 2 root root 4096 Jan 29 16:01 .seeme
-rw-r--r-- 1 root root    0 Jan 29 16:09 test.c
[root@VM-4-3-centos /]# ls -d /root/112/lesson2
/root/112/lesson2
[root@VM-4-3-centos /]# ls -ld /root/112/lesson2
drwxr-xr-x 5 root root 4096 Jan 29 16:48 /root/112/lesson2
[root@VM-4-3-centos /]# ls -l /root/112/lesson2
total 8
drwxr-xr-x 2 root root 4096 Jan 29 15:35 dir
drwxr-xr-x 2 root root 4096 Jan 29 15:36 dir2
-rw-r--r-- 1 root root    0 Jan 29 16:09 test.c

2.2 pwd 命令

语法: pwd
功能: 显示用户当前所在的目录

c 复制代码
[root@VM-4-3-centos lesson2]# pwd
/root/112/lesson2
[root@VM-4-3-centos lesson2]# ls
dir  dir2  test.c
[root@VM-4-3-centos lesson2]# ls /root
112
[root@VM-4-3-centos lesson2]# ls -l /root 
total 4
drwxr-xr-x 3 root root 4096 Jan 29 15:27 112
[root@VM-4-3-centos lesson2]# ls -la /root
total 56
dr-xr-x---.  7 root root 4096 Jan 29 15:26 .
dr-xr-xr-x. 19 root root 4096 Jan 29 17:43 ..
drwxr-xr-x   3 root root 4096 Jan 29 15:27 112
-rw-------   1 root root 2225 Jan 29 17:42 .bash_history
-rw-r--r--.  1 root root   18 Dec 29  2013 .bash_logout
-rw-r--r--.  1 root root  176 Dec 29  2013 .bash_profile
-rw-r--r--.  1 root root  176 Dec 29  2013 .bashrc
drwxr-xr-x   4 root root 4096 Nov 24 03:19 .cache
drwxr-xr-x   3 root root 4096 Mar  7  2019 .config
-rw-r--r--.  1 root root  100 Dec 29  2013 .cshrc
drwxr-xr-x   2 root root 4096 Nov 12 22:27 .pip
-rw-r--r--   1 root root   73 Nov 12 22:27 .pydistutils.cfg
drwx------   2 root root 4096 Nov  5  2019 .ssh
-rw-r--r--.  1 root root  129 Dec 29  2013 .tcshrc

2.3 cd 指令

语法: cd 目录名
功能: 改变工作目录,将当前工作目录改变到指定的目录下

c 复制代码
[root@VM-4-3-centos lesson2]# pwd
/root/112/lesson2
[root@VM-4-3-centos lesson2]# cd /
[root@VM-4-3-centos /]# pwd
/
[root@VM-4-3-centos /]# cd /root/112/lesson2
[root@VM-4-3-centos lesson2]# pwd
/root/112/lesson2
[root@VM-4-3-centos lesson2]# ls -al
total 20
drwxr-xr-x 5 root root 4096 Jan 29 16:48 .
drwxr-xr-x 3 root root 4096 Jan 29 15:27 ..
drwxr-xr-x 2 root root 4096 Jan 29 15:35 dir
drwxr-xr-x 2 root root 4096 Jan 29 15:36 dir2
-rw-r--r-- 1 root root    0 Jan 29 16:48 .hello
drwxr-xr-x 2 root root 4096 Jan 29 16:01 .seeme
-rw-r--r-- 1 root root    0 Jan 29 16:09 test.c
[root@VM-4-3-centos lesson2]# pwd
/root/112/lesson2
[root@VM-4-3-centos lesson2]# cd ..
[root@VM-4-3-centos 112]# pwd
/root/112
[root@VM-4-3-centos 112]# cd ..
[root@VM-4-3-centos ~]# pwd
/root
[root@VM-4-3-centos ~]# cd ..
[root@VM-4-3-centos /]# pwd
/
[root@VM-4-3-centos /]# cd ..
[root@VM-4-3-centos /]# pwd
/
[root@VM-4-3-centos /]# cd ..
[root@VM-4-3-centos /]# pwd
/
[root@VM-4-3-centos /]# cd /root/112/lesson2
[root@VM-4-3-centos lesson2]# pwd
/root/112/lesson2
[root@VM-4-3-centos lesson2]# ls
dir  dir2  test.c
[root@VM-4-3-centos lesson2]# ls -al
total 20
drwxr-xr-x 5 root root 4096 Jan 29 16:48 .
drwxr-xr-x 3 root root 4096 Jan 29 15:27 ..
drwxr-xr-x 2 root root 4096 Jan 29 15:35 dir
drwxr-xr-x 2 root root 4096 Jan 29 15:36 dir2
-rw-r--r-- 1 root root    0 Jan 29 16:48 .hello
drwxr-xr-x 2 root root 4096 Jan 29 16:01 .seeme
-rw-r--r-- 1 root root    0 Jan 29 16:09 test.c
[root@VM-4-3-centos lesson2]# cd .
[root@VM-4-3-centos lesson2]# pwd
/root/112/lesson2
[root@VM-4-3-centos lesson2]# cd ..
[root@VM-4-3-centos 112]# pwd
/root/112
[root@VM-4-3-centos 112]# cd ..
[root@VM-4-3-centos ~]# pwd
/root
[root@VM-4-3-centos ~]# cd ..
[root@VM-4-3-centos /]# pwd
/
[root@VM-4-3-centos /]# cd ..
[root@VM-4-3-centos /]# pwd
/
[root@VM-4-3-centos /]# cd ..
[root@VM-4-3-centos /]# pwd
/
[root@VM-4-3-centos /]# ls /root/112/lesson2
dir  dir2  test.c
[root@VM-4-3-centos /]# ls -al /root/112/lesson2
total 20
drwxr-xr-x 5 root root 4096 Jan 29 16:48 .
drwxr-xr-x 3 root root 4096 Jan 29 15:27 ..
drwxr-xr-x 2 root root 4096 Jan 29 15:35 dir
drwxr-xr-x 2 root root 4096 Jan 29 15:36 dir2
-rw-r--r-- 1 root root    0 Jan 29 16:48 .hello
drwxr-xr-x 2 root root 4096 Jan 29 16:01 .seeme
-rw-r--r-- 1 root root    0 Jan 29 16:09 test.c
[root@VM-4-3-centos /]# ls -d /root/112/lesson2
/root/112/lesson2
[root@VM-4-3-centos /]# ls -ld /root/112/lesson2
drwxr-xr-x 5 root root 4096 Jan 29 16:48 /root/112/lesson2
[root@VM-4-3-centos /]# ls -l /root/112/lesson2
total 8
drwxr-xr-x 2 root root 4096 Jan 29 15:35 dir
drwxr-xr-x 2 root root 4096 Jan 29 15:36 dir2
-rw-r--r-- 1 root root    0 Jan 29 16:09 test.c
[root@VM-4-3-centos /]# pwd
/
[root@VM-4-3-centos /]# ls /
bin   data  etc   lib    lost+found  mnt  proc  run   srv  tmp  var
boot  dev   home  lib64  media       opt  root  sbin  sys  usr
[root@VM-4-3-centos /]# ls -l /
total 72
lrwxrwxrwx.   1 root root     7 Mar  7  2019 bin -> usr/bin
dr-xr-xr-x.   5 root root  4096 Jul  8  2024 boot
drwxr-xr-x    2 root root  4096 Nov  5  2019 data
drwxr-xr-x   19 root root  3020 Nov 13 18:46 dev
drwxr-xr-x.  95 root root 12288 Jan 28 15:41 etc
drwxr-xr-x.   3 root root  4096 Nov 14 15:58 home
lrwxrwxrwx.   1 root root     7 Mar  7  2019 lib -> usr/lib
lrwxrwxrwx.   1 root root     9 Mar  7  2019 lib64 -> usr/lib64
drwx------.   2 root root 16384 Mar  7  2019 lost+found
drwxr-xr-x.   2 root root  4096 Apr 11  2018 media
drwxr-xr-x.   2 root root  4096 Apr 11  2018 mnt
drwxr-xr-x.   4 root root  4096 Nov 12 22:27 opt
dr-xr-xr-x  100 root root     0 Nov 13 18:46 proc
dr-xr-x---.   7 root root  4096 Jan 29 15:26 root
drwxr-xr-x   25 root root   920 Jan 21 17:06 run
lrwxrwxrwx.   1 root root     8 Mar  7  2019 sbin -> usr/sbin
drwxr-xr-x.   2 root root  4096 Apr 11  2018 srv
dr-xr-xr-x   13 root root     0 Jan 29 17:53 sys
drwxrwxrwt.   9 root root  4096 Jan 29 03:40 tmp
drwxr-xr-x.  14 root root  4096 Jan  8  2021 usr
drwxr-xr-x.  20 root root  4096 Jan  8  2021 var
[root@VM-4-3-centos /]# ls -ld /
dr-xr-xr-x. 19 root root 4096 Jan 29 19:54 /

/ 是目录,里面可以放普通文件和目录;/ 里面的目录也可以放普通文件和目录

因此,Linux的目录结构,整体是一颗多叉树的形状!


如何证明?

我们可以用 tree 命令来证明:

首先,我们要下载 tree 命令(因为它不是自带的),输入以下指令:

c 复制代码
yum install -y tree

这样我们就可以使用 tree 命令了:

格式: tree 指定目录

c 复制代码
[root@VM-4-3-centos /]# tree /root
/root
`-- 112
    `-- lesson2
        |-- dir
        |-- dir2
        `-- test.c

4 directories, 1 file
c 复制代码
[root@VM-4-3-centos /]# cd /root
[root@VM-4-3-centos ~]# cd 112
[root@VM-4-3-centos 112]# cd lesson2
[root@VM-4-3-centos lesson2]# cd dir
[root@VM-4-3-centos dir]# pwd
/root/112/lesson2/dir
[root@VM-4-3-centos dir]# whoami
root
[root@VM-4-3-centos dir]# touch /root/112/hello.txt
[root@VM-4-3-centos dir]# ls /root/112
hello.txt  lesson2
[root@VM-4-3-centos dir]# pwd
/root/112/lesson2/dir
[root@VM-4-3-centos dir]# ls /root/112/hello.txt
/root/112/hello.txt
[root@VM-4-3-centos dir]# ls ../../hello.txt
../../hello.txt
[root@VM-4-3-centos dir]# pwd
/root/112/lesson2/dir
[root@VM-4-3-centos dir]# ls /root/112/lesson2/dir/../../hello.txt
/root/112/lesson2/dir/../../hello.txt



c 复制代码
[root@VM-4-3-centos lesson2]# pwd
/root/112/lesson2
[root@VM-4-3-centos lesson2]# ll
total 8
drwxr-xr-x 2 root root 4096 Jan 29 15:35 dir
drwxr-xr-x 2 root root 4096 Jan 29 15:36 dir2
-rw-r--r-- 1 root root    0 Jan 29 16:09 test.c
[root@VM-4-3-centos lesson2]# mkdir empty
[root@VM-4-3-centos lesson2]# ll
total 12
drwxr-xr-x 2 root root 4096 Jan 29 15:35 dir
drwxr-xr-x 2 root root 4096 Jan 29 15:36 dir2
drwxr-xr-x 2 root root 4096 Jan 29 22:22 empty
-rw-r--r-- 1 root root    0 Jan 29 16:09 test.c
[root@VM-4-3-centos lesson2]# cd empty
[root@VM-4-3-centos empty]# ls
[root@VM-4-3-centos empty]# ls -al
total 8
drwxr-xr-x 2 root root 4096 Jan 29 22:22 .
drwxr-xr-x 6 root root 4096 Jan 29 22:22 ..
[root@VM-4-3-centos empty]# cd /
[root@VM-4-3-centos /]# ls
bin   dev   lib         media  proc  sbin  tmp
boot  etc   lib64       mnt    root  srv   usr
data  home  lost+found  opt    run   sys   var
[root@VM-4-3-centos /]# ls -a
.    boot  etc   lib64       mnt   root  srv  usr
..   data  home  lost+found  opt   run   sys  var
bin  dev   lib   media       proc  sbin  tmp
[root@VM-4-3-centos /]# pwd
/
[root@VM-4-3-centos /]# cd home
[root@VM-4-3-centos home]# pwd
/home
[root@VM-4-3-centos home]# ls
lighthouse
[root@VM-4-3-centos home]# ls -a
.  ..  lighthouse

cd 命令的特殊用法:

  1. cd -

作用: 跳转到最近的上一次路径

c 复制代码
[root@VM-4-3-centos 112]# pwd
/root/112
[root@VM-4-3-centos 112]# cd lesson2
[root@VM-4-3-centos lesson2]# pwd
/root/112/lesson2
[root@VM-4-3-centos lesson2]# cd /
[root@VM-4-3-centos /]# pwd
/
[root@VM-4-3-centos /]# cd -
/root/112/lesson2
[root@VM-4-3-centos lesson2]# pwd
/root/112/lesson2
[root@VM-4-3-centos lesson2]# cd -
/
[root@VM-4-3-centos /]# pwd
/
[root@VM-4-3-centos /]# cd -
/root/112/lesson2
  1. cd ~
c 复制代码
[root@VM-4-3-centos lesson2]# pwd
/root/112/lesson2
[root@VM-4-3-centos lesson2]# cd ~
[root@VM-4-3-centos ~]# pwd
/root
[root@VM-4-3-centos ~]# cd /
[root@VM-4-3-centos /]# cd ~
[root@VM-4-3-centos ~]# pwd
/root
[root@VM-4-3-centos ~]# cd /home
[root@VM-4-3-centos home]# pwd
/home
[root@VM-4-3-centos home]# cd ~
[root@VM-4-3-centos ~]# pwd
/root

2.4 touch 指令

语法: touch [选项]... 文件...
功能: touch命令参数可更改文档或目录的日期时间(后面再讲),包括存取时间和更改时间,或者新建一个不存在的文件。

c 复制代码
[root@VM-4-3-centos ~]# cd /root/112/lesson2
[root@VM-4-3-centos lesson2]# pwd
/root/112/lesson2
[root@VM-4-3-centos lesson2]# ll
total 12
drwxr-xr-x 2 root root 4096 Jan 29 15:35 dir
drwxr-xr-x 2 root root 4096 Jan 29 15:36 dir2
drwxr-xr-x 2 root root 4096 Jan 29 22:22 empty
-rw-r--r-- 1 root root    0 Jan 29 16:09 test.c
[root@VM-4-3-centos lesson2]# touch ./hello.txt
[root@VM-4-3-centos lesson2]# ll
total 12
drwxr-xr-x 2 root root 4096 Jan 29 15:35 dir
drwxr-xr-x 2 root root 4096 Jan 29 15:36 dir2
drwxr-xr-x 2 root root 4096 Jan 29 22:22 empty
-rw-r--r-- 1 root root    0 Jan 30 16:30 hello.txt
-rw-r--r-- 1 root root    0 Jan 29 16:09 test.c
[root@VM-4-3-centos lesson2]# touch ../helle.txt
[root@VM-4-3-centos lesson2]# ll
total 12
drwxr-xr-x 2 root root 4096 Jan 29 15:35 dir
drwxr-xr-x 2 root root 4096 Jan 29 15:36 dir2
drwxr-xr-x 2 root root 4096 Jan 29 22:22 empty
-rw-r--r-- 1 root root    0 Jan 30 16:30 hello.txt
-rw-r--r-- 1 root root    0 Jan 29 16:09 test.c
[root@VM-4-3-centos lesson2]# ls ..
helle.txt  hello.txt  lesson2

2.5 mkdir 指令

语法: mkdir [选项] dirname...
功能: 在当前目录下创建一个名为"dirname"的目录
常用选项:

  • -p :可以是一个路径名称,此时若路径中的某些目录尚不存在,加上此选项后,系统将自动建立好那些尚不存在的目录,即一次可以建立多个目录
c 复制代码
[root@VM-4-3-centos lesson2]# mkdir newdir
[root@VM-4-3-centos lesson2]# ls
dir  dir2  empty  hello.txt  newdir  test.c
[root@VM-4-3-centos lesson2]# ls -l
total 16
drwxr-xr-x 2 root root 4096 Jan 29 15:35 dir
drwxr-xr-x 2 root root 4096 Jan 29 15:36 dir2
drwxr-xr-x 2 root root 4096 Jan 29 22:22 empty
-rw-r--r-- 1 root root    0 Jan 30 16:30 hello.txt
drwxr-xr-x 2 root root 4096 Jan 30 16:38 newdir
-rw-r--r-- 1 root root    0 Jan 29 16:09 test.c
[root@VM-4-3-centos lesson2]# mkdir ../newdir
[root@VM-4-3-centos lesson2]# ls -l ../
total 8
-rw-r--r-- 1 root root    0 Jan 30 16:31 helle.txt
-rw-r--r-- 1 root root    0 Jan 29 21:01 hello.txt
drwxr-xr-x 7 root root 4096 Jan 30 16:38 lesson2
drwxr-xr-x 2 root root 4096 Jan 30 16:39 newdir
[root@VM-4-3-centos lesson2]# mkdir -p ./dir1/dir2/dir3/dir4
[root@VM-4-3-centos lesson2]# ll
total 20
drwxr-xr-x 2 root root 4096 Jan 29 15:35 dir
drwxr-xr-x 3 root root 4096 Jan 30 16:42 dir1
drwxr-xr-x 2 root root 4096 Jan 29 15:36 dir2
drwxr-xr-x 2 root root 4096 Jan 29 22:22 empty
-rw-r--r-- 1 root root    0 Jan 30 16:30 hello.txt
drwxr-xr-x 2 root root 4096 Jan 30 16:38 newdir
-rw-r--r-- 1 root root    0 Jan 29 16:09 test.c
[root@VM-4-3-centos lesson2]# tree dir1
dir1
`-- dir2
    `-- dir3
        `-- dir4

3 directories, 0 files
[root@VM-4-3-centos lesson2]# ls dir1
dir2
[root@VM-4-3-centos lesson2]# ls dir1/dir2
dir3
[root@VM-4-3-centos lesson2]# ls dir1/dir2/dir3
dir4
[root@VM-4-3-centos lesson2]# ls dir1/dir2/dir3/dir4
[root@VM-4-3-centos lesson2]# mkdir -p ./dir1/dir2/dir3/dir4
[root@VM-4-3-centos lesson2]# ls dir1/dir2/dir3/dir4
[root@VM-4-3-centos lesson2]# mkdir -p ./dir1/dir2/dir3/dir4/test
[root@VM-4-3-centos lesson2]# tree dir1
dir1
`-- dir2
    `-- dir3
        `-- dir4
            `-- test

4 directories, 0 files

2.6 rmdir 指令 && rm 指令

rmdir是一个与mkdir相对应的命令,mkdir是建立目录,而rmdir是删除命令
语法: rmdir [-p] [dirName]
适用对象: 具有当前目录操作权限的所有使用者
功能: 删除空目录
常用选项:

  • -p :当子目录被删除后如果父目录也变成空目录的话,就连带父目录一起删除
c 复制代码
[root@VM-4-3-centos lesson2]# ll
total 20
drwxr-xr-x 2 root root 4096 Jan 29 15:35 dir
drwxr-xr-x 3 root root 4096 Jan 30 16:42 dir1
drwxr-xr-x 2 root root 4096 Jan 29 15:36 dir2
drwxr-xr-x 2 root root 4096 Jan 29 22:22 empty
-rw-r--r-- 1 root root    0 Jan 30 16:30 hello.txt
drwxr-xr-x 2 root root 4096 Jan 30 16:38 newdir
-rw-r--r-- 1 root root    0 Jan 29 16:09 test.c
[root@VM-4-3-centos lesson2]# rmdir test.c
rmdir: failed to remove 'test.c': Not a directory
[root@VM-4-3-centos lesson2]# tree .
.
|-- dir
|-- dir1
|   `-- dir2
|       `-- dir3
|           `-- dir4
|               `-- test
|-- dir2
|-- empty
|-- hello.txt
|-- newdir
`-- test.c

9 directories, 2 files
[root@VM-4-3-centos lesson2]# rmdir dir1
rmdir: failed to remove 'dir1': Directory not empty
[root@VM-4-3-centos lesson2]# rmdir dir
[root@VM-4-3-centos lesson2]# rmdir dir2
[root@VM-4-3-centos lesson2]# ll
total 12
drwxr-xr-x 3 root root 4096 Jan 30 16:42 dir1
drwxr-xr-x 2 root root 4096 Jan 29 22:22 empty
-rw-r--r-- 1 root root    0 Jan 30 16:30 hello.txt
drwxr-xr-x 2 root root 4096 Jan 30 16:38 newdir
-rw-r--r-- 1 root root    0 Jan 29 16:09 test.c
[root@VM-4-3-centos lesson2]# tree .
.
|-- dir1
|   `-- dir2
|       `-- dir3
|           `-- dir4
|               `-- test
|-- empty
|-- hello.txt
|-- newdir
`-- test.c

7 directories, 2 files

rm 命令可以同时删除文件或目录
语法: rm [-f-i-r-v] [dirName/dir]
适用对象: 所有使用者
功能: 删除文件或目录
常用选项:

  • -r :删除目录及其下所有文件
c 复制代码
[root@VM-4-3-centos lesson2]# tree .
.
|-- dir1
|   `-- dir2
|       `-- dir3
|           `-- dir4
|               `-- test
|-- empty
|-- hello.txt
|-- newdir
`-- test.c

7 directories, 2 files
[root@VM-4-3-centos lesson2]# rm dir1
rm: cannot remove 'dir1': Is a directory
[root@VM-4-3-centos lesson2]# rm hello.txt
rm: remove regular empty file 'hello.txt'? y
[root@VM-4-3-centos lesson2]# ll
total 12
drwxr-xr-x 3 root root 4096 Jan 30 16:42 dir1
drwxr-xr-x 2 root root 4096 Jan 29 22:22 empty
drwxr-xr-x 2 root root 4096 Jan 30 16:38 newdir
-rw-r--r-- 1 root root    0 Jan 29 16:09 test.c
[root@VM-4-3-centos lesson2]# rm dir1
rm: cannot remove 'dir1': Is a directory
[root@VM-4-3-centos lesson2]# rm -r dir1
rm: descend into directory 'dir1'? y
rm: descend into directory 'dir1/dir2'? y
rm: descend into directory 'dir1/dir2/dir3'? y
rm: descend into directory 'dir1/dir2/dir3/dir4'? y
rm: remove directory 'dir1/dir2/dir3/dir4/test'? y
rm: remove directory 'dir1/dir2/dir3/dir4'? y
rm: remove directory 'dir1/dir2/dir3'? y
rm: remove directory 'dir1/dir2'? y
rm: remove directory 'dir1'? y
[root@VM-4-3-centos lesson2]# ll
total 8
drwxr-xr-x 2 root root 4096 Jan 29 22:22 empty
drwxr-xr-x 2 root root 4096 Jan 30 16:38 newdir
-rw-r--r-- 1 root root    0 Jan 29 16:09 test.c

注: 这里因为我们是root用户,所以删除的时候系统会格外小心,它会问我们是不是要删除

如果不想让系统问,就要使用 -f :

c 复制代码
[root@VM-4-3-centos lesson2]# ll
total 8
drwxr-xr-x 2 root root 4096 Jan 29 22:22 empty
drwxr-xr-x 2 root root 4096 Jan 30 16:38 newdir
-rw-r--r-- 1 root root    0 Jan 29 16:09 test.c
[root@VM-4-3-centos lesson2]# mkdir -p dir1/dir2/dir3/dir4
[root@VM-4-3-centos lesson2]# tree .
.
|-- dir1
|   `-- dir2
|       `-- dir3
|           `-- dir4
|-- empty
|-- newdir
`-- test.c

6 directories, 1 file
[root@VM-4-3-centos lesson2]# rm dir1
rm: cannot remove 'dir1': Is a directory
[root@VM-4-3-centos lesson2]# rm -rf dir1
[root@VM-4-3-centos lesson2]# ll
total 8
drwxr-xr-x 2 root root 4096 Jan 29 22:22 empty
drwxr-xr-x 2 root root 4096 Jan 30 16:38 newdir
-rw-r--r-- 1 root root    0 Jan 29 16:09 test.c
c 复制代码
[root@VM-4-3-centos lesson2]# ll
total 8
drwxr-xr-x 2 root root 4096 Jan 29 22:22 empty
drwxr-xr-x 2 root root 4096 Jan 30 16:38 newdir
-rw-r--r-- 1 root root    0 Jan 29 16:09 test.c
[root@VM-4-3-centos lesson2]# pwd
/root/112/lesson2
[root@VM-4-3-centos lesson2]# touch file1 file2 file3 file4 file5
[root@VM-4-3-centos lesson2]# ll
total 8
drwxr-xr-x 2 root root 4096 Jan 29 22:22 empty
-rw-r--r-- 1 root root    0 Jan 30 18:33 file1
-rw-r--r-- 1 root root    0 Jan 30 18:33 file2
-rw-r--r-- 1 root root    0 Jan 30 18:33 file3
-rw-r--r-- 1 root root    0 Jan 30 18:33 file4
-rw-r--r-- 1 root root    0 Jan 30 18:33 file5
drwxr-xr-x 2 root root 4096 Jan 30 16:38 newdir
-rw-r--r-- 1 root root    0 Jan 29 16:09 test.c
[root@VM-4-3-centos lesson2]# ls file*
file1  file2  file3  file4  file5
[root@VM-4-3-centos lesson2]# rm file*
rm: remove regular empty file 'file1'? n
rm: remove regular empty file 'file2'? n
rm: remove regular empty file 'file3'? n
rm: remove regular empty file 'file4'? n
rm: remove regular empty file 'file5'? n
[root@VM-4-3-centos lesson2]# ll
total 8
drwxr-xr-x 2 root root 4096 Jan 29 22:22 empty
-rw-r--r-- 1 root root    0 Jan 30 18:33 file1
-rw-r--r-- 1 root root    0 Jan 30 18:33 file2
-rw-r--r-- 1 root root    0 Jan 30 18:33 file3
-rw-r--r-- 1 root root    0 Jan 30 18:33 file4
-rw-r--r-- 1 root root    0 Jan 30 18:33 file5
drwxr-xr-x 2 root root 4096 Jan 30 16:38 newdir
-rw-r--r-- 1 root root    0 Jan 29 16:09 test.c
[root@VM-4-3-centos lesson2]# rm -f file*
[root@VM-4-3-centos lesson2]# ll
total 8
drwxr-xr-x 2 root root 4096 Jan 29 22:22 empty
drwxr-xr-x 2 root root 4096 Jan 30 16:38 newdir
-rw-r--r-- 1 root root    0 Jan 29 16:09 test.c
[root@VM-4-3-centos lesson2]# touch file1 file2 file3 file4 file5 file
[root@VM-4-3-centos lesson2]# ls
empty  file  file1  file2  file3  file4  file5  newdir  test.c
[root@VM-4-3-centos lesson2]# ls file*
file  file1  file2  file3  file4  file5
[root@VM-4-3-centos lesson2]# ll
total 8
drwxr-xr-x 2 root root 4096 Jan 29 22:22 empty
-rw-r--r-- 1 root root    0 Jan 30 18:38 file
-rw-r--r-- 1 root root    0 Jan 30 18:38 file1
-rw-r--r-- 1 root root    0 Jan 30 18:38 file2
-rw-r--r-- 1 root root    0 Jan 30 18:38 file3
-rw-r--r-- 1 root root    0 Jan 30 18:38 file4
-rw-r--r-- 1 root root    0 Jan 30 18:38 file5
drwxr-xr-x 2 root root 4096 Jan 30 16:38 newdir
-rw-r--r-- 1 root root    0 Jan 29 16:09 test.c
[root@VM-4-3-centos lesson2]# rm -rf *
[root@VM-4-3-centos lesson2]# ll
total 0

注: *代表通配符,表示任意内容


补充:

2.7 man 指令

Linux的命令有很多参数,我们不可能全记住,可以通过查看联机手册获取帮助
语法: man [选项] 命令

c 复制代码
[root@VM-4-3-centos lesson2]# man ls
[root@VM-4-3-centos lesson2]# man pwd
[root@VM-4-3-centos lesson2]# man 3 printf
[root@VM-4-3-centos lesson2]# man 3 strstr
[root@VM-4-3-centos lesson2]# man 3 sin
[root@VM-4-3-centos lesson2]# man man
[root@VM-4-3-centos lesson2]# man 1 ls
[root@VM-4-3-centos lesson2]# man 1 pwd
[root@VM-4-3-centos lesson2]# man 3 printf
[root@VM-4-3-centos lesson2]# man 2 fork
[root@VM-4-3-centos lesson2]# man ls
[root@VM-4-3-centos lesson2]# man printf
[root@VM-4-3-centos lesson2]# printf "%d-%f-%c-%s\n" 10 3.14 'c' "helloworld"
10-3.140000-c-helloworld
[root@VM-4-3-centos lesson2]# man printf
[root@VM-4-3-centos lesson2]# man 3 printf
[root@VM-4-3-centos lesson2]# man strstr


注:

  • 我们一般只用1~3号手册
  • man 命令 默认从1号手册开始查,查到就停下来

2.8 cp 指令

先补充一个知识点:

nano 相当于Windows系统里的记事本

c 复制代码
[root@VM-4-3-centos lesson2]# touch test.c
[root@VM-4-3-centos lesson2]# ll
total 0
-rw-r--r-- 1 root root 0 Jan 30 20:13 test.c
[root@VM-4-3-centos lesson2]# nano test.c
[root@VM-4-3-centos lesson2]# ll
total 4
-rw-r--r-- 1 root root 76 Jan 30 20:16 test.c
[root@VM-4-3-centos lesson2]# cat test.c
#include <stdio.h>

int main()
{
    printf("hello world, hello bite\n");
}
[root@VM-4-3-centos lesson2]# gcc test.c -o mytest
[root@VM-4-3-centos lesson2]# ll
total 16
-rwxr-xr-x 1 root root 8360 Jan 30 20:20 mytest
-rw-r--r-- 1 root root   76 Jan 30 20:16 test.c
[root@VM-4-3-centos lesson2]# ./mytest
hello world, hello bite
[root@VM-4-3-centos lesson2]# mytest
-bash: mytest: command not found
[root@VM-4-3-centos lesson2]# pwd
/root/112/lesson2
[root@VM-4-3-centos lesson2]# /root/112/lesson2/mytest
hello world, hello bite
[root@VM-4-3-centos lesson2]# ./mytest
hello world, hello bite

语法: cp [选项] 源文件或目录 目标文件或目录
功能: 复制文件或目录
说明:

  • cp指令用于复制文件或目录
  • 如同时指定两个以上的文件或目录,且最后的目的地是一个已经存在的目录,则它会把前面指定的所有文件或目录复制到此目录中

常用选项:

  • -f : 强行复制文件或目录, 不论目的文件或目录是否已经存在
  • -r : 递归处理,将指定目录下的文件与子目录一并处理;若源文件或目录的形态,不属于目录或符号链接,则一律视为普通文件处理
c 复制代码
[root@VM-4-3-centos lesson2]# ll
total 16
-rwxr-xr-x 1 root root 8360 Jan 30 20:20 mytest
-rw-r--r-- 1 root root   76 Jan 30 20:16 test.c
[root@VM-4-3-centos lesson2]# cp test.c hello.c
[root@VM-4-3-centos lesson2]# ls -l
total 20
-rw-r--r-- 1 root root   76 Jan 30 20:29 hello.c
-rwxr-xr-x 1 root root 8360 Jan 30 20:20 mytest
-rw-r--r-- 1 root root   76 Jan 30 20:16 test.c
[root@VM-4-3-centos lesson2]# ll
total 20
-rw-r--r-- 1 root root   76 Jan 30 20:29 hello.c
-rwxr-xr-x 1 root root 8360 Jan 30 20:20 mytest
-rw-r--r-- 1 root root   76 Jan 30 20:16 test.c
[root@VM-4-3-centos lesson2]# cat hello.c
#include <stdio.h>

int main()
{
    printf("hello world, hello bite\n");
}
[root@VM-4-3-centos lesson2]# cp mytest ../
[root@VM-4-3-centos lesson2]# ll ../
total 20
-rw-r--r-- 1 root root    0 Jan 30 16:31 helle.txt
-rw-r--r-- 1 root root    0 Jan 29 21:01 hello.txt
drwxr-xr-x 3 root root 4096 Jan 30 20:29 lesson2
-rwxr-xr-x 1 root root 8360 Jan 30 20:31 mytest
drwxr-xr-x 2 root root 4096 Jan 30 16:39 newdir
[root@VM-4-3-centos lesson2]# ./mytest
hello world, hello bite
[root@VM-4-3-centos lesson2]# ../mytest
hello world, hello bite
[root@VM-4-3-centos lesson2]# pwd
/root/112/lesson2
[root@VM-4-3-centos lesson2]# cd ..
[root@VM-4-3-centos 112]# ll
total 20
-rw-r--r-- 1 root root    0 Jan 30 16:31 helle.txt
-rw-r--r-- 1 root root    0 Jan 29 21:01 hello.txt
drwxr-xr-x 3 root root 4096 Jan 30 20:29 lesson2
-rwxr-xr-x 1 root root 8360 Jan 30 20:31 mytest
drwxr-xr-x 2 root root 4096 Jan 30 16:39 newdir
[root@VM-4-3-centos 112]# tree .
.
|-- helle.txt
|-- hello.txt
|-- lesson2
|   |-- hello.c
|   |-- mytest
|   `-- test.c
|-- mytest
`-- newdir

2 directories, 6 files
[root@VM-4-3-centos 112]# mkdir empty
[root@VM-4-3-centos 112]# ll
total 24
drwxr-xr-x 2 root root 4096 Jan 30 20:34 empty
-rw-r--r-- 1 root root    0 Jan 30 16:31 helle.txt
-rw-r--r-- 1 root root    0 Jan 29 21:01 hello.txt
drwxr-xr-x 3 root root 4096 Jan 30 20:29 lesson2
-rwxr-xr-x 1 root root 8360 Jan 30 20:31 mytest
drwxr-xr-x 2 root root 4096 Jan 30 16:39 newdir
[root@VM-4-3-centos 112]# tree .
.
|-- empty
|-- helle.txt
|-- hello.txt
|-- lesson2
|   |-- hello.c
|   |-- mytest
|   `-- test.c
|-- mytest
`-- newdir

3 directories, 6 files
[root@VM-4-3-centos 112]# cp lesson2 empty
cp: omitting directory 'lesson2'
[root@VM-4-3-centos 112]# ll
total 24
drwxr-xr-x 2 root root 4096 Jan 30 20:34 empty
-rw-r--r-- 1 root root    0 Jan 30 16:31 helle.txt
-rw-r--r-- 1 root root    0 Jan 29 21:01 hello.txt
drwxr-xr-x 3 root root 4096 Jan 30 20:29 lesson2
-rwxr-xr-x 1 root root 8360 Jan 30 20:31 mytest
drwxr-xr-x 2 root root 4096 Jan 30 16:39 newdir
[root@VM-4-3-centos 112]# tree .
.
|-- empty
|-- helle.txt
|-- hello.txt
|-- lesson2
|   |-- hello.c
|   |-- mytest
|   `-- test.c
|-- mytest
`-- newdir

3 directories, 6 files
[root@VM-4-3-centos 112]# cp -r lesson2 empty
[root@VM-4-3-centos 112]# tree .
.
|-- empty
|   `-- lesson2
|       |-- hello.c
|       |-- mytest
|       `-- test.c
|-- helle.txt
|-- hello.txt
|-- lesson2
|   |-- hello.c
|   |-- mytest
|   `-- test.c
|-- mytest
`-- newdir

4 directories, 9 files
[root@VM-4-3-centos 112]# cp -r lesson2 empty
cp: overwrite 'empty/lesson2/mytest'? y
cp: overwrite 'empty/lesson2/test.c'? y
cp: overwrite 'empty/lesson2/.hello'? y
cp: overwrite 'empty/lesson2/hello.c'? y
[root@VM-4-3-centos 112]# cp -rf lesson2 empty
cp: overwrite 'empty/lesson2/mytest'? y
cp: overwrite 'empty/lesson2/test.c'? y
cp: overwrite 'empty/lesson2/.hello'? y
cp: overwrite 'empty/lesson2/hello.c'? y

注: 因为这里是 root 用户,为了安全,所以 -f 系统还是会让我们确认,在普通用户下就不会让我们确认了。


补充:

任何目录下不允许存在同名文件!

c 复制代码
[root@VM-4-3-centos 112]# pwd
/root/112
[root@VM-4-3-centos 112]# ll
total 24
drwxr-xr-x 3 root root 4096 Jan 30 20:37 empty
-rw-r--r-- 1 root root    0 Jan 30 16:31 helle.txt
-rw-r--r-- 1 root root    0 Jan 29 21:01 hello.txt
drwxr-xr-x 3 root root 4096 Jan 30 20:29 lesson2
-rwxr-xr-x 1 root root 8360 Jan 30 20:31 mytest
drwxr-xr-x 2 root root 4096 Jan 30 16:39 newdir
[root@VM-4-3-centos 112]# touch hello.txt
[root@VM-4-3-centos 112]# ll
total 24
drwxr-xr-x 3 root root 4096 Jan 30 20:37 empty
-rw-r--r-- 1 root root    0 Jan 30 16:31 helle.txt
-rw-r--r-- 1 root root    0 Jan 30 20:58 hello.txt
drwxr-xr-x 3 root root 4096 Jan 30 20:29 lesson2
-rwxr-xr-x 1 root root 8360 Jan 30 20:31 mytest
drwxr-xr-x 2 root root 4096 Jan 30 16:39 newdir
[root@VM-4-3-centos 112]# mkdir empty
mkdir: cannot create directory 'empty': File exists
[root@VM-4-3-centos 112]# touch empty
[root@VM-4-3-centos 112]# ll
total 24
drwxr-xr-x 3 root root 4096 Jan 30 20:59 empty
-rw-r--r-- 1 root root    0 Jan 30 16:31 helle.txt
-rw-r--r-- 1 root root    0 Jan 30 20:58 hello.txt
drwxr-xr-x 3 root root 4096 Jan 30 20:29 lesson2
-rwxr-xr-x 1 root root 8360 Jan 30 20:31 mytest
drwxr-xr-x 2 root root 4096 Jan 30 16:39 newdir
相关推荐
a小胡哦9 分钟前
Windows、Mac、Linux,到底该怎么选?
linux·windows·macos·操作系统
_extraordinary_19 分钟前
Linux权限(一)
android·linux·excel
易安杰26 分钟前
ElasticSearch+Kibana通过Docker部署到Linux服务器中
linux·elasticsearch·搜索引擎·全文检索·中文分词
web2u31 分钟前
Docker入门及基本概念
java·运维·服务器·spring·docker·容器
人生!?1 小时前
给小米/红米手机root(工具基本为官方工具)——KernelSU篇
android·linux·智能手机
元气满满的热码式1 小时前
Docker实战-使用docker compose搭建博客
运维·docker·容器
Anna_Tong2 小时前
阿里云如何协助解决操作系统兼容性问题
linux·服务器·ubuntu·阿里云·centos·云计算·系统迁移
HaoHao_0102 小时前
如何将MySQL数据库迁移至阿里云
服务器·数据库·阿里云·云计算·云服务器·迁移
不良人天码星2 小时前
Linux的基础指令和环境部署,项目部署实战(下)
linux·运维·服务器
火一线3 小时前
【ASP .NET Core】ASP .NET Core介绍
服务器·游戏·.netcore