7 essential command line skills for a professional developer

These essential command line skills will help you get more productive with the terminal by utilizing only native features. It will help you to brush up on basics if you are already familiar with command line. Note that these tips more fitted for Mac/Linux command line.

1. Figuring out how to use a particular command

Often you know the exact command you need to use but don’t know how to use it. For example, I can never remember how to properly use the find command. Do I specify the file name first and then the path? Here is how you can look it up without searching the web.

man find

will produce a screen that looks like this

essential command line skills
Manual for the find command.

This is a searchable manual that you can use to see usage examples for a given command. Let’s try to find these usage example in the manual. Type the following:

/examples

Then hit ENTER.

In case of the find command the first occurrence of examples term is not what you looking for so you need to hit n key to get to the actual examples section:

So by using the man utility you just found how to properly use the find command.

Exit the manual by pressing q.

Ability to know how to use a particular command is one of the essential command line skills.

2. Combining commands for more complex tasks

Embrace Unix core philosophy: small specialized commands can be combined to accomplish complex tasks. Here is how you do it.

Piping commands

Piping is when you use output of one command as an input for another command. The following example lists all text files in the current folder in descending order:

-> find . -name "*.txt" | sort -r
./5.txt
./4.txt
./3.txt
./2.txt
./1.txt

| is the pipe which takes the output of the find command and supplies it to the sort command.

Learn more about piping: https://alvinalexander.com/blog/post/linux-unix/linux-unix-command-mashups

Redirecting command input/output

Let’s say you need to save the output of the command from above into a file. You can use the redirect feature (>) as follows:

-> find . -name "*.txt" | sort -r > mylist.txt

This will create a file containing command’s output:

-> cat mylist.txt
./5.txt
./4.txt
./3.txt
./2.txt
./1.txt

Learn more about redirections: http://www.westwind.com/reference/os-x/commandline/pipes.html

Ability to combine commands is an absolute must in anyone’s list of essential command line skills.

3. Putting commands into custom scripts

For some reason my virtual machine always falls behind on time. To fix it I need to run few commands. If you are like me, instead of running these commands manually, you (1) put them into a file:

sudo service ntp stop
sudo ntpdate -s time.nist.gov
sudo service ntp start

(2) Save it under whatever name, for example, fixtime.

Using a command line, navigate into the folder where you saved the file and (3) make sure it is executable:

chrmod +x fixtime

Then (4) run it:

./fixtime

Done.

An ability to add some logic around this is another item in the set of essential command line skills. For example if you want to run these commands only if current time is out of sync, then you would need to turn it into a Bash script. Learn more about Bash scripting here https://linuxconfig.org/bash-scripting-tutorial.

4. Understanding $PATH

Now wouldn’t it be awesome if you could run the above custom fixtime script from anywhere on you machine and not just from the folder where you saved it? If you try to execute fixtime from any other folder, you’ll get:

fixtime
-bash: fixtime: command not found

The operating system is not able to find our script. An ability to fix issues like this certainly makes it into the list of essential command line skills. Turns out we need to make sure to tell the operating system where to look for our executables.

How does the operating system know where to look for executable files? Meet the environment variable $PATH.

First, let’s create a folder where we will keep all our scripts including fixtime:

mkdir /usr/bin

Update $PATH to include your newly created folder:

export PATH=$PATH:/usr/bin

Move the script into the folder:

mv ./fixtime /usr/bin

Now you should be able to run fixtime from any folder on your computer. If it still says command not found try to rebuild your bash profile:

source ~/.bash_profile

 

5. Navigating using command line

Below are few basic tips for navigating in a file system:

  1. Use the TAB key to autocomplete path and filenames when using the cd command. No need for all the typing.
  2. Use ~ alias for your home folder. For example:
    cd ~/Downloads
  3. Execute the cd command without any arguments to jump to your home directory.
  4. Use
    cd --

    to go to the previous folder.

  5. If you find yourself constantly jumping between two folders, split your terminal screen and switch between panes instead:

6. Creating shortcuts for folders

When you have to navigate to the same directory over and over (let’s say ~/Documents/personal/github/projects/project_x), you can create a link to save your future self a lot of typing:

ln -s ~/Documents/personal/github/projects/project_x ~/px

Now all you need to do to get to that folder is:

cd ~/px

px of course can be any string you want.

7. Creating shortcuts for commands

There are always few commands that you run multiple times a day. For example, I often check what changes are staged in Git cache:

git diff --cached

To save myself all that typing I created a file: /usr/bin/gdc containing the above command as plain text and made sure it is executable:

chmod +x /usr/bin/gdc

And now, whenever I want to see a cached diff I simply run

gdc

Note that for this to work your $PATH must include /usr/bin.


Command line is only one item on a tool belt of an experienced developer. Next, learn about best software engineering practices and principles.

Leave a Reply

Your email address will not be published. Required fields are marked *