Posted: . At: 6:01 AM. This was 3 years ago. Post ID: 14846
Page permalink. WordPress uses cookies, or tiny pieces of information stored on your computer, to verify who you are. There are cookies for logged in users and for commenters.
These cookies expire two weeks after they are set.


Some very useful Linux bash functions.


Show a preview of a directory as you cd into it.

c() { cd "$@" 
    ls_truncate=20
    files=$(ls -F -C --color=always)
    files_num=$(echo "$files" | wc -l)
    echo "$files" | head -n $ls_truncate
    [ $(echo "$files" | wc -l) -gt "$ls_truncate" ] && echo "(Ommited $((files_num-$ls_truncate)) files/directories)" 
}

This is a very useful bash function. Below is this function in action, this will show the first 20 files and folders in the directory.

jason@jason-Lenovo-H50-55:~/Documents$ c systemd/
azure-pipelines.yml  Makefile           presets/           test/
catalog/             man/               README             tmpfiles.d/
coccinelle/          meson.build        README.md          TODO
configure*           meson_options.txt  rules.d/           tools/
docs/                mkosi.build*       semaphoreci/       travis-ci/
factory/             modprobe.d/        shell-completion/  units/
hwdb.d/              network/           src/               xorg/
LICENSE.GPL2         NEWS               sysctl.d/          zanata.xml
LICENSE.LGPL2.1      po/                sysusers.d/

Print manual pages in color.

man() {
    env \
        LESS_TERMCAP_mb=$(printf "\e[1;31m") \
        LESS_TERMCAP_md=$(printf "\e[1;31m") \
        LESS_TERMCAP_me=$(printf "\e[0m") \
        LESS_TERMCAP_se=$(printf "\e[0m") \
        LESS_TERMCAP_so=$(printf "\e[1;44;33m") \
        LESS_TERMCAP_ue=$(printf "\e[0m") \
        LESS_TERMCAP_us=$(printf "\e[1;32m") \
        man "$@"
}

This makes reading man pages easier and much more fun.

Universal extraction function, this allows easy extraction of files from any archive format.

winrar ()
{
  if [ -f $1 ] ; then
    case $1 in
      *.tar.bz2)   tar xjf $1   ;;
      *.tar.gz)    tar xzf $1   ;;
      *.bz2)       bunzip2 $1   ;;
      *.rar)       unrar x $1   ;;
      *.gz)        gunzip $1    ;;
      *.tar)       tar xf $1    ;;
      *.tbz2)      tar xjf $1   ;;
      *.tgz)       tar xzf $1   ;;
      *.zip)       unzip $1     ;;
      *.Z)         uncompress $1;;
      *.7z)        7z x $1      ;;
      *.deb)       ar x $1      ;;
      *.tar.xz)    tar xf $1    ;;
      *.tar.zst)   unzstd $1    ;;
      *)           echo "'$1' cannot be extracted via winrar()" ;;
    esac
  else
    echo "'$1' is not a valid file. Your trial has expired. Please purchase a valid copy of WinRar."
  fi
}

These functions placed in your ~/.bashrc file will be very useful to streamline your computer usage on the Linux command line. Linux is very powerful when using the Bash shell, and these show just how useful it really is.


Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.