前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Linux必会基础命令

Linux必会基础命令

作者头像
废柴阿尤
发布2023-12-18 16:23:03
1820
发布2023-12-18 16:23:03
举报
文章被收录于专栏:Typecho_ayou

1. pwd 显示当前所在的路径 print working directory

代码语言:javascript
复制
语法结构:
[root@luckly ~]# pwd   # 回车
/root

2. cd 切换目录 change directory

代码语言:javascript
复制
cd           回到家目录
    cd ~         回到家目录
    cd /root         回到家目录
    cd -         返回到上一次所在的路径
    cd ..         回到上一级目录
    cd .          表示当前的目录
语法结构:
相对路径进入目录:
[root@luckly ~]# cd /
[root@luckly /]# cd etc
[root@luckly etc]# cd sysconfig/
[root@luckly sysconfig]# cd network-scripts/
[root@luckly network-scripts]# pwd
/etc/sysconfig/network-scripts
[root@luckly network-scripts]#
使用绝对路径进入目录:
[root@luckly network-scripts]# cd  # cd直接回到皇宫
[root@luckly ~]#
[root@luckly ~]# cd /etc/sysconfig/network-scripts/
[root@luckly network-scripts]# pwd
/etc/sysconfig/network-scripts
[root@luckly ~]# cd -     # 回到上一次所在的路径
/etc/sysconfig/network-scripts
[root@luckly network-scripts]# pwd
/etc/sysconfig/network-scripts
# cd ~ 回到家目录
[root@luckly network-scripts]# cd ~
[root@luckly ~]#
# cd /root 回到家目录
[root@luckly network-scripts]# cd /root/
[root@luckly ~]# pwd
/root
# 回到上一级目录
[root@luckly network-scripts]# cd ..
[root@luckly sysconfig]# pwd
/etc/sysconfig
# 回到上二级目录
[root@luckly sysconfig]# cd ../..
[root@luckly /]# pwd
/
# 点表示当前所在的目录
[root@luckly ~]# cd .
[root@luckly ~]#

3.ls 查看文件或目录

代码语言:javascript
复制
参数选项:
         -l 显示文件或目录的详细信息
         -d 只查看目录本身的详细信息
语法结构:
# 查看1.txt是否存在
[root@luckly ~]# ls 1.txt
1.txt
[root@luckly ~]# ls 2.txt
ls: cannot access 2.txt: No such file or directory
# 显示当前目录的所有的文件
[root@luckly ~]# ls
1.txt  hosts  luckly
# 指定查看/tmp目录下有哪些文件
[root@luckly ~]# ls /tmp/
ks-script-zVRXqX            vmware-root_848-2697663887
vmware-root_800-2999657415  yum.log
# 查看/目录和/tmp目录下有哪些文件
[root@luckly ~]# ls / /tmp/
/:
bin   dev  home  lib64  mnt  proc  run   srv  tmp  var
boot  etc  lib   media  opt  root  sbin  sys  usr
/tmp/:
ks-script-zVRXqX            vmware-root_848-2697663887
vmware-root_800-2999657415  yum.log
# ls -l 查看详细信息
[root@luckly ~]# ls -l
total 4
-rw-r--r--. 1 root root   0 Jul  3 11:50 1.txt
-rw-r--r--. 1 root root 158 Jul  3 16:33 hosts
drwxr-xr-x. 2 root root   6 Jul  3 16:41 luckly
# 执行ll命令相当于执行 ls -l命令
[root@luckly ~]# ll
total 4
-rw-r--r--. 1 root root   0 Jul  3 11:50 1.txt
-rw-r--r--. 1 root root 158 Jul  3 16:33 hosts
drwxr-xr-x. 2 root root   6 Jul  3 16:41 luckly
[root@luckly ~]# ll /tmp/
total 8
-rwx------. 1 root root 836 Jun 29 17:51 ks-script-zVRXqX
drwx------. 2 root root   6 Jul  3 10:45 vmware-root_800-2999657415
drwx------. 2 root root   6 Jul  3 10:44 vmware-root_846-2697139606
drwx------. 2 root root   6 Jun 30 09:12 vmware-root_848-2697663887
-rw-------. 1 root root   0 Jun 29 17:44 yum.log
[root@luckly ~]# ll 1.txt
-rw-r--r--. 1 root root 0 Jul  3 11:50 1.txt
[root@luckly ~]# ll 1.txt hosts
-rw-r--r--. 1 root root   0 Jul  3 11:50 1.txt
-rw-r--r--. 1 root root 158 Jul  3 16:33 hosts

