Basic Command Line Commands

Let’s talk basic command line commands today! I’ve shared some recent command line articles on Google PageSpeed Insights on Command Line (CLI) and Running Google Lighthouse programmatically, in addition to older posts like Screaming Frog Command Line (CLI) automation.

I want to share some more fundamental components of command line and batch scripting: basic command line commands! (Spoiler alert: this article, like the others only covers Windows Command Line, not Mac or Linux.)

Getting Started With Command Line / CMD

To do anything with command line commands, you’ll first need to open up the Command Prompt! This is an easy start: click into your search bar (if you have Windows 10 or recent) and type “CMD.”

The Command Prompt app should pop up as the best match – click it!

Opening up command prompt for basic command line commands

I certainly don’t recommend doing this every time. For convenience, you can right click the Command Prompt icon in your taskbar once open, and pin it for quick access. (Similar for Start Menu if that’s your fancy.)

Once Command Prompt is open, you should see some information about the version of Windows you’re running, the copyright statement, and your default current directory below.

The Command Prompt (CMD) for basic command line commands

Basic Commands & Parameters in CLI / CMD

Surprisingly, I feel there’s a gentle learning curve in using command prompt vs. some other programming languages or settings. One of the first things you can do learn the basic “super user” kind of commands in command line.

What’s Here? Using the “dir” command

The “dir” command in CLI/CMD isn’t far from Hello world! The “dir” command will show you information about the folders and files in your current directory.

You may often find that the default current directory for Command Line is C:\Users\[YOUR USER NAME HERE]

The line containing the single period represents the current directory, and the line containing two periods represents the parent directory above your current. (More often than not, this will be the C:\Users\ directory.)

From left to right, the dates and times should represent when the files or folders were created. If the line is a folder, you’ll see the “<DIR>” notation. If the line is a file, the file size in bytes will be shown, along with the folder or file name on the far right. At bottom, a summary line will be displayed.

I need somebody (Help!) Not just anybody

Like any programming language, there are ton of commands, options, functions – you name it! For command line commands, just add help to your command, and when you run help + command, you should get back instructions and help on parameters for the command.

Helpers: Parameters in Basic Command Line Commands

Parameters are the first building block to doing fun things with basic command line commands. Parameters may also be known as options or arguments, depending on the setting.

Parameters can take the shape of words, symbols or letters, or sometimes a mix of these. Adding parameters can help you customize the output of your commands in command line.

Google PageSpeed Insights on Command Line (CLI)

Welcome back to more in adventures in Command Line! Previously, we were looking at running Google Lighthouse programmatically.

Today, we’re going to explore a similar exercise- running Google PageSpeed Insights on Windows Command Line (CLI)! Running PageSpeed insights on Command Line was probably the easiest self-coding experience I’ve had to-date. (More on the hard part later. 🙂 )

3 Prep Steps for Running PageSpeed Insights on Command Line

Unlike Screaming Frog Command Line automation, there are very few parameters or options to mess with for running data on a single URL. To run a single URL on PageSpeed Insights from Command Line:

  • Ensure CURL works in your command line (test CURL https://www.sampledomainhere.com) and check that the HTML source code is returned
    • Most recent Windows machines should already have CURL ready out of the box for CLI usage
  • Visit the Google PageSpeed Insights API documentation page
    • Get an API key (free) and create a project for credentials (also free) if needed
  • Copy the code sample into a code editor or text editor

Running Your First URL on PageSpeed Insights CLI

Is CLI for PageSpeed insights easy? Yes!

Effortless? No.

Here’s the out-of-the-box code sample from Google:

curl https://www.googleapis.com/pagespeedonline/v5/runPagespeed?url=https://developers.google.com

If you try to run that out of the box on Windows, you’ll like run into one or more of these errors:

  • 429 error: Too many requests / exhausted
  • Invalid syntax: Command(s) not recognized

First, you’ll need to append your API key to the end of the URL.

curl https://www.googleapis.com/pagespeedonline/v5/runPagespeed?url=https://www.zldoty.com&key=yourAPIKeyHere

Here’s what did the trick for me: placing escape characters in front of any special operators. Specifically, the ampersand (&) and equals (=) signs have special functions in CMD that need to be escaped with a carrot (^).

curl https://www.googleapis.com/pagespeedonline/v5/runPagespeed?url^=https://www.zldoty.com^&key^=yourAPIKeyHere

Great, CLI Works! What’s next?

If everything works, you should see a flurry of JSON code scroll through the command prompt window – hooray! But here’s the rub, what do you with this information?

Some extra code will help us capture the output into a file:

curl https://www.googleapis.com/pagespeedonline/v5/runPagespeed?url^=https://www.zldoty.com^&key^=yourAPIKeyHere -o fileName.json

Adding -o for output and specifying a file name will populate a table in CLI during processing with download progress stats. After the file has downloaded (sample runs took about 18 seconds), you should be able to see it in the default CLI directory in Windows File Explorer. (Typically C:\Users\youUserName)

Stay tuned for some more updates on how to scale this process across multiple URLs and extracting insights from the JSON file!