Bash shortcuts
One thing I enjoy about using the command line and Bash is that it allows you to create shortcuts for actions you perform on a regular basis. If you often cd
into the same long directory path or run a set of commands repeatedly you could just assign the former to an alias and the latter to a function. This can be done in your .bashrc
file.
Aliases
E.g. I often end up with files on the desktop. To go there more quickly I can assign an alias desk
to the command cd ~/desktop
.
Granted, that’s not a huge improvement, but it has become part of my workflow.
alias desk='cd ~/desktop'
To open my todo list quickly in the Sublime text editor I use the following:
alias todo='subl /path/to/my/todo_list.md'
Here are a bunch of standard unix commands that I like to overwrite with their interactive and verbose counterparts, so I get a prompt when deleting, moving or copying files:
alias mv='mv -iv'
alias cp='cp -iv'
alias rm='rm -iv'
When listing files in a directory I also like more information, like permissions, file sizes and hidden files. So I use ls -lah
aliased as ll
.
alias ll='ls -lah'
# So instead of just one visible file
macbook:dotfiles (master) $ ls
readme.md
# I get all hidden files and extra details
macbook:dotfiles (master) $ ls -lah
total 32
drwxr-xr-x 7 robert staff 238B 7 Aug 13:47 .
drwxr-xr-x 95 robert staff 3.2K 15 Aug 14:48 ..
drwxr-xr-x 13 robert staff 442B 9 Aug 13:32 .dotfiles
drwxr-xr-x 13 robert staff 442B 25 Aug 00:43 .git
-rw-r--r-- 1 robert staff 107B 5 Nov 2016 .gitignore
-rw-r--r-- 1 robert staff 630B 5 Nov 2016 readme.md
Sometimes it can be useful to find out which IP address your ISP assigned you:
alias myip='curl http://ipinfo.io/ip'
This alias uses cURL
to send a get request to http://ipinfo.io/ip which responds with my external IP address.
Here is one last shortcut I don’t use that often, but it can be helpful when you need to tether your laptop with your iPhone to get internet access:
alias tether='networksetup -setairportnetwork en0 iPhone my_password'
This is MacOS specific and the last two arguments are the name of your iPhone and the password set for wifi tethering on your smartphone.
Functions
Bash functions allow you to do even more cool stuff and write small programs that when added to your .bashrc
are available everywhere in your terminal. To take this a step further you could write shell scripts and make them executable and put them in your path, but we won’t get into this here.
A simple function I wrote to create a new file with touch
and then open the same file with my Sublime text editor:
new() {
touch $1
subl $1
}
This can be used by running $ new my_new_text_file.txt
. In that example my_new_text_file.txt
is the first argument passed to new
and it is assigned to the $1
variable. That way the file name can be used inside the function to create the file and to open it with subl
.
If you ever feel the need to create a random and strong password from the command line there is a helpful tool called pwgen. All the below new_password
function does is call pwgen
with some arguments to customize what password I want. Instead of printing out the password to the screen it copies it to your clipboard by piping stdout to pbcopy
and then prints that it did so.
new_password() {
pwgen --secure --symbols 16 1 | pbcopy
echo "Password copied to clipboard"
}
Here is another function. I use it to upload and share a file under my domain name files.robertsj.com. It’s a bit more involved and uses the B2 service command line client. It uploads a file to B2 and returns a shareable link. It’s helpful for quickly sharing a file like a screenshot.
# Upload file to B2 and get shareable link
upload_and_share() {
filename=$1
# generate new shared file name
date=`date +%Y-%m-%d`
# generate random url safe string
random_string=`cat /dev/urandom | env LC_CTYPE=C tr -cd 'a-z0-9' | head -c 6`
file_extension=`echo $filename | awk -F . '{print $NF}'`
# build shared file name out of random string, date and file extension
shared_file_name="$random_string-$date.$file_extension"
# $filename has to be in quotes in case it has spaces
b2 upload_file bucket_name "$filename" $shared_file_name
sharing_link="https://files.robertsj.com/file/bucket_name/$shared_file_name"
echo "File uploaded"
echo $sharing_link | pbcopy
echo "Sharing link $sharing_link copied to clipboard"
}