4.touch 创建普通文件 摸(如果文件存在则修改文件的时间)

代码语言:javascript
复制
时间和时间戳的区别
语法结构:
            相对路径
            touch  file1
            touch  file1 file2
            绝对路径
            touch /tmp/file1
            touch /tmp/file2 /tmp/file3
[root@luckly ~]# ll
total 0
案例1:创建一个文件
代码语言:javascript
复制
[root@luckly ~]# touch 1.txt
[root@luckly ~]# ll
total 0
-rw-r--r--. 1 root root 0 Jul  3 17:06 1.txt
案例2: 创建多个文件
代码语言:javascript
复制
[root@luckly ~]# touch 2.txt 3.txt
[root@luckly ~]# ll
total 0
-rw-r--r--. 1 root root 0 Jul  3 17:06 1.txt
-rw-r--r--. 1 root root 0 Jul  3 17:06 2.txt
-rw-r--r--. 1 root root 0 Jul  3 17:06 3.txt
案例3: 在/opt目录下创建test.txt
代码语言:javascript
复制
相对路径:
[root@luckly ~]# cd /opt/
[root@luckly opt]# ll
total 0
[root@luckly opt]# touch test.txt
[root@luckly opt]# ll
total 0
-rw-r--r--. 1 root root 0 Jul  3 17:08 test.txt
绝对路径:
[root@luckly ~]# touch /opt/1.txt
[root@luckly ~]# ll /opt/
total 0
-rw-r--r--. 1 root root 0 Jul  3 17:09 1.txt
-rw-r--r--. 1 root root 0 Jul  3 17:08 test.txt
案例4:在/opt目录下创建a.txt和b.txt
代码语言:javascript
复制
相对路径:
[root@luckly ~]# cd /opt/
[root@luckly opt]# ll
total 0
-rw-r--r--. 1 root root 0 Jul  3 17:09 1.txt
-rw-r--r--. 1 root root 0 Jul  3 17:08 test.txt
[root@luckly opt]# touch a.txt b.txt
[root@luckly opt]# ll
total 0
-rw-r--r--. 1 root root 0 Jul  3 17:09 1.txt
-rw-r--r--. 1 root root 0 Jul  3 17:10 a.txt
-rw-r--r--. 1 root root 0 Jul  3 17:10 b.txt
绝对路径:
错误的创建方式:
[root@luckly ~]# ll /opt/
total 0
[root@luckly ~]# touch /opt/a.txt b.txt
[root@luckly ~]# ll
total 0
-rw-r--r--. 1 root root 0 Jul  3 17:06 1.txt
-rw-r--r--. 1 root root 0 Jul  3 17:06 2.txt
-rw-r--r--. 1 root root 0 Jul  3 17:06 3.txt
-rw-r--r--. 1 root root 0 Jul  3 17:11 b.txt
[root@luckly ~]# ll /opt/
total 0
-rw-r--r--. 1 root root 0 Jul  3 17:11 a.txt
--------------------我是一条快乐的分割线--------------------
正确的创建方式:
[root@luckly ~]# touch /opt/a.txt /opt/b.txt
[root@luckly ~]# ll /opt/
total 0
-rw-r--r--. 1 root root 0 Jul  3 17:12 a.txt
-rw-r--r--. 1 root root 0 Jul  3 17:12 b.txt
--------------------我是一条快乐的分割线--------------------
扩展: 序列 /opt下创建1.txt 2.txt 3.txt
[root@luckly ~]# touch /opt/{1..3}.txt
[root@luckly ~]# ll /opt/
total 0
-rw-r--r--. 1 root root 0 Jul  3 17:13 1.txt
-rw-r--r--. 1 root root 0 Jul  3 17:13 2.txt
-rw-r--r--. 1 root root 0 Jul  3 17:13 3.txt
案例5: 在/opt下和/tmp下同时创建luckly.txt
代码语言:javascript
复制
[root@luckly ~]# touch /opt/luckly.txt /tmp/luckly.txt
[root@luckly ~]# ll /opt/
total 0
-rw-r--r--. 1 root root 0 Jul  3 17:15 luckly.txt
[root@luckly ~]# ll /tmp/
total 0
-rw-r--r--. 1 root root 0 Jul  3 17:15 luckly.txt
案例6: 在上一级创建一个test.txt
代码语言:javascript
复制
[root@luckly tmp]# pwd
/tmp
[root@luckly tmp]# touch /test.txt
[root@luckly tmp]# cd /
[root@luckly /]# touch test.txt
使用..上一级方式
[root@luckly tmp]# touch ../test.txt

