dark mode light mode Search Menu
Search

Command Line Interface (CLI) Part II

Juan Lupion on Flickr

Last time we spent some time getting comfortable with the command line and using a terminal. Here, we’ll be continuing where we left off with learning how to use the command line interface to control your computer.

We’ll be discussing a few common built-in programs that come with the terminal, how to combine commands together, how to redirect the output of a command into a file, and just exactly how the terminal even finds commands and programs in the first place. Finally, we’ll have our first little taste of writing our own terminal commands by doing some bash scripting.

To follow along, first open up a terminal on your computer. Now try typing nano and hitting enter. You’re actually now in a tiny text editor and it should look something like the following:

Go ahead and type a little bit of text and hit Ctrl-X to exit and save. You’ll be prompted to give it a filename. You can actually call it anything, but I’ll call it test.txt. Nano is a convenient little program for doing quick edits on the command line, especially if you’re remotely controlling another computer over SSH, but that’s a topic we’ll cover in a future article.

Now, type less test.txt, where test.txt should be replaced by whatever you you called this file and you should see the text you just typed. Go ahead and hit q to exit.

Less is a program for viewing files, not editing them. It doesn’t just work with files, though, as you’ll see if you type:

ls | less

In my default (or home) directory it looks like:

You can scroll up and down with the arrow keys. This is the best way to look at all the files in a large directory from the command line. It’s also our first example of what’s called a pipe in command line programming. Pipes are a way of taking the text that would be produced by the program, its output, and feeding it to another program just like you would a file.

Here’s another example of pipes with the find command this time:

find . -name "*.py" | less

You can read this by calling find in the current directory (.), and look for files that end in “py” (-name “*.py”, remembering that the * stands for anything), and then send the output to less.

This will find all python source code files visible from the directory you’re in or any of its subdirectories. If your computer is anything like mine, you get a lot of files back that you can scroll through.

How many files, though? Well, first notice that the output of find puts each result on a different line. There’s a command called wc, which stands for word count, that can count—among other things—the number of lines in the input it receives.

So if we type:

find . -name "*.py" | wc -l

When we hit enter, a second or so later you should see the number of files that end in .py. In my case it was 2019. Wow.

The find command can actually do a lot of cool things and we link below to an article that lists a lot of tricks you can pull with it.

Now, let’s say that we want to run our special find command over and over again but don’t really want to type it in full every time. We can write a bash script that will run this command. Bash is, in a sense, what we’ve been calling “the command line”. It’s the underlying program that takes in the input we type into the terminal and then executes it.

So to start, type nano findPy.sh and type the following before you hit Ctrl-X to exit:

#!/bin/bash
find . -name "*.py"

From here, you need to tell the command line that this file is a program it should run, which you can do by typing chmod +x findPy.sh. This tells the computer that the file findPy.sh is “eXecutable”.

Finally, to run your bash script, type:

./findPy.sh

The one catch is that you’ll only be able to run this file by referencing its exactly location. You want to run it from anywhere without thinking about where it lives. That means you need to add the file to the path that bash uses to find commands to run.

In your home directory, which you can always get to by just typing cd and hitting the “enter” key, there should be a file called .bashrc. Open it up in nano and take a look. At the bottom of the file you’ll want to add the line:

PATH="$HOME/scripts":$PATH

Every time you type something on the command line bash looks up the first word to see if it’s a command or program it can find in its path. Here, we just told bash to first look in the scripts subdirectory of your home directory for a program with that name. If it doesn’t find it, it goes on to look in the rest of the path.

All that’s left to do is move your findPy.sh file into the scripts directory and then open a new terminal. If you type find and hit TAB you should see findPy.sh as an option.

This was just a taste of what you can do programming in bash. With all the tools you have so far you should be able to write a script that backs up a directory you’re working on when it’s run. Just be sure to use cp and not rm so that you don’t erase the original files!

Learn More

Examples of the “find” command

https://www.tecmint.com/35-practical-examples-of-linux-find-command/

A long-form tutorial on bash scripting

http://tldp.org/LDP/abs/html/

Another longer guide to bash scripting

https://guide.bash.academy/

An alternative to bash

https://rootnroll.com/d/fish-shell/

Related Posts