Linux hash table Study Notes




A, hash table summarizes


When a Linux system for the first time execute external commands, hash cache table is empty;
this time, the system will start looking at the PATH command, the path will be added to find the Hash cache;

When this command is executed again, the path will be executed directly from the hash table;
If there is direct execution, will continue to find if there is no path from the PATH;

Therefore, hash table can call the command rate of increase.




Two, hash cache table position


Command executed on Linux systems, there is a execution priority, the location cache hash table is as follows:

alias -------------------------------------别名
  builtin------------------------------内部命令
    hash-------------------------缓存表
      $PATH---------------可执行程序或脚本(外部命令)

Linux on the difference between internal commands and external commands are interested can poke "Linux internal commands and external commands" understanding.




Three, hash command Introduction


hashCommand can be used to display and configure the cache hash table;
when executing the command, the system will first check the cache hash table.

hash The main parameters command:

-l    显示哈希表所有的项目
-r    清除哈希表所有项
-d <名称>   删除哈希表其中的一项
-p <路径>   向哈希表中增加一项内容,添加后就可以使用了
-t  <命令>  显示hash表中命令的完整路径,如果没有就会报not found错误



Four, hash command usage examples


:~> bash   # 进入一个全新的 shell
:~> hash
hash: hash table empty    # 由于是新建 shell,所以hash表为空,符合预期

:~> ls -l

:~> hash
hits    command
   1    /usr/bin/ls
:~> top

:~> hash
hits    command
   1    /usr/bin/ls
   1    /usr/bin/top

:~> uptime
 17:56:40 up 1138 days, 7 min,  1 user,  load average: 0.47, 0.36, 0.28

:~> hash   #  可以看到hash表在不断更新
hits    command
   1    /usr/bin/uptime
   1    /usr/bin/ls
   1    /usr/bin/top

:~> hash -l
builtin hash -p /usr/bin/uptime uptime
builtin hash -p /usr/bin/ls ls
builtin hash -p /usr/bin/top top

:~> hash -p /tmp/mydate  date   # 手动添加hash表

:~> hash -l
builtin hash -p /usr/bin/uptime uptime
builtin hash -p /tmp/mydate date   # 添加成功
builtin hash -p /usr/bin/ls ls
builtin hash -p /usr/bin/top top

:~> date
bash: /tmp/mydate: No such file or directory    # 由于手动添加的hash表指向执行文件不存在,所以报错,这说明新增的hash表确实在work

:~> hash -d  date   # 手动清楚刚加的hash表,使用 -r 则全部清除

:~> hash -l
builtin hash -p /usr/bin/uptime uptime
builtin hash -p /usr/bin/ls ls
builtin hash -p /usr/bin/top top

:~> date   # 恢复
Sun Jan 19 18:02:45 CST 2020

Published 37 original articles · won praise 24 · views 3072

Guess you like

Origin blog.csdn.net/weixin_44648216/article/details/104059516