5.mkdir 创建目录

代码语言:javascript
复制
语法格式:
    mkdir 目录名称                  # 创建单个目录
    mkdir test1 test2            # 创建多个目录
    mkdir /opt/test            # 指定目录下创建目录
    mkdir /opt/test1 /opt/test2    # 在相同目录下创建多个文件
    mkdir /opt/test1 /tmp/test2    # 在不同目录下创建目录
    #在/opt/test/hehe下创建hehe目录
    mkdir /opt/test/hehe        # test目录必须存在
案例1:在当前目录下创建luckly目录
代码语言:javascript
复制
[root@luckly ~]# mkdir luckly
[root@luckly ~]# ll
total 0
drwxr-xr-x. 2 root root 6 Jul  4 10:03 luckly
[root@luckly ~]# mkdir luckly
mkdir: cannot create directory ‘luckly’: File exists
案例2:在当前创建多个目录
代码语言:javascript
复制
[root@luckly ~]# mkdir test1 test2 test3
[root@luckly ~]# ll
total 0
drwxr-xr-x. 2 root root 6 Jul  4 10:03 luckly
drwxr-xr-x. 2 root root 6 Jul  4 10:04 test1
drwxr-xr-x. 2 root root 6 Jul  4 10:04 test2
drwxr-xr-x. 2 root root 6 Jul  4 10:04 test3
案例3:在指定目录下创建多个目录
代码语言:javascript
复制
# 在/opt目录下创建luckly和test目录
[root@luckly ~]# mkdir /opt/luckly /opt/test
[root@luckly ~]# ll /opt/
total 0
drwxr-xr-x. 2 root root 6 Jul  4 10:13 luckly
drwxr-xr-x. 2 root root 6 Jul  4 10:13 test
案例4:在不同目录下创建不同目录
代码语言:javascript
复制
# 在/opt目录和/tmp目录下创建luckly2
[root@luckly ~]# mkdir /opt/luckly2 /tmp/luckly2
[root@luckly ~]# ll /opt/
total 0
drwxr-xr-x. 2 root root 6 Jul  4 10:15 luckly2
[root@luckly ~]# ll /tmp/
total 0
drwxr-xr-x. 2 root root 6 Jul  4 10:15 luckly2
案例5:递归创建多级目录
代码语言:javascript
复制
# 在当前目录下创建test1/test2/test3/luckly
    -p  #递归创建目录,如果目录存在则不提示文件已存在,如果目录不存在则创建
[root@luckly ~]# mkdir -p test1/test2/test3/luckly
[root@luckly ~]# ll
total 0
drwxr-xr-x. 3 root root 19 Jul  4 10:16 test1
[root@luckly ~]# ll test1/
total 0
drwxr-xr-x. 3 root root 19 Jul  4 10:16 test2
[root@luckly ~]# ll test1/test2/
total 0
drwxr-xr-x. 3 root root 20 Jul  4 10:16 test3
[root@luckly ~]# ll test1/test2/test3/
total 0
drwxr-xr-x. 2 root root 6 Jul  4 10:16 luckly

6.cat 查看文件内容

代码语言:javascript
复制
语法结构:
  命令参数:
      -n            # 显示行号
