Programmer Productivity – Part 1

Update 2014-05-06: Added I/O Redirection

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 mv
man 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 – mv
mv file1.txt ./folder/file1.txt
mv file1.txt file2.txt
Use this to move your file from one place to another. Or to rename a file.

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

Delete – rm
rm file1.txt
rm -r folder
rm -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 – find
find . -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 – cat
cat file.txt
Cat can be used to output the contents of files. Makes sense for e.g. small text or configuration files.

Show output -less
less file.txt
Less 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 – touch
touch file.txt
Simply create a new file without content. For example useful for .gitkeep files, which will prevent empty folders from getting deleted.

Open files – open
open file.txt
open 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 grep
ls -l | grep txt
cat 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.

Grep
grep -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 Redirection
echo “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!