1

I have a shell script i use frequently to manually clear the dentries, inodes and page cache in my RAM: ramflush.sh

#!/bin/bash
echo " ██▀███   ▄▄▄       ███▄ ▄███▓   "
echo "▓██ ▒ ██▒▒████▄    ▓██▒▀█▀ ██▒  _____ "
echo "▓██ ░▄█ ▒▒██  ▀█▄  ▓██    ▓██░   |   | F"
echo "▒██▀▀█▄  ░██▄▄▄▄██ ▒██    ▒██    |   |  L "
echo "░██▓ ▒██▒ ▓█   ▓██▒▒██▒   ░██▒   |   |   U"
echo "░ ▒▓ ░▒▓░ ▒▒   ▓▒█░░ ▒░   ░  ░   \___|    S       _"
echo "  ░▒ ░ ▒░  ▒   ▒▒ ░░  ░      ░     ||  ____H__  -( (-"
echo "  ░░   ░   ░   ▒   ░      ░        |_'(-------)  '-'"
echo "   ░           ░  ░       ░           |       /"
echo "___________VERSION 1.0______________,-\__..__|_____"
echo " "
read -p "[*] Do you have a need to flush?:    " yn
case $yn in
   [Yy]* ) ;;
   [Nn]* ) echo "[X] Understood."; exit;;
       * ) echo "[X] No input detected. Exiting."; exit;;
 esac

echo " "
echo " <=== OPTIONS ===>"
echo " "
echo "1. Clear RAM Page Cache."
echo "2. Clear Dentries and Inodes."
echo "3. Clear Page Cache, Dentries and Inodes."
echo " "
read -p "[*] Choose what to flush:    " ans

case $ans in
   [1]* ) echo 1 > /proc/sys/vm/drop_caches; echo "[*] Cache Cleared.";;
   [2]* ) echo 2 > /proc/sys/vm/drop_caches; printf "[*]Dentries Cleared.\n[*]Inodes Cleared.\n";;
   [3]* ) echo 3 > /proc/sys/vm/drop_caches; printf "[*]Page Cache Cleared\n[*]Dentries Cleared.\n[*]Inodes Cleared.\n";;
      * ) echo "[X] No input detected. Exiting."; exit;;

esac

However it gets tiresome constantly changing back to my home directory, then going in to a folder and calling the script. I also refuse to just do the commands manually because it defies the point to me having made the script.

Is there a way I can add this to my bash shell in order to be able to run the script from any directory simply by typing ramflush in order to call and run this script, similar to nmap or ping?

Would I have to add it to the package manager and how do I go about doing this?

  • In your ~/.bash_profile (or ~/.profile, whichever you use), add the script's directory to your PATH. Then, assuming the script file is executable, you can run it from anywhere. – glenn jackman Nov 12 '18 at 22:11
  • 1
    Place it in the ~/bin directory and add it to your path or place it in the /usr/local/bin folder which is already on your path – George Udosen Nov 12 '18 at 22:11
  • You might want to investigate the select bash command to present your menu. – glenn jackman Nov 12 '18 at 22:12

2 Answers2

3

The quickest way to do this, would be using an alias.

Add this to your .bashrc:

alias ramflush='/path/to/your/script/ramflush.sh'

Then open a new terminal or run source ~/.bashrc to reload your bash configuration.

You can now call ramflush anywhere in the terminal.

Wayne_Yux
  • 4,873
2

You can drop that script as ramflush without the sh extenion in two places:

  1. ~/bin or
  2. /usr/local/bin,

for the first case add that path in .bashrc with the line export PATH=$PATH:$HOME/bin and the other is already in your path. Now you can simply type ramflush.

George Udosen
  • 36,677
  • Thankyou for all the help. I've accepted @GeorgeUdosen 's answer as the best answer because of method two, which, from my perspective as a rookie to shell scripting - is the most universal method and easy to incorporate in a shell script to make this happen automatically. When I consulted that directory it appears that several tools have coined this method all ready. –  Nov 12 '18 at 22:23