Linux2.2路径、删除目录及文件

路径

  1. 绝对路径:一定由根目录 / 写起,例如/usr/share/doc
  2. 相对路径:路径的写起不是由  /  写起。如例子中第三行表示为相对路径
    [root@chy001 doc]# pwd
    /usr/share/doc
    [root@chy001 doc]# cd ../man
    [root@chy001 man]# pwd
    /usr/share/man

cd命令

[root@chy001 etc]# pwd                    #显示当前路径
/etc
[root@chy001 etc]# cd .                     # . 代表此层目录
[root@chy001 etc]# cd ..                    # .. 代表上一层工作目录
[root@chy001 /]# cd -                        #  -  代表上一个工作目录
/etc
[root@chy001 etc]# cd -                     #  回到上一个工作目录,类似电视的回看
/
[root@chy001 /]# cd /etc/init.d
[root@chy001 init.d]# cd ~                 #  ~ 代表目前用户身份的主文件夹
[root@chy001 ~]# cd -
/etc/init.d
[root@chy001 init.d]# cd ~chyuanliu    #  ~account 代表account用户的主文件夹
[root@chy001 chyuanliu]# pwd
/home/chyuanliu

创建、删除目录

创建目录 mkdir
[root@chy001 ~]# mkdir /tmp/chyuanliu/1/2
mkdir: 无法创建目录"/tmp/chyuanliu/1/2": 没有那个文件或目录
[root@chy001 ~]# ls /tmp/chyuanliu
ls: 无法访问/tmp/chyuanliu: 没有那个文件或目录
[root@chy001 ~]# mkdir /tmp/chyuanliu              #创建目录
[root@chy001 ~]# mkdir -p /tmp/chyuanliu/1/2    #级联创建目录,-v可视化
[root@chy001 ~]# tree /tmp/chyuanliu/
/tmp/chyuanliu/
└── 1
    └── 2

2 directories, 0 files

#无-p选项的话,只能一层一层创建

删除目录 rmdir

[root@chy001 ~]# tree /tmp/chyuanliu/
/tmp/chyuanliu/
├── 1
│   └── 2
│       └── 1.txt
└── 3

3 directories, 1 file
[root@chy001 ~]# rmdir /tmp/chyuanliu/1/2/1.txt       #不可以删除文件
rmdir: 删除 "/tmp/chyuanliu/1/2/1.txt" 失败: 不是目录
[root@chy001 ~]# rmdir /tmp/chyuanliu/1/2/              #不可以删除非空目录
rmdir: 删除 "/tmp/chyuanliu/1/2/" 失败: 目录非空
[root@chy001 ~]# rmdir /tmp/chyuanliu/3
[root@chy001 ~]# tree /tmp/chyuanliu/
/tmp/chyuanliu/
└── 1
    └── 2
        └── 1.txt

2 directories, 1 file

rm命令

[root@chy001 chyuanliu]# mkdir -pv ./3/4
mkdir: 已创建目录 "./3"
mkdir: 已创建目录 "./3/4"
[root@chy001 chyuanliu]# tree
.
├── 1
│   └── 2
│       └── 1.txt
└── 3
    └── 4

4 directories, 1 file
[root@chy001 chyuanliu]# rm ./1/2/1.txt          #默认删除有交互
rm:是否删除普通空文件 "./1/2/1.txt"?y
[root@chy001 chyuanliu]# rm -fr ./1       #-f可以强制删除,无交互  -r可以删除目录    
[root@chy001 chyuanliu]# tree
.
└── 3
    └── 4

2 directories, 0 files
[root@chy001 chyuanliu]# touch ./3/4/1.txt
[root@chy001 chyuanliu]# rm -r ./3/
rm:是否进入目录"./3/"? y
rm:是否进入目录"./3/4"? y
rm:是否删除普通空文件 "./3/4/1.txt"?y
rm:是否删除目录 "./3/4"?y
rm:是否删除目录 "./3/"?y

[root@chy001 ~]# history
    1  init 0
    2  dhclient
    3  ip add
 ... ...
  171  touch ./3/4/1.txt
  172  rm -r ./3/
  173  tree
  174  cd ~
  175  history
[root@chy001 ~]# !173      # !数字    为命令历史里第某个命令
tree
.
└── anaconda-ks.cfg

0 directories, 1 file
[root@chy001 ~]# !t            # !字母     为命令历史里最近一个以该字母开头的命令
tree
.
└── anaconda-ks.cfg

0 directories, 1 file
原文地址:https://www.cnblogs.com/chyuanliu/p/7711356.html