Skip To Content

Command Line

Command line, the backbone of all front-end coding.

Pipes #

Pipes are used to direct the output of one command into another.

For example, you can run a grep to get a list of times a string appears in a file. Then you can pipe those results into another command to organize or filter them. Multiple pipes can be used at once in this way.

A useful command with a pipe is ps aux | grep <string>, which shows all the processes that contain a specific string. See more info about it in Killing a Process below.

Useful Commands #

grep #

grep is often combined with a pipe to organize the results. Some useful ones are:

sed #

sed manipulates text in files without need a text editor, often through regular expressions.

awk #

awk is a command for processing text files. It usually is set up like this:

awk '/<string_1>/ {<actions>}' <file_name>

The awk command processes the file line by line. By default, it looks for all lines that have string_1 in them. The ones that match will have the actions executed on them, which can be MANY things (the default is just printing the entire record). It does all this in the file_name that's been targeted.

A working example is awk '/root/ {print $1, $9;}' temp.txt. It looks for all lines in the text file with root in them, and prints the first and ninth words in each line. Each line's word is counted as a record, and the $ are the "fields" that can identify specific records. In this case it looks for records 1 and 9, written as fields $1 and $9.

You don't need to include at all if you don't need to. Writing it as awk '{print $1, $9;}' temp.txt prints the noted fields for every line.

However it can also have much simpler uses with the defaults. If you want to show all lines that contain a specific string, you can use awk '/<string>/' <file_name>. If you want to print only the first word in each matching line, you would use awk '/<string>/ {print $1;}' <file_name>.

You can also use more complex logic here. If you know field three has an integer, and you only want ones larger than 10, you could use awk '{if ($3 > 10) print "Matches the condition: ", $1;}' <file_name>. This only prints field one for ones where field three matches the condition. It also adds custom text to the print statement by using double quotes.

awk also has BEGIN and END, which denote code that will run before or after each file's line is run. An example from the above link using this shows it can even do basic calculations, like so:

awk '/gold/ {ounces += $2} END {print "value = $" 425*ounces}' coins.txt
  1. This looks for all lines with the word "gold" in them
  2. It takes the number of ounces shown in each line, which is in field two or $2, and adds them to an ounces variable
  3. After doing this for all lines in the file, it calculates the final value based on the total ounces from each record.

If you wanted to add a BEGIN function to this, it'd look like this:

awk BEGIN {print "Runs first!"} '/gold/ {ounces += $2} END {print "value = $" 425*ounces}' coins.txt

awk also has several pre-defined variables to use:

Killing a Process #

This is useful if trying to run something locally but it can't start since it's already started or the needed port is in use.

Note about terminal instances: each new window is a different instance of a shell. This means you can change something on one terminal window that won't automatically carry to others. For example, changing the active version of Ruby in one window, switching to another, and realizing that change didn't affect that window. So if you're making terminal changes in multiple windows, be aware your changes light not be in sync with each other.