Let me tell you how to maximize your productivity on the Bourne Again SHell while minimizing your effort. bash has a ton of tricks and shortcuts that allow you to command it with little effort, and I intend to show you the features that help me day in and day out.
Today I’m going to explain the use of features like history, brace and file expansion, and other tricks by example and give you references for later.
Master your history
Those who don’t know their history are doomed to repeat it. This is arguably one of the best productivity enhancing features of any shell.
You can check your history with the history command, which prints your entire history by default. Alternatively, you can filter the list with:
[bash]
history 10 # prints last 10 entries
history | grep cmd # searches history for cmd
[/bash]
Each entry has a number, which you can then execute with !<number>
Now suppose I want to copy a file to a directory and then change to that directory. The quick way to do that with history is:
[bash]
cp myfile.txt my/directory/path
cd !$ # cd my/directory/path
[/bash]
or if I forget to run a command as super-user:
[bash]
vi /etc/fstab # oops!
sudo !! # sudo vi /etc/fstab
[/bash]
Continue reading →
One of my favorite ways to save time on the command-line is to utilize the directory stack to jump between tasks. Today’s article will show you how to do this and provide some tips for effective use.
What is the directory stack?
Most Linux environments have a way for you to put paths on a stack (push) and then take them off in reverse order (pop). This is useful when you have more than one directory that you need to switch between frequently. Let’s take a look at how to do this.
The pushd command
Suppose you need to switch between your project: ~/src/myproject, your web-server: /opt/webserver7/logs and some code examples: ~/examples/othercode often.
Continue reading →
I seem to preach a lot about automation for productivity, and with good reason. You should not have to perform mundane tasks repeatedly. Crontab is a fantastic tool for simply running exactly what you want at times you specify.
Fire up your terminal or Cygwin now.
crontab tutorial
Suppose I want to copy my personal wiki to my website every other hour between 8:30 and 18:30 on weekdays only. This only takes a couple minutes to setup with a bit of cron-fu.
I’m going to go ahead and use FTP to put my wiki where I want, so I wrote a quick bash script (backup_wiki.sh) for this purpose:
[code language="bash"]
#!/bin/bash
# File: backup_wiki.sh
HOST='mysite.com'
USER='myuser'
PASS='mypassword'
ftp -n ${HOST} <
quote USER ${USER}
quote PASS ${PASS}
put path/to/my/wikifile.html wikifile.html
bye
END_SCRIPT
exit 0
[/code]
Sweet, so now we can just use backup_wiki.sh
Let's edit (or create) our new crontab file:
[code language="bash"]
crontab -e
[/code]
This brings up vi (by default) with a file that may have a comment or may be empty. I don't feel like using vi right now, so I'll change it to jEdit by adding the following to my .bashrc file:
[code language="bash"]
export EDITOR="[/path/to/jedit.bat (windows) or 'jedit' (*nix)]"
[/code]
Ah, that's better. Now that we can open it up in our fav. text editor, let's learn how to create an entry in our crontab file. Continue reading →