cat 文件名                    # 相对路径
cat /etc/hosts                # 绝对路径
cat file1 file2             # 查看多个文件的内容
案例1:查看/etc/hosts文件内容
代码语言:javascript
复制
[root@luckly ~]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
案例2:查看多个文件内容
代码语言:javascript
复制
echo 11 > test.txt                # 将11字符串输入到test.txt中
echo 22 > luckly.txt            # 将22字符串输入到oluckly.txt中
[root@luckly ~]# cat test.txt 
11
[root@luckly ~]# cat luckly.txt 
22
[root@luckly ~]# cat test.txt luckly.txt 
11
22
案例3:-n 显示行号
代码语言:javascript
复制
[root@luckly ~]# cat -n /etc/hosts
     1    127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
     2    ::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
案例4: cat 将内容写道文件中(扩展)
代码语言:javascript
复制
# 将内容写入到文件中
[root@luckly ~]# cat>1.txt< 1
> 2
> hehe
> EOF
[root@luckly ~]# cat 1.txt 
1
2
hehe
[root@luckly ~]# ll
total 12
-rw-r--r--. 1 root root 9 Jul  4 11:48 1.txt
# 扩展: 文件合并
[root@luckly ~]# echo 11 > 1.txt
[root@luckly ~]# echo 22 > 2.txt
[root@luckly ~]# cat 1.txt 
11
[root@luckly ~]# cat 2.txt 
22
[root@luckly ~]# cat 1.txt 2.txt > all.txt
[root@luckly ~]# cat all.txt 
11
22
[root@luckly ~]# ll
total 12
-rw-r--r--. 1 root root 3 Jul  4 11:52 1.txt
-rw-r--r--. 1 root root 3 Jul  4 11:52 2.txt
-rw-r--r--. 1 root root 6 Jul  4 11:52 all.txt

7.cp 复制文件

代码语言:javascript
复制
语法格式:
      cp 源文件 目标位置
      cp /etc/hosts 当前
      cp /etc/hosts /opt/ 复制到指定位置
      cp /etc/hosts host.bak 改名
      cp /etc/hosts /opt/hosts.txt 改名
案例1:将当前位置的1.txt复制到/opt目录下
代码语言:javascript
复制
[root@luckly ~]# touch 1.txt
[root@luckly ~]# ll
total 0
-rw-r--r--. 1 root root 0 Jul  4 12:01 1.txt
# 清空/opt目录下所有的文件
[root@luckly ~]# rm -rf /opt/*
[root@luckly ~]# ll /opt/
total 0
[root@luckly ~]# cp 1.txt /opt/
[root@luckly ~]# ll
total 0
-rw-r--r--. 1 root root 0 Jul  4 12:01 1.txt
[root@luckly ~]# ll /opt/
total 0
-rw-r--r--. 1 root root 0 Jul  4 12:02 1.txt
# 如果目标文件存在则提示是否覆盖?
[root@luckly ~]# cp 1.txt /opt/
cp: overwrite ‘/opt/1.txt’?  输入y或者n  y为覆盖 n为不拷贝
案例2:拷贝多个文件到/opt目录
代码语言:javascript
复制
[root@luckly ~]# touch test.txt luckly.txt
[root@luckly ~]# ll
total 0
-rw-r--r--. 1 root root 0 Jul  4 12:01 1.txt
-rw-r--r--. 1 root root 0 Jul  4 12:03 luckly.txt
-rw-r--r--. 1 root root 0 Jul  4 12:03 test.txt
# 注意: 拷贝多个文件最后的必须为目录
[root@luckly ~]# cp luckly.txt test.txt /opt/1.txt
cp: target ‘/opt/1.txt’ is not a directory
    目标1.txt不是一个目录
[root@luckly ~]# cp luckly.txt test.txt /opt/
[root@luckly ~]# ll /opt/
total 0
-rw-r--r--. 1 root root 0 Jul  4 12:05 1.txt
-rw-r--r--. 1 root root 0 Jul  4 12:06 luckly.txt
-rw-r--r--. 1 root root 0 Jul  4 12:06 test.txt
# 如果想覆盖目标文件且不提示则加\
[root@luckly ~]# cp 1.txt luckly.txt test.txt /opt/
cp: overwrite ‘/opt/1.txt’? 
cp: overwrite ‘/opt/luckly.txt’? 
cp: overwrite ‘/opt/test.txt’? 
[root@luckly ~]# \cp 1.txt luckly.txt test.txt /opt/
# 在LInux系统中系统为了保证数据安全在危险的命令执行的时候增加了-i的参数
rm -i
mv -i
cp -i
案例3:拷贝不同目录下的文件到/opt目录
代码语言:javascript
复制
# 复制/etc/hsots luckly.txt /opt/1.txt 到/tmp目录下
清空tmp目录
[root@luckly ~]# rm -rf /tmp/*
[root@luckly ~]# 
[root@luckly ~]# cp /etc/hosts luckly.txt /opt/1.txt /tmp/
[root@luckly ~]# ll /tmp/
total 4
-rw-r--r--. 1 root root   0 Jul  4 12:15 1.txt
-rw-r--r--. 1 root root 158 Jul  4 12:15 hosts
-rw-r--r--. 1 root root   0 Jul  4 12:15 luckly.txt
案例4:改名
代码语言:javascript
复制
# 将当前的luckly.txt 改名为 2.txt
[root@luckly ~]# cp 1.txt 2.txt
[root@luckly ~]# ll
total 0
-rw-r--r--. 1 root root 0 Jul  4 12:01 1.txt
-rw-r--r--. 1 root root 0 Jul  4 12:16 2.txt
# 将当前目录的1.txt拷贝到/tmp目录改名为1.txt.bak
[root@luckly ~]# cp 1.txt /tmp/1.txt.bak
[root@luckly ~]# ll /tmp/
total 4
-rw-r--r--. 1 root root   0 Jul  4 12:15 1.txt
-rw-r--r--. 1 root root   0 Jul  4 12:18 1.txt.bak
# 复制/opt目录下的luckly.txt 到/opt目录改名为luckly.bak
相对路径:
[root@luckly ~]# cd /opt/
[root@luckly opt]# ll
total 0
-rw-r--r--. 1 root root 0 Jul  4 12:13 1.txt
-rw-r--r--. 1 root root 0 Jul  4 12:07 luckly.txt
-rw-r--r--. 1 root root 0 Jul  4 12:07 test.txt
[root@luckly opt]# cp luckly.txt luckly.bak
[root@luckly opt]# ll
total 0
-rw-r--r--. 1 root root 0 Jul  4 12:13 1.txt
-rw-r--r--. 1 root root 0 Jul  4 12:19 luckly.bak
-rw-r--r--. 1 root root 0 Jul  4 12:07 luckly.txt
绝对路径:
[root@luckly ~]# cp /opt/1.txt /opt/1.bak
案例5:将/etc/hosts拷贝到当前目录
代码语言:javascript
复制
[root@luckly ~]# rm -rf *
[root@luckly ~]# cp /etc/hosts .
[root@luckly ~]# ll
total 4
-rw-r--r--. 1 root root 158 Jul  4 12:21 hosts
[root@luckly ~]# cp /etc/passwd /opt/luckly.txt .
[root@luckly ~]# ll
total 8
-rw-r--r--. 1 root root 158 Jul  4 12:21 hosts
-rw-r--r--. 1 root root   0 Jul  4 12:22 luckly.txt
-rw-r--r--. 1 root root 981 Jul  4 12:22 passwd
# 拷贝源文件必须存在
[root@luckly ~]# cp /tmp/1111.txt .
cp: cannot stat ‘/tmp/1111.txt’: No such file or directory
案例6:拷贝目录
代码语言:javascript
复制
命令参数:
    -r  # 递归
# 拷贝目录及目录下所有的文件到当前
[root@luckly ~]# rm -rf *
[root@luckly ~]# rm -rf /opt/*
[root@luckly ~]# ll /opt/
total 0
# cp无法直接操作目录 必须使用参数
[root@luckly ~]# cp /etc .
cp: omitting directory ‘/etc’
[root@luckly ~]# cp -r /etc .
[root@luckly ~]# ll
total 12
drwxr-xr-x. 79 root root 8192 Jul  4 12:25 etc
# 拷贝多个目录
[root@luckly ~]# cp -r /etc /opt/luckly .
[root@luckly ~]# ll
total 12
drwxr-xr-x. 79 root root 8192 Jul  4 12:28 etc
drwxr-xr-x.  2 root root    6 Jul  4 12:28 luckly

8.mv 移动文件

代码语言:javascript
复制
语法结构:
      mv 源文件 目标位置              # 移动单个文件
      mv file1 file2 目标位置        # 移动多个文件
      mv luckly.txt luckly.bak      # 改名
      mv dir 目标位置 
案例1.将当前1.txt移动到/opt目录下
代码语言:javascript
复制
[root@luckly ~]# touch 1.txt
[root@luckly ~]# ll
total 0
-rw-r--r--. 1 root root 0 Jul  4 15:18 1.txt
# 将当前的1.txt移动到/opt目录下
[root@luckly ~]# mv 1.txt /opt/
[root@luckly ~]# ll
total 0
[root@luckly ~]# ll /opt/
total 0
-rw-r--r--. 1 root root 0 Jul  4 15:18 1.txt
drwxr-xr-x. 2 root root 6 Jul  4 12:28 luckly
[root@luckly ~]# 
# 将/opt/1.txt移动到当前目录
[root@luckly ~]# mv /opt/1.txt ./
[root@luckly ~]# ll
total 0
-rw-r--r--. 1 root root 0 Jul  4 15:18 1.txt
案例2:移动多个文件
代码语言:javascript
复制
[root@luckly ~]# touch 2.txt 3.txt
[root@luckly ~]# ll
total 0
-rw-r--r--. 1 root root 0 Jul  4 15:18 1.txt
-rw-r--r--. 1 root root 0 Jul  4 15:22 2.txt
-rw-r--r--. 1 root root 0 Jul  4 15:22 3.txt
[root@luckly ~]# rm -rf /tmp/*
[root@luckly ~]# mv 1.txt 2.txt 3.txt /tmp/
[root@luckly ~]# ll /tmp/
total 0
-rw-r--r--. 1 root root 0 Jul  4 15:18 1.txt
-rw-r--r--. 1 root root 0 Jul  4 15:22 2.txt
-rw-r--r--. 1 root root 0 Jul  4 15:22 3.txt
[root@luckly ~]# ll /tmp/
total 0
-rw-r--r--. 1 root root 0 Jul  4 15:18 1.txt
-rw-r--r--. 1 root root 0 Jul  4 15:22 2.txt
-rw-r--r--. 1 root root 0 Jul  4 15:22 3.txt
# 将/tmp目录下所有的文件移动到当前目录
[root@luckly ~]# mv /tmp/* .
[root@luckly ~]# ll
total 0
-rw-r--r--. 1 root root 0 Jul  4 15:18 1.txt
-rw-r--r--. 1 root root 0 Jul  4 15:22 2.txt
-rw-r--r--. 1 root root 0 Jul  4 15:22 3.txt
案例3:修改文件名称
代码语言:javascript
复制
# 将1.txt修改为1.bak
[root@luckly ~]# mv 1.txt 1.bak
[root@luckly ~]# ll
total 0
-rw-r--r--. 1 root root 0 Jul  4 15:18 1.bak
-rw-r--r--. 1 root root 0 Jul  4 15:22 2.txt
-rw-r--r--. 1 root root 0 Jul  4 15:22 3.txt
# 将1.bak 移动到/opt/改名称为1.log
[root@luckly ~]# mv 1.bak /opt/1.log
[root@luckly ~]# ll /opt/
total 0
-rw-r--r--. 1 root root 0 Jul  4 15:18 1.log
案例4:移动目录
代码语言:javascript
复制
[root@luckly ~]# ll /opt/
total 0
-rw-r--r--. 1 root root 0 Jul  4 15:18 1.log
drwxr-xr-x. 2 root root 6 Jul  4 12:28 luckly
[root@luckly ~]# ll /opt/luckly/
total 0
[root@luckly ~]# touch /opt/luckly/2.txt
[root@luckly ~]# ll /opt/luckly/
total 0
-rw-r--r--. 1 root root 0 Jul  4 15:30 2.txt
# 移动目录luckly到当前目录下
[root@luckly ~]# mv /opt/luckly/ .
[root@luckly ~]# ll
total 0
-rw-r--r--. 1 root root  0 Jul  4 15:22 2.txt
-rw-r--r--. 1 root root  0 Jul  4 15:22 3.txt
drwxr-xr-x. 2 root root 19 Jul  4 15:30 luckly
[root@luckly ~]# ll /opt/
total 0
-rw-r--r--. 1 root root  0 Jul  4 15:18 1.log
drwxr-xr-x. 2 root root 24 Jul  4 15:32 test
# 将test目录移动到当前目录命名为test1
[root@luckly ~]# mv /opt/test/ ./test1
[root@luckly ~]# ll
total 0
-rw-r--r--. 1 root root  0 Jul  4 15:22 2.txt
-rw-r--r--. 1 root root  0 Jul  4 15:22 3.txt
drwxr-xr-x. 2 root root 19 Jul  4 15:30 luckly
drwxr-xr-x. 2 root root 24 Jul  4 15:32 test1

9.rm 删除文件

代码语言:javascript
复制
语法结构:
    命令参数:
        -f参数 强制删除不提示 force强制的意思
  rm file                # 删除文件
  rm dir                # 删除目录
  rm file1 file2 ..        # 删除多个文件
  rm dir1 dir2 ..        # 删除多个目录
案例1:删除1.txt
代码语言:javascript
复制
[root@luckly ~]# touch 1.txt
会提示用户是否删除 y删除 n不删除
[root@luckly ~]# rm 1.txt
rm: remove regular empty file ‘1.txt’?
# 在命令前加\ eg.(\rm)取消别名还原本意
[root@luckly ~]# \rm 1.txt
[root@luckly ~]# ll
total 0
-rw-r--r--. 1 root root  0 Jul  4 15:22 2.txt
-rw-r--r--. 1 root root  0 Jul  4 15:22 3.txt
drwxr-xr-x. 2 root root 19 Jul  4 15:30 luckly
drwxr-xr-x. 2 root root 24 Jul  4 15:32 test1
案例2:删除多个文件
代码语言:javascript
复制
# 强制删除2.txt 3.txt
[root@luckly ~]# rm -f 2.txt 3.txt 
[root@luckly ~]# ll
total 0
-rw-r--r--. 1 root root  0 Jul  4 15:47 1.txt
drwxr-xr-x. 2 root root 19 Jul  4 15:30 luckly
drwxr-xr-x. 2 root root 24 Jul  4 15:32 test1
[root@luckly ~]# ll
total 0
-rw-r--r--. 1 root root  0 Jul  4 15:47 1.txt
drwxr-xr-x. 2 root root 19 Jul  4 15:30 luckly
drwxr-xr-x. 2 root root 24 Jul  4 15:32 test1
# 删除/opt下的1.log和当前路径的1.txt
[root@luckly ~]# ll /opt/
total 0
-rw-r--r--. 1 root root 0 Jul  4 15:18 1.log
[root@luckly ~]# rm -f 1.txt /opt/1.log 
[root@luckly ~]# ll
total 0
drwxr-xr-x. 2 root root 19 Jul  4 15:30 luckly
drwxr-xr-x. 2 root root 24 Jul  4 15:32 test1
[root@luckly ~]# ll /opt/
total 0
案例3:删除目录(在工作中尽量使用mv替代rm)
代码语言:javascript
复制
# rm默认无法直接操作目录 需要参数配合
[root@luckly ~]# rm test1
rm: cannot remove ‘test1’: Is a directory
[root@luckly ~]# rm -rf test1
[root@luckly ~]# ll
total 0
drwxr-xr-x. 2 root root 19 Jul  4 15:30 luckly
案例4:删除多个文件
代码语言:javascript
复制
[root@luckly ~]# mkdir luckly test luckly1
[root@luckly ~]# ll
total 0
drwxr-xr-x. 2 root root 6 Jul  4 16:06 luckly
drwxr-xr-x. 2 root root 6 Jul  4 16:06 luckly1
drwxr-xr-x. 2 root root 6 Jul  4 16:06 test
[root@luckly ~]# rm -rf luckly luckly1 test
[root@luckly ~]# ll
total 0
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2023年07月07日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1. pwd 显示当前所在的路径 print working directory
  • 2. cd 切换目录 change directory
  • 3.ls 查看文件或目录
  • 4.touch 创建普通文件 摸(如果文件存在则修改文件的时间)
  • 5.mkdir 创建目录
  • 6.cat 查看文件内容
  • 7.cp 复制文件
  • 8.mv 移动文件
  • 9.rm 删除文件
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档