Development Productivity: Part 1, Linux Command-Line

This is the first post of a five part series aimed at development productivity. The subjects covered in the series will be:

  1. Basic linux commands
  2. "dot" file configurations
  3. SSH, Networking commands
  4. PHP and git command options and shortcuts
  5. Process and disk monitoring

The first part deals with shortcuts and common commands I use in my day to day development on a linux environment.

Moving around the command line
These shortcuts are useful when you type a long command and need to modify the beginning part or end part without using the mouse
ctrl-a jump to the start of the current line.
ctrl-e jump to the end of a line
[up arrow] cycle through previous commands
Start typing a command, file name or directory and then hit tab to complete it. You can also type

to view your most recent commands

Moving about To switching to the directory foobar you would type

cd foobar
. To switch to the directory zerotheorem you would type
cd zerotheorem
. To go back and forth between directories you can push and pop the directory onto a stack for later retrieval, you can type
pushd foobar
. This saves the previous directory, zerotheorem onto the stack. Now if you type
, this will bring you back to zerotheorem directory. I find that if I have a few directories that I frequently navigate to, then setting up some permanent aliases in my .bashrc file can be very useful. I will cover this in part 2 - "dot" file configurations.

Finding things To view the contents of a directory use ls.

ls -al
will give you a list and show hidden files.
Find files matching a pattern in the present directory and subdirectories with
find . -name 'foobar'
This might give output that you do not want such as
y[$]› find / -name 'hola'
find: `/run/udisks2': Permission denied
find: `/run/lightdm': Permission denied
find: `/run/wpa_supplicant': Permission denied
To hide the errors send the error stream to /dev/null with
so that the previous command is
find . -name 'foobar' 2>/dev/null

To search files, grep is your best friend.

grep function *php
would find all of the occurences of the word function in PHP files in the current directory. Common flags are -R for recursion, -i for case insensitivity, -v to ignore (invert) matches, -e, and -n to list the line number to specify a pattern. For example,
grep FUNction * -Rni 2>/dev/null | grep -v -e "private" -e "js"
would recursively match, case insensively lines with the keyword "FUNction" but not those that also contain the words "private" or "js".

Another useful flag for use with grep are -C=num_lines which allows you to specify a number of lines to output before and after the matching lines. This is useful when you want more context into matches. Lastly, the flag -l is useful when you only care about the files that match and not the details.

Create a file with touch. For example,

touch hello.txt
. Change permissions with chmod and owner/group with chown. For example,
touch hello.txt && chmod 755 hello.txt && chown brian.mygroup hello.txt

Sometimes you need to modify a file timestamp, for example if you have code that caches the file and need to immediately break the cache. Then you can use

touch -D "July 1, 2001" hello.txt"
to set the date to July 2001.

Create a directory with mkdir

View a file's contents with less. ie

less hello.txt
. View the start of a file with head and the end of it with tail. You can also specify a number of lines to view such as
tail -3 hello.txt
to view the last three lines of that file. Lastly, you can follow a file with the -f flag, such as
tail -f /some_log_file.log
, useful when monitoring logs or other frequently updated files.

Finally, one my favourite command shortcuts is the brace notation {} which enables a short hand for certian commands. Normally, a command such as copying a file would be

cp my_great_file.txt my_greatest_file.txt
. However you can also use the shorthand
cp my_great{,est}_file.txt
. Another use would be creating or moving multiple files
touch daily_file_{1,2,3,4,5,6,7}.txt
which is much nicer than typing out daily_file_ six more times!!

Hope you find a few of these shortcuts and commands useful. Stay tuned for more advanced usage and command examples. And feel free to comment about your favorite shortcuts/commands.