绝对路径和相对路径及cd、pwd、mkdir、rmdir、tree命令详解
一、绝对路径和相对路径(由下图进行讲解)
绝对路径:由根目录开始(/)开始的文件名或者目录名称,我们通过绝对路径切换到/man1文件处,所经过的路径。
cat /root/usr/share/man/man1
如下图
相对路径:相对于当前路径,以当前所在位置的相对路径。例如,我现在在/sbin目录下,我要访问man1文件。相对路径为
cat /usr/share/man/man1
如下图
同理我们也可以用绝对路径来访问这个文件 cd /root/usr/share/man/man1 如下图
那我们访问一个文件或者目录时最好用相对路径还在绝对路径? 这要看你所处的位置,如下图的两种情况,
1.我在share目录中,我要访问man1文件; 2.我在man目录下要访问test目录。
我们可以发现第二种情况,我们要访问test1目录只有一种方式,使用绝对路径。 这下应该能理解相对路径和绝对路径。至于我当前在share目录下我要访问man1文件,我应该用那种路径。这需要看你访问man1文件的目的等等,这里暂时不做详细介绍。
二、cd命令
切换工作目录 无论是作为一名管理员还是一名Linux普通的用户,我们在使用Linux系统过程中,都会因为需要不停的切换工作目录。这时候就需要用到cd这个命令。
1.使用方法:
cd destination e.g. 如上图所示我们由切换到tmp目录下的test1目录下的1目录下的2目录下
[root@local ~]# cd /tmp/test1/1/2
[root@local 2]# pwd
/tmp/test1/1/2
注意(均是双引号里面为特殊目录)
在这里面我们需要记住几个特殊的目录,可以帮助更加便捷的使用Linux。
1. "."代表是当前目录的意思
2. ".."代表是上一级目录的意思
cd .. 我们可以快速切换到上一级目录,比使用相对路径和绝对路径都更加快速。
[root@local ~]# cd -
/tmp/test1/1/2
[root@local 2]# cd ..
[root@local 1]# cd -
/tmp/test1/1/2
[root@local 2]# cd ..
[root@local 1]# pwd
/tmp/test1/1
[root@local 1]# cd ..
[root@local test1]# pwd
/tmp/test1
[root@local test1]# cd ..
[root@local tmp]# pwd
/tmp
[root@local tmp]# cd ..
[root@local /]# pwd
/
3. "-"在刚刚使用过的工作目录与现在的工作目录进行来回切换
[root@local 2]# cd -
/root
[root@local ~]# cd -
/tmp/test1/1/2
[root@local 2]# cd -
/root
[root@local ~]# cd -
/tmp/test1/1/2
4. "~" 代表当前用户家目录
5. "~mageedu" 代表mageedu这个用户的jiamu
root 用户可以直接切入任何其他用户的家目录,而不需要密码认证。
三、pwd命令
显示当前工作目录
[root@local 1]# pwd
/tmp/1
-P 选项,显示软链接指向路径而不显示当前路径。具体看下面的命令。
[root@local tmp]# ll
total 0
lrwxrwxrwx. 1 root root 4 Jul 31 09:35 1 -> /dev
[root@local 1]# pwd
/tmp/1
[root@local 1]# pwd -P
/dev
可以看到1目录时个软链接指向dev目录,而pwd只是显示的当前路径,而加上-P显示的就是软链接指向的路径。
注意:pwd命令一般的用法都是直接用命令而不跟着任何参数。
同时又相关的环境变量也能实现这个功能:
1.PWD(大写) :显示当前所在目录
[root@local ~]# echo $PWD
/root
2.OLDPWD(大写):显示上一次所在目录
[root@local ~]# cd /tmp
[root@local tmp]# cd /usr
[root@local usr]# echo $OLDPWD
/tmp
四、mkdir
创建一个目录,这个命令也是管理Linux基础的命令之一。请务必掌握
常用选项
-m, –mode=MODE,创建指定MODE的目录,注意这个权限不需要减掉umask。如下图
[root@local tmp]# ll
total 0
drwxrwxrwx. 2 root root 6 Jul 31 10:24 test.dir
[root@local tmp]# mkdir --mode=111 test.dir2
[root@local tmp]# ll
total 0
drwxrwxrwx. 2 root root 6 Jul 31 10:24 test.dir
d--x--x--x. 2 root root 6 Jul 31 10:25 test.dir2
-p, –parents 连续创建多个不存在的目录,并且不会报错。我们知道mkdir这个命令只能在存在的路径中创建目录,而我们有时候需要这样创建,例如,在tmp目录下面再创建1目录,1目录下再创建2目录,2目录下再创建3目录,3目录下再创建4目录,4目录下再创建5目录。要求一次创建成功。这是就需要用过这个选项,具体见下图。
[root@local tmp]# mkdir -p /tmp/1/2/3/4/5
[root@local tmp]# tree /tmp
/tmp
├── 1
│ └── 2
│ └── 3
│ └── 4
│ └── 5
五、tree
上面的图我们可以大概知道tree命令的用法。没错,tree命令就用来显示目录和文件的层级结构。
常用选项
-a 显示所有文件和目录,除了.和.. (常用)
[root@local tmp]# tree -a /root
/root
├── anaconda-ks.cfg
├── .bash_history
├── .bash_logout
├── .bash_profile
├── .bashrc
├── .cache
│ ├── abrt
│ │ └── lastnotification
│ └── dconf
│ └── user
├── .config
│ ├── abrt
│ └── lftp
├── .cshrc
├── .dbus
│ └── session-bus
│ └── 7a8157e8e3f64383a8612754a1682bcb-9
├── initial-setup-ks.cfg
├── .lesshst
├── .local
│ └── share
│ └── lftp
│ ├── cwd_history
│ ├── rl_history
│ └── transfer_log
├── .tcshrc
├── .viminfo
├── .xauth3F8Xbp
└── .xauth3fbnpT
11 directories, 18 files
-d 仅仅显示目录(常用和-L 一起使用)
[root@local tmp]# tree -d /root
/root
0 directories
-L显示几级层数,和-d 一起使用
[root@local usr]# tree -L 1 -d /
/
├── bin
├── boot
├── dev
├── etc
├── home
├── lib
├── lib64
├── lost+found
├── media
├── mnt
├── opt
├── proc
├── root
├── sbin
├── selinux
├── srv
├── sys
├── tmp
├── usr
└── var
20 directories
tree还有很多其他的命令,详见man。常用的就是以上的选项
六、rmdir
这是一个很少用的命令,因为他的一起功能我们能通过rm 实现,因此就不在这里做过多介绍需要了解详解man。
原创文章,作者:fighter,如若转载,请注明出处:http://www.178linux.com/26965