Master Linux CLI (part I). Useful Linux commands.


Interestingly, when you start reading books about Linux or going through different tutorials, they often don’t tell you about some of the cool commands that make your work sometimes so much easier. Maybe they are hiding them from you to make sure you still have to learn something in the future? ¯\_(ツ)_/¯

Anyway, today I decided to make a quick overview of some of the commands which I find useful, but which are sometimes hard to find out about.

tee

tee command allows you to write to the stdout and a file (or files) at the same time.

This is useful when you want to store and view the output of any command.

200x200

And you can also use it to save the output to multiple files.

200x200

This command is incredibly useful when you want to store the output of a command to a file but also redirect it as an input to another command.

200x200

As you can see, we are able to take snapshots of the data as it flows through the pipes.

pbcopy (Mac) or xclip (Linux)

This allows you to copy a file’s content to the clipboard.

200x200

Now if you try to paste, you’ll get cucaracha, which by the way means a cockroach in Spanish :)

This command makes copying from the terminal a breeze. I find it especially useful when I need to copy SSH or GPG keys.

watch

watch runs a specified command repeatedly at regular intervals and displays its output on a console.

This is used when you need to continuously monitor some command’s output.

Simple examples include monitoring who is logged in to the system with watch who command or watching for changes inside a directory with watch ls

We should note the -d option that allows you to highlight the changes that happen in the command’s output.

Just to show you how it works, we’ll use it with a date command.

script & scriptreplay

These are 2 awesome commands which allow you to record and replay a shell session for you.

To use a script command, we can just type script in which case the session will be stored in a default file named typescript. We can also specify the name of the file in which we want to store our session as the first argument to the script command.

An alternative to the script command is history, but it only keeps track of the commands you use and not their outputs.

Another cool thing about the script command is that the shell session that you have recorded can then be replayed in your terminal with a scriptreplay command. This is particularly helpful if during the session you start interacting with some programs like htop.

To be able to replay a recorded shell session, we need to specify a filename for storing the timing information.

$ script --timing=time.txt myshell.log

Then after we’re done recording, we can use the scriptreplaycommand to replay the session.

$ scriptreplay --timing=time.txt myshell.info

It’s also important to note the -c option to this command which allows to record the output of a single command. For example, this might come in handy when we need to record a command’s output in our bash script. The syntax goes like this:

$ script -c 'ping -c 3 google.com' myshell3.log

jq

jq is a handy JSON processor that allows you to extract necessary fields from a JSON object.

Let’s take a simple JSON file and extract some specific fields.

{
  "colors":
    {
      "color": "black",
      "category": "hue",
      "type": "primary",
      "code": {
        "rgba": [255,255,255,1],
        "hex": "#000"
      }
    }
}

200x200

env

If being run without any arguments, this command will show you a list of current environment variables.

It also allows you to run commands with specific environment variables without actually changing your environment.

This can be helpful when you need to run a one time command that requires some specific env variable, but you don’t really want to change your environment. For example, to build a GO binary for Ubuntu on my Macbook I would run a command like this.

$ env GOOS=linux GOARCH=amd64 go build src/hello-world.go

This runs the go build command with two additional environment variables. If I run env command right after that, I won’t find those variables in my environment.

Hopefully, you’ve found this post useful. And if you have some interesting commands to share with me, please leave a comment below!