My Favorite Linux Aliases

“A misspelled word is probably an alias for some desperate call for aid...” - Ben Marcus

Photo by Jon Tyson on Unsplash

The alias command (and the related unalias command) is a means to make command-line interface commands easier to remember. What follows is a sample of my favorite aliases.

I have provided a link to them here in my keypuncher GitHub repository -- note that there are more aliases in that file than covered here. Note further that Bash and zsh (see https://ohmyz.sh/) are almost (if not fully) compatible with one another. At least I haven’t yet run into any differences between these two shells. In any case, the examples provided in my repository are written in zsh.

Networking

Show open ports (see netstat - Wikipedia):

alias ports="netstat -tulanp"

Show current connections (see lsof - Wikipedia):

alias connections="sudo lsof -nPi"

dos2unix

If the need arises to remove those pesky windows-style line endings (see Newline) then this dos2unix alias may be helpful:

alias d2u="dos2unix"

See also DOS to Unix: Commands and Examples (phoenixnap.com).

Trash Instead of Remove (rm)

I have previously covered (see Useful Utils) my affinity for the trash tool. The given alias is now a bit out of date because a while ago you had to enter “trash-put” instead of “trash” but now I think most flavors of Linux accept “trash” once the trash-cli package is installed. Nonetheless it’s still hanging around in my .zshrc file:

alias trash="trash-put"

To further enforce not using the Linux remove command (see rm (Unix)) I have defined an in-line function to do just that:

alias rm='function remove(){ rm "$1"; echo -e "\e[1m\e[91mPlease use trash-put to delete.\n\e[0m"; };remove'

With this alias in place, if rm is called with any options the result is:

  • a call to remove a directory will fail

  • a call to remove a file will only delete the first file in the list and then echo a polite request to use trash in the future.

Some scripts I run require the rm command explicitly so I allow rm if it is called without options.

Filesystem Aliases

To prevent unintentional overwrites with cp (see cp (Unix) - Wikipedia):

alias cp="cp -i"

To prevent unintentional overwrites with mv (see mv (Unix) - Wikipedia):

alias mv="mv -i"

Find files taking up too much space (to identify the fat ducks):

alias ducks="du -ch | sort -hr | less"

See du (Unix), sort (Unix), and less (Unix).

Alias for ls by date (see ls - Wikipedia):

alias lsdate="ls -FlAtr"

Alias for ls by size:

alias lssize="ls -FlASr"

Processes

The ps command with lots of info (see ps (Unix) - Wikipedia):

alias psverbose='ps -Helf'

Calls psverbose listing only the current user’s processes:

alias psme="psverbose | grep $USER"

Better Diff and Git

Arguably better diff (see Stack Overflow):

alias diff="colordiff"

Git Aliases:

alias gs="git status"

alias gf="git fetch"

alias ga="git add"

alias gb="git branch"

alias gbd="git branch -d"

alias gbD="git branch -D"

alias gc="git commit"

alias gcm="git commit -m"

alias gcam="git commit --amend -m"

alias gco="git checkout"

alias gcob="git checkout -b"

alias grv="git remote -v"

alias grs="git reset"

alias gcp="git cherry-pick"

alias gcpa="git cherry-pick --abort"

alias gcpc="git cherry-pick --continue"

alias gdiff="git diff"

alias gl="git log"

alias grl="git reflog"

alias grb="git rebase"

alias grba="git rebase --abort"

alias grbc="git rebase --continue"

alias grbi="git rebase --interactive"

alias gsm="git submodule"

alias gsms="git submodule sync"

alias gsmsr="git submodule sync --recursive"

alias gsmu="git submodule update"

alias gsmur="git submodule update --recursive"

alias gsmuir="git submodule update --init --recursive"

alias gw="git whatchanged"

alias gls="git ls-files"

And that’s it for now! For those who don’t like scrolling, the link to my repo above is: https://github.com/mday299/keypuncher/tree/main/aliases.

Please let me know some of your favorite aliases in the comments!

Credits

6 lesser-known but seriously useful Linux commands | Enable Sysadmin (redhat.com)

The 40 Most-Used Linux Commands You Should Know (kinsta.com)

Git - Git Aliases (git-scm.com)

10 git aliases for faster and productive git workflow | Snyk

Previous
Previous

Linting with clang-format

Next
Next

Demystifying Regular Expressions