The Linux Command-Line (WIP)

Purpose

This lesson provides an introduction to the Linux command-line. Command line tools are less discoverable than graphical tools, but once you get used to them they’re often more efficient. It is also easier to combine multiple tools together and automate repetitive workflows.

Learning Material

Start by reading through this MDN command line crash course.

Read through the ten lessons at linuxcommand.com. Note that most of the discussion applies, to some degree, to macOS as well.

Exercises

To learn as much as possible from these exercises, write your responses before revealing the provided answers. If any exercises seem irrelevant, you can skip them and instead write a justification as to why they are unimportant. These justifications will help us improve the lesson for future employees.

Exercise 1

Write a cheat sheet with all of the new commands you learned.

Exercise 2

Ripgrep is a powerful commandline tool that searches through files for patterns. It is similar to grep, although it is often more convenient to use since it will ignore many types of files developers often don’t care about.

Install ripgrep using the appropriate package manager for your operating system (e.g., homebrew for macOS). Once you’ve installed ripgrep, try running which rg. What does the output mean? Run man rg and read through the description. You may also want to glance through the rip-grep user manual.

Clone a copy of our dicom standard repository. Now use ripgrep to search for the phrase “cow”. How many times does this word show up?

Answer

On my macOS I see:

~ $ which rg
/opt/homebrew/bin/rg

The which command searches for the location of the executable for a given command. In this case, it indicates that the rg executable is located in /opt/homebrew/bin. The which command is useful for debugging. E.g., if you want to know which python executable is being resolved by your shell.

To count how many lines include the word “cow” in it, you can pipe the output of rg cow into wc -l. (If you don’t recall what wc does, try running man wc.)

Here’s what I see:

~/Projects/innolitics/dicom-standard $ rg cow | wc -l
  49

What happens if the word “cow” shows up twice on a line though? I was curious if ripgrep had a count option, so I ran man rg | rg count.

It appears that the --count option will work. Thus rg --count cow is a more robust way to count the matches than rg cow | wc -l.

Exercise 3

Search online for a command that will let you pipe stadard output into your operating systems’ clipboard. Use this to copy the results of rg cow into your clibpoard.

Answer

On windows you can use the clip commande. Thus, rg cow | clip should work. On macOS you should be able to use rg cow | pbcopy.

Exercise 4

jq is a lightweight and flexible command-line JSON processor. Install jq and read through the tutorial here.

Read through the examples under each of the basic filters on this page.

Our DICOM Standard repository is a set of Python scripts that is used to convert the DICOM standard from a bunch of HTML into a consistent JSON format. The final JSON files are stored in the standard subdirectory of the repository. You can use these JSON files to play around with jq.

Using jq, print each CIOD’s name to standard output (if you don’t know what a CIOD is, don’t worry about that for now!).

Answer

I produced the list using this command:

~/Projects/innolitics/dicom-standard/standard $ jq '.[].name' ciods.json

Continuous Lesson Improvement

Please help us make these lessons as relevant and up-to-date for future engineers as possible!

You can help in several ways:

  • Shorten or clarify the writing. We're all busy and less is more.
  • Ask if the purpose of the lesson is unclear. We want all of the lessons to seem useful.
  • Remove exercises or learning material that aren't useful.
  • Add more exercises, exercise answers, or learning material as appropriate.

You can quickly open the lesson page in the GitHub editor. Create a new branch and pull request and assign it to David.