Primary Menu

Category Archives: Programming

In the first part of the series I wrote about basic console commands. Those are very useful. But besides the plain commands you can also use aliases or even functions to enhance your command line workflows. For more complex tasks various build systems can be used.

Aliases

Every bash offers the possibility to adapt commands to your personal needs by using aliases. An alias is a symbol for a original command that can be called instead of the original command.

The first and most important alias for me is the alias that lets me open the file which defines the aliases (~/.bash_profile on MacOs):

alias aliases=’subl ~/.bash_profile’

The line starts with the string alias. This tells the bash that you want to define a new one. The name of your new alias comes next, in this case aliases. After an equal sign I define that I want to call “subl ~/.bash_profile” which will call my favourite text editor Sublime Text 2 and open the file .bash_profile. This way I can easily edit my aliases afterwards.

As I use GIT via the command line I created short aliases for commands I use frequently:

Functions

Aliases are great, but they are just aliases. If you need even more advanced functionality and more flexibility you can use functions. As software developer you should be familiar with functions. They have a name, a signature and offer a certain functionality. So you can incorporate complex logic in bash scripts and use the similar to aliases. I’m not an expert on complex functions, but those simple functions help a lot in my git workflow:

function gco() { git checkout $@; }
gco my-branch-or-commit

function gc() { git commit -m “$@” ;}
gc “my commit message”

Build Scripts/Automation

Automation is the key to remove cumbersome work which thwarts other more important tasks. A good way to automate tasks like copying, combining, compiling or even deploying files is to use a build system. There are endless numbers of build systems out there, you will easily be able to find one for your favourite runtime environment and language. Personally I love to use rake, grunt and gulp. All three help to create tasks in a very clean and declarative way without removing the flexibility of using a custom script. Also there a many gems (Rake) and plugins (grunt, gulp) out there, so you probably find one which solves your automation problem.

Conclusion

Automation is what technology is all about. There are many tools out there – especially for developers – that help you to automate repetitive tasks.

Nevertheless I think that automation needs time. Maybe you ask yourself how often you will reach the break-even-point at that the time invested on creating the automation is less than the time spent by manually doing a task. I think that often you won’t reach this point.

But I personally think that even if an automation project will never reach this point it helped you to keep your routine (so the next automation can be implemented faster) and also it saves you from repetitive tasks that can be frustrating and demotivating.

Learn how to automate, keep yourself trained and you will be more productive.

As software developer you need to be as productive as possible. Even if you use an IDE you might spent a lot of time in the command line, also called terminal/bash/shell.

So one idea is to improve your command line abilities.

Those commands are your friends. Instead of moving forward and backward from UI to the shell all time you might accomplish simple tasks in the shell. Why? Less distraction. More productivity. I assume that you know how to navigate with cd. Other simple commands are (most of them are Unix based):

man [command]man mvman cp
Should you ever forget what a command does, call the manual pages with the man command. Not that easy to read, but you will get used to. Quit by pressing [q] on your keyboard.

Move – mvmv file1.txt ./folder/file1.txtmv file1.txt file2.txt
Use this to move your file from one place to another. Or to rename a file.

Copy – cpcp file1.txt file2.txtcp file1.txt ./folder/file1.txt
cp -r folder1 folder2Us this command to copy one file to another location. Use the -r flag to copy a whole folder hierarchy.

Delete – rmrm file1.txtrm -r folderrm -rf folder
Use this command to remove files. Use rm -r to delete recursively in folders. -f means force, so the command does not ask you if files should really be deleted. Combine ’em and be careful***, cause there is no undelete!***

Find – findfind . -name “*.txt”
Use find to locate files you are searching for. In the example above we search in the current directory (.) with the parameter -name and set the name to “*.txt”. * is used as wildcard. This will show all *.txt files in the current folder and subfolders.

Show output – catcat file.txt
Cat can be used to output the contents of files. Makes sense for e.g. small text or configuration files.

Show output -lessless file.txtLess is also showing the contents of a file. But it is not printing them to the console like cat. This is useful for longer files, since the terminal has a limited number of rows. Press [q]to quit the program.

Create files – touchtouch file.txtSimply create a new file without content. For example useful for .gitkeep files, which will prevent empty folders from getting deleted.

Open files – openopen file.txtopen file.html
With this command you can open files with the default program. Use to open images or html-files in the browser.

Pipes with grepls -l | grep txtcat file.txt | grep John
Pipes are a unix thing. They connect two data streams. E.g. it could connect the output of a list command with the grep command that filters output as seen in the first example. Or take the output of the cat command and filter all lines with John in the line content.

Grepgrep -nr some_function ./src
Grep can be used for many things. I found it useful to search for strings all files recursively. The example command searches for some_function in ./src recursively (-r) and displays the line number (-n).

I/O Redirectionecho “node_modules/” >> .gitignore
With >> you can redirect the standard output to a file instead of the bash. That means you can echo strings which are then appended to the file you name after the >>. Can be used to append single lines to text or configuration files. Careful: The single > does nearly the same, but instead of appending a line it overrides the file contents. That is only the tip of the iceberg, read more about it here.

If you get used to those commands you can drastically improve your productivity. Try it!