I started using Linux 10 years ago this month (actually, my very first Linux install would've been around 10 years ago today, though I'm not sure of the exact date). Throughout all those years, I've compiled a number of useful Bash functions and aliases that I use on a daily basis to save me time and help get things done. I figure that some of these would be useful to others as well, so I'm posting a list of them here, along with commentary where appropriate.
For those of you that either don't know what I'm talking about, or just aren't very familiar familiar with this topic, Bash stands for the "Bourne-again shell", and is the standard command line interface for Linux. There are plenty of other shells available, but Bash is the most common and is the default on most Linux distributions. Bash aliases and functions allow you to defined shortcuts for longer or more complicated commands.
Bash aliases are used for substituting a long/complicated string for a much shorter one that you type on the command line. As a simple example, consider the following alias that is defined by default on most distributions:
alias ls='ls --color'
This simply means that anytime you use the command ls
, bash will automatically substitute ls --color
for you. So, if you entered the command ls /home
, bash will treat this as ls --color /home
.
Bash functions provide the same essential concept, but allow for allow for much more complicated functionality through the use of shell scripting. Here's an example of a function:
function l() { locate "$1" | grep --color "$1"; }
This defines a function named l
that will:
- Pass the supplied argument (search term) to the locate command, then
- pipe the output to the grep command to highlight the matched results
So, if you entered the command l filename
, bash would actually run locate "filename" | grep --color "filename"
, This will search for all files on your computer named "filename", then use grep to highlight the word "filename" in the results. These are two fairly simple examples of aliases and functions, but when used frequently they can lead to significant time savings.
I'm including a full list of my personal aliases and functions below. Note: Some of these commands are rather obscure, but I'm including them anyway just for reference. At the very least, it may inspire similar shortcuts that make sense for you.
To use any of these, simply add them to your ~/.bashrc file.
######### # Aliases ######### # Show filetype colors and predictable date/timestamps alias ls="ls --color=auto --time-style=long-iso" # Highlight matched pattern alias grep='grep --color' # Common shortcuts and typos alias c=clear alias x=startx alias m=mutt alias svi='sudo vim' alias ci='vim' alias reboot='sudo /sbin/reboot' alias halt='sudo /sbin/halt' # Clear and lock console (non-X) terminal alias lock="clear && vlock -c" # If in a directory containing a symlink in the path, change to the "real" path alias rd='cd "`pwd -P`"' # Useful utility for sending files to trash from command line instead of # permanently deleting with rm - see http://code.google.com/p/trash-cli/ alias tp='trash-put' # Generic shortcut for switching to root user depending on system #alias root='su -' #alias root='sudo -i' alias root='sudo bash -l' # Compile kernel, install modules, display kernel version and current date # useful for building custom kernels; version and date are for the filename alias kernbuild='make -j3 && make modules_install && ls -ld ../linux && date' # Shortcut for downloading a torrent file on the command line alias bt='aria2c --max-upload-limit=10K --seed-time=60 --listen-port=8900-8909' # Only show button events for xev alias xevs="xev | grep 'keycode\|button'" # Launch dosbox with a preset configuration for Daggerfall alias daggerfall='dosbox -conf ~/.dosbox.conf.daggerfall' ########### # Functions ########### # Search Gentoo package database (portage) using eix # $1 = search term (package name) function s() { eix -Fc "$1"; } # Search all available; show summary function sd() { eix -FsSc "$1"; } # Search all available w/ desc.; show summary function se() { eix -F "^$1\$"; } # Search exact available; show details function si() { eix -FIc "$1"; } # Search installed; show summary # Search Debian package database (apt) using dpkg # $1 = search term (package name) #function s() { apt-cache search "$1" | grep -i "$1"; } # search all available # Search Arch package database using pacman # $1 = search term (package name #function s() { # echo -e "$(pacman -Ss "$@" | sed \ # -e 's#^core/.*#\\033[1;31m&\\033[0;37m#g' \ # -e 's#^extra/.*#\\033[0;32m&\\033[0;37m#g' \ # -e 's#^community/.*#\\033[1;35m&\\033[0;37m#g' \ # -e 's#^.*/.* [0-9].*#\\033[0;36m&\\033[0;37m#g' ) \ # \033[0m" #} # Mount/unmount CIFS shares; pseudo-replacement for smbmount # $1 = remote share name in form of //server/share # $2 = local mount point function cifsmount() { sudo mount -t cifs -o username=${USER},uid=${UID},gid=${GROUPS} $1 $2; } function cifsumount() { sudo umount $1; } # Generate a random password # $1 = number of characters; defaults to 32 # $2 = include special characters; 1 = yes, 0 = no; defaults to 1 function randpass() { if [ "$2" == "0" ]; then cat /dev/urandom | tr -cd '[:alnum:]' | head -c ${1:-32} else cat /dev/urandom | tr -cd '[:graph:]' | head -c ${1:-32} fi echo } # Display text of ODF document in terminal # $1 = ODF file function o3() { unzip -p "$1" content.xml | o3totxt | utf8tolatin1; } # Search all files on system using locate database # $1 = search term (file name) function li() { locate -i "$1" | grep -i --color "$1"; } # case-insensitive function l() { locate "$1" | grep --color "$1"; } # case-sensitive # View latest installable portage ebuild for specified package # $1 = package name function eview() { FILE=$(equery which $1) if [ -f "$FILE" ]; then view $FILE fi } # View portage changelog for specified package # $1 = package name function echange() { PACKAGE="$(eix -e --only-names $1)" if [ "$PACKAGE" != "" ]; then view /usr/portage/$PACKAGE/ChangeLog fi } # Displays metadata for specified media file # $1 = media file name function i() { EXT=`echo "${1##*.}" | sed 's/\(.*\)/\L\1/'` if [ "$EXT" == "mp3" ]; then id3v2 -l "$1" echo mp3gain -s c "$1" elif [ "$EXT" == "flac" ]; then metaflac --list --block-type=STREAMINFO,VORBIS_COMMENT "$1" else echo "ERROR: Not a supported file type." fi } # Sets custom Catalog Number ID3 tag for all MP3 files in current directory # $1 = catalog number function cn() { for i in *.mp3; do id3v2 --TXXX "Catalog Number":"$1" "$i"; done; }
Re: Bash (Shell) Aliases and Functions
very useful. thanks.