DEV Community

Cover image for What are some useful bash aliases you use?
Tushar Tyagi
Tushar Tyagi

Posted on • Updated on

What are some useful bash aliases you use?

Here are mine.

1. yeet (delete) the directory and all it's contents.

alias yeet="rm -rf"
Enter fullscreen mode Exit fullscreen mode

2. quickly move one or two directory up in bash.

alias ..="cd .."
alias ...="cd ../.."
Enter fullscreen mode Exit fullscreen mode

3. [function] Extract files from any archive. (stolen from Manjaro's Default bashrc)

ex ()
{
  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      ;;
      *)           echo "'$1' cannot be extracted via ex()" ;;
    esac
  else
    echo "'$1' is not a valid file"
  fi
}
Enter fullscreen mode Exit fullscreen mode

Please do not forget to share yours.

Top comments (3)

Collapse
 
vonheikemen profile image
Heiker • Edited

The only use case I have for du

alias duh='du -d 1 -h'

For directory navigation I prefer numbers.

alias -- -='cd -'
alias c1='cd ..'
alias c2='cd ../..'
alias c3='cd ../../..'
alias c4='cd ../../../..'

Some shortcuts for common tmux commands

alias ta='tmux attach -t'
alias tl='tmux list-sessions'
alias ts='tmux new-session -A -D -s'
alias tmus='ts music "$(which cmus)"'

alias pmd-start='ts pomodoro gone -e "notify-send -u critical Pomodoro Timeout"'

That last one uses: gone.

And some cool key bindings. I put these in a .inputrc file in my home.

# Up Arrow
"\e[A": history-search-backward

# Down Arrow
"\e[B": history-search-forward

This changes the behavior of the Up and Down arrows, if you have some text in your prompt it will perform a search using that text. Is like a "prefix search". Let's say I have these three commands in my history.

vim /tmp/test.txt
nvim /tmp/test.txt
echo "a string with vi in it"
vim /tmp/other-text.txt

If you start writing vi and then hit the Up arrow it will only cycle through the commands that start with vi. In this case it will skip nvim and the one that has vi in the middle.

If you are not a fan of changing the arrow keys behavior I suggest using these.

# Alt + k
"\ek": history-search-backward

# Alt + j
"\ej": history-search-forward

You can find more aliases and functions on my dotfiles.

Collapse
 
paraspl01t profile image
Tushar Tyagi

I'll steal you directory navigation aliases.

Collapse
 
soconn profile image
soconn • Edited

alias psg="ps -ef| grep"
alias update="sudo apt update && sudo apt upgrade -y && sudo snap refresh"
alias adios="update && sudo shutdown"
alias ltr="ls -ltr"
alias h="history | grep "