Wrap your Commands into a Shell Script

Recently when I try to backup a MongoDB database running in a docker container with command line, I find the command is too long to remember.

docker run --rm -it --net server_default --link server_mongo_1:mongo -v ~/backup:/backup mongo mongodump --host mongo --db olivia-server --out /backup

Wouldn't it be great if you don't need to remember or look up the command when use! Fortunatelty you can! All you need to do is wrap your long commands into a shell script.


Let me show you how to realize this:

1. Create a file called mongodump under the ~/bin directory:

mkdir ~/bin && touch ~/bin/mongodump

2. Edit the file and add your command:

#!/bin/sh
docker run --rm -it --net server_default --link server_mongo_1:mongo -v ~/backup:/backup mongo mongodump --host mongo --db olivia-server --out /backup

3. Modify the permission of your script file:

chmod 755 mongodump

4. Add ~/bin to your system path:

If you are using bash edit ~/.bash_profile or if you are using zsh edit ~/.zshrc add following line:

export PATH="$HOME/bin:$PATH"

Save and restart your shell. Now you can just type dockerdump to run the shell.