Skip to content

Instantly share code, notes, and snippets.

@RyanSchu
Last active January 7, 2019 16:52
Show Gist options
  • Save RyanSchu/ced43408f0939b7bbe68299045f4d564 to your computer and use it in GitHub Desktop.
Save RyanSchu/ced43408f0939b7bbe68299045f4d564 to your computer and use it in GitHub Desktop.

About Aliases

aliases are a way to create shortcuts for a given command. While their scope is limited they are useful for saving time on tedious typing. For example, using the bash command less there is the useful flag -S that prevents text from wrapping on the display. This is quite useful for readability but it can be tedious to write less -S every time. Instead what you can do is use alias to create a shortcut for that specific command.

alias less='less -S'

Now whenever you ype less it instead performs less -S by default. However this alias will go away once you leave the session. If you want the alias to be permanent then we will need to work with two hidden files

Making a permanent alias

first lets create a hidden file that will contain all the aliases you want. hidden files follow the syntax .file_name. To create a hidden file do

mkdir .bash_aliases
chmod a+x .bash_aliases #make it executable

If you want to find this file again you use ls -a to find hidden files. Now we can fill this file with all the aliases we think will be useful. And this is not limited to just aliases. Useful functions can also be placed within the file. I've attached an example file here containing all the aliases and functions I use.

Next, your home directory should contain a hidden file called .bashrc. This file is actually executed every time you start up a session on the lab machine. Open it with nano .bashrc and take a look at it. You may notice that this already contains several aliases you may already use. Chances are you may find a snippet of code that says the following:

if [ -f ~/.bash_aliases ]; then
    . ~/.bash_aliases
fi

If not found then you can simply add this snippet to this file. This will execute the bash aliases file at startup every time. These aliases will not take effect until you log back in for the first time, so log out and log in if you want to use them immediately

Undoing Aliases

to undo an alias in the long term you can simply delete it from the bash_aliases file. However this will not remove the alias until you restart your session. To remove an alias immediately use unalias

That's all, have fun with aliases!

#count the max number of columns in a file
maxcol() {
awk '{print NF}' "$1" | sort -nu | tail -n 1
}
#count the min number of columns in a file
mincol() {
awk '{print NF}' "$1" | sort -nu | head -n 1
}
#transpose a file like a matrix
transpose() {
awk '
{
for (i=1; i<=NF; i++) {
a[NR,i] = $i
}
}
NF>p { p = NF }
END {
for(j=1; j<=p; j++) {
str=a[1,j]
for(i=2; i<=NR; i++){
str=str"\t"a[i,j];
}
print str
}
}' "$1"
}
alias zless='zless -S'
alias less='less -S'
alias tl='sudo tar -tf'
alias chx='chmod a+x'
alias chr='chmod a+r'
alias sudo='sudo '
alias cd..='cd ..'
#list files by date created, newest first
alias lt='ls -t'
#douple check that you want to remove more than three files
alias rm='rm -I'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment