Useful Bash Aliases and Functions
Working efficiently on the command line can significantly enhance your productivity, especially when you’re frequently performing repetitive tasks. In this post, I’ll share some of my favorite bash aliases and functions that I’ve accumulated from around the web and find incredibly useful. These shortcuts and helpers streamline various tasks, from file operations to Git commands, making your workflow smoother and more efficient.
General Shortcuts
Date and Job Queue
alias Q="date && squeue --user jnprice"
This is probably my most used shortcut. When running jobs on our Slurm-based HPC, I like to be able to see the current date and time, as well as the status of my jobs in the queue using squeue
. It’s really handy for keeping track of the time of your job submissions without needing to run multiple commands.
Listing Files
alias lh="ls -lh"
I often want to list the contents of a directory in the long format so that I can check file sizes and modification time. The ls -lh
command lists files in the current directory with human-readable file sizes. Using this alias makes it easier to quickly see file sizes without needing to interpret byte counts.
Directory Operations
alias md="mkdir"
alias rd="rmdir"
I’m extremely lazy efficient and these aliases simplify the creation and removal of directories. md
for making directories and rd
for removing them. Intuitive and saves me a few keystrokes.
Create and Navigate
function mkcd() {
mkdir -p "$1" && cd "$1"
}
I’m constantly creating directories to keep my analyses organised. This function creates a directory (and any necessary parent directories) and then navigates into it. It’s perfect for when you want to create a new directory and start working in it immediately.
Interactive File Operations
alias rm="rm -i"
alias mv='mv -i'
alias cp='cp -i'
Like many others, I’ve had some bad experiences using rm
with wildcards. These aliases have saved me several times and make the rm
, mv
, and cp
commands interactive by prompting you before making any changes, preventing accidental deletions or overwrites.
Disk Usage
alias duh='du -sh ./* ./.*'
When using a shared system, it’s important to be a considerate user and keep an eye on how much disk space you’re using. This alias calculates disk usage for all files and directories in the current directory, including hidden ones, and presents the information in a human-readable format. It’s very handy for identifying what’s taking up space before a quick tidy up.
Universal Extractor
extract () {
if [ $# -ne 1 ]
then
echo "Error: No file specified."
return 1
fi
if [ -f $1 ] ; then
case $1 in
*.tar.bz2) tar xvjf $1 ;;
*.tar.gz) tar xvzf $1 ;;
*.bz2) bunzip2 $1 ;;
*.rar) unrar x $1 ;;
*.gz) gunzip $1 ;;
*.tar) tar xvf $1 ;;
*.tbz2) tar xvjf $1 ;;
*.tgz) tar xvzf $1 ;;
*.zip) unzip $1 ;;
*.Z) uncompress $1 ;;
*.7z) 7z x $1 ;;
*) echo "'$1' cannot be extracted via extract" ;;
esac
else
echo "'$1' is not a valid file"
fi
}
Whether you’ve generated it yourself or you’ve downloaded it from a public database, lots of data is compressed to save space. This versatile function handles the extraction of almost all of the compressed file types that you’re likely to come across. The best thing is that you only need to remember one command to extract files, regardless of their format.
Git Aliases
Using Git for version control is obviously important for providing a history of any changes made, enabling easy tracking, reversing, and keeping track of modifications over time. These last few shortcuts make working with Git just a bit easier.
Git Status
alias gstatus='git status -sb'
This alias provides a succinct summary of the Git status, showing changes in a brief format which I find easier and quicker to read than the normal output.
Git Branch
alias gbranch="git checkout -b "
You can use this shortcut to quickly create and switch to a new Git branch, making it a simple way to streamline branch management.
Lazy Git Commit
function lazygit() {
git add .
git commit -m "$1"
git push
}
The ultimate function for any time conscious bioinformatician! This stages all changes, commits them with a specified message, and pushes to the remote repository. It’s a super convenient way to quickly commit all your changes with a single command.
By incorporating these aliases and functions into your .bashrc
or .bash_profile
, you can reduce the amount of typing and save time, working smarter not harder!
Enjoy Reading This Article?
Here are some more articles you might like to read next: