Skip to content

Instantly share code, notes, and snippets.

@firedynasty
Last active September 8, 2021 18:28
Show Gist options
  • Save firedynasty/90ee3ab26c97fa019f1a3a24fb064582 to your computer and use it in GitHub Desktop.
Save firedynasty/90ee3ab26c97fa019f1a3a24fb064582 to your computer and use it in GitHub Desktop.
Change and store directories that you have been
function cd_func () {
  local x2 the_new_dir adir index
  local -i cnt

  if [[ $1 ==  "--" ]]; then
    cat -n /Users/username/desktop/macbook_pro_scripts/echo_files/test_3.txt
    return 0
  fi

  the_new_dir=$1
  [[ -z $1 ]] && the_new_dir=$HOME

  if [[ ${1:0:1} == '-' ]]; then
    #
    echo "$(cat /Users/username/desktop/macbook_pro_scripts/echo_files/test_3.txt | head -${1:1:10} | tail -1)" > /Users/stanleytan/desktop/macbook_pro_scripts/echo_files/recently_opened.txt
    echo "$(cat /Users/username/desktop/macbook_pro_scripts/echo_files/test_3.txt | head -${1:1:10} | tail -1)"
    sed -i '' 's!~!/Users/stanleytan!g' /Users/stanleytan/desktop/macbook_pro_scripts/echo_files/recently_opened.txt
    cd "$(cat /Users/username/desktop/macbook_pro_scripts/echo_files/recently_opened.txt)"
    return 0
  fi

  #
  # '~' has to be substituted by ${HOME}
  [[ ${the_new_dir:0:1} == '~' ]] && the_new_dir="${HOME}${the_new_dir:1}"

  #
  # Now change to the new dir and add to the top of the stack
  pushd "${the_new_dir}" > /dev/null
  [[ $? -ne 0 ]] && return 1
  the_new_dir=$(pwd)

  #
  # Trim down everything beyond 11th entry
  popd -n +11 2>/dev/null 1>/dev/null

  #
  # Remove any other occurence of this dir, skipping the top of the stack
  for ((cnt=1; cnt <= 10; cnt++)); do
    x2=$(dirs +${cnt} 2>/dev/null)
    [[ $? -ne 0 ]] && return 0
    [[ ${x2:0:1} == '~' ]] && x2="${HOME}${x2:1}"
    if [[ "${x2}" == "${the_new_dir}" ]]; then
      popd -n +$cnt 2>/dev/null 1>/dev/null
      cnt=cnt-1
    fi
  done
  
  
  marsh=$(echo $PWD)
  if [ "${#marsh}" -gt 30 ]; then 
    echo $PWD >> /Users/username/desktop/macbook_pro_scripts/echo_files/test_3.txt
  fi


  length_="$(cat /Users/username/desktop/macbook_pro_scripts/echo_files/test_3.txt | wc -l)"
  length_="${length_#"${length_%%[![:space:]]*}"}"
  length_="${length_%"${length_##*[![:space:]]}"}"

  if [ "$length_" -gt 22 ]; then
    sed -i '' '1d' /Users/username/desktop/macbook_pro_scripts/echo_files/test_3.txt

  fi
  return 0
}

alias cd="cd_func"


See: https://linuxgazette.net/109/marinov.html

Changed Row 6 : to read off the file that is storing these directories
Changed Row 14: jump to the number and row of the command cd_func --
Changed Row 49: for file dump into the file that is storing the directories :
/Users/username/desktop/macbook_pro_scripts/echo_files/recently_opened.txt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment