讲解思路:
对$PATH含义的解释
更改PATH的方法
当你在linux中的任何一个目录下输入pwd命令,都会显示当前目录的位置
[root@localhost human]# pwd
/lives/human
问题:pwd可是一条命令啊,这条命令是存放在某个目录下
[root@localhost human]# whereis pwd;
pwd: /bin/pwd /usr/include/pwd.h /usr/share/man/mann/pwd.n.gz /usr/share/man/man1/pwd.1.gz
pwd命令存在于/bin目录下,可是你却可以在/lives/human目录下调用它,这不奇怪吗?计算机可不是神?你如果不告诉它,去哪里找pwd命令。它是不可能找到pwd命令的,进而它会显示-bash: pwd: command not found,但是现实是它却是能够在任何目录下执行pwd命令?这到底发生了什么?记得Java中的path吗?聪明的你,应该一瞬间就明白了这一切把戏。不错,安装linux系统时,已经默认在PATH中写进了/bin/pwd,当你执行pwd时,计算机首先去/bin中去查找,发现存在pwd命令,可以使用。我们可以看看,/bin中是否存在pwd命令:
[root@localhost /]# cd /bin
[root@localhost bin]# ls -l
-rwxr-xr-x. 1 root root 85304 7月 12 2017 ps
-rwxr-xr-x. 1 root root 28008 3月 23 2017 pwd
-rwxr-xr-x. 1 root root 8184 1月 26 15:41 raw
这就是为什么你在任何地方都可以使用pwd命令的原因。
假如我现在将/bin/ls命令剪切到/lives下,你是否还可以在任何地方使用ls呢?一起来看看。
[root@localhost lives]# ls -l
总用量 4
drwxr-xr-x. 3 root root 4096 5月 10 21:28 human
[root@localhost lives]# mv /bin/ls /lives/
[root@localhost lives]# ls -l
-bash: /bin/ls: 没有那个文件或目录
将/bin下的ls命令转移到/lives下,然后再/lives下使用ls命令,发现报错。可是我的ls命令,确实是在/lives下啊,那能不能在/bin中使用呢?虽然此时/bin中已经没有ls命令了。
[root@localhost lives]# cd /bin
[root@localhost bin]# ls -l
-bash: /bin/ls: 没有那个文件或目录
果然还是不可以,至于其它目录就更不可能使用ls的可能性。为什么会这样?还是那个问题PATH
之所以不能够在/bin中使用ls命令的原因是虽然PATH中存在/bin目录,但是/bin目录下此时并没有ls命令。
之所以不能在/lives中使用ls命令的原因是虽然lives中存在ls命令,但是/lives并没有写进PATH中。
那可咋办?
方法一:使用绝对路径
[root@localhost lives]# /lives/ls
human ls
方法二:使用相对路径
[root@localhost lives]# ./ls
human ls
问题:为什么刚才在/lives中直接使用ls不可以,而此处使用./ls就可以呢?这个问题你知道麻烦你告诉我,我不知道。
方法三:自己设置将/lives添加到PATH中
[root@localhost lives]# PATH="$PATH":/lives;
[root@localhost lives]# ls
human ls
提醒一下:将ls恢复到/bin中去,系统默认的目录结构绝对不可以随意更改,否则,你会死得很惨。
[root@localhost lives]# mv /lives/ls /bin
[root@localhost lives]# cd /
[root@localhost /]# whereis ls;
ls: /bin/ls /usr/share/man/man1/ls.1.gz
=====================================================================
下午发现,上午最后漏了点什么,不仅仅需要将ls转移到/bin,而且将/bin规范到PATH。
[root@localhost /]# ls -l
-bash: /lives/ls: 没有那个文件或目录
[root@localhost /]# PATH="$PATH":/bin
[root@localhost /]# ls -l
总用量 13074
dr-xr-xr-x. 2 root root 4096 5月 10 22:48 bin
dr-xr-xr-x. 5 root root 1024 5月 9 21:30 boot
drwxr-xr-x. 19 root root 3740 5月 10 16:14 dev
drwxr-xr-x. 83 root root 4096 5月 10 16:13 etc
drwxr-xr-x. 2 root root 4096 5月 9 19:31 fromwindows
drwxr-xr-x. 4 root root 4096 5月 10 17:42 home
dr-xr-xr-x. 9 root root 4096 5月 9 21:57 lib
dr-xr-xr-x. 10 root root 12288 5月 9 21:29 lib64
drwx------. 2 root root 16384 5月 7 23:02 lost+found
drwxr-xr-x. 2 root root 4096 9月 23 2011 media
drwxr-xr-x. 2 root root 4096 9月 23 2011 mnt
drwxr-xr-x. 3 root root 4096 5月 9 00:35 mongodb
drwxr-xr-x. 2 root root 4096 9月 23 2011 opt
dr-xr-xr-x. 99 root root 0 5月 10 16:13 proc
drwxr-xr-x. 18 git 1000 4096 5月 9 22:45 Python-2.7.14
-rw-r--r--. 1 root root 12576112 9月 17 2017 Python-2.7.14.tar.xz
dr-xr-x---. 8 root root 4096 5月 10 23:44 root
dr-xr-xr-x. 2 root root 12288 5月 9 21:29 sbin
drwxr-xr-x. 7 root root 0 5月 10 16:13 selinux
-rw-r--r--. 1 root root 702770 5月 10 00:10 setuptools-33.1.1.zip
drwxr-xr-x. 2 root root 4096 9月 23 2011 srv
drwxr-xr-x 13 root root 0 5月 10 16:13 sys
drwxrwxrwt. 3 root root 4096 5月 10 23:06 tmp
drwxr-xr-x. 13 root root 4096 5月 7 23:02 usr
drwxr-xr-x. 18 root root 4096 5月 8 06:39 var