Overview:
The Linux command line interface (CLI) is a powerful and flexible environment for computing. As a beginner, it may initially feel unintuitive—full of strange incantations and scrolling walls of text. This is completely normal and part of the learning process.
Once you’ve picked up some foundational commands like cd
, ls
, cat
, cp
, mv
, and nano
, and are comfortable navigating the filesystem, you may notice you’re repeating certain tasks over and over. This is where the CLI truly shines, through automation and scripting.
This article introduces a core CLI philosophy and real-world use cases where automation can improve efficiency. Hopefully, these examples will inspire you to reflect on your own workflows and identify opportunities to simplify or automate your routine tasks.
CLI vs GUI:
Graphical user interfaces (GUIs) are often more intuitive for first-time users, but they come with limitations—particularly when tasks need to be repeated or scripted.
For example, suppose you need to remove all lines starting with a #
from a configuration file. In a GUI text editor, you'd open the program, load the file, find a regex tool, enter a pattern, and execute it. (or worse, delete the lines by hand! yikes!)
In a CLI, a single command can do the same job quickly and reproducibly. Even better, once you know the command, you can wrap it in a script to easily use it again in the future. That’s the power of the CLI: anything you can do by hand, you can later automate.
An example: catmeat
Let’s revisit the task of removing comments from a file. Specifically, lines starting with #
or that are blank.
In something like Notepad++, you might use a regular expression like:
- find: ^#.*\R?
- replace: [empty]
To also remove blank lines, you'd probably use a second regex operation for readability.
The Linux incantation for these two patterns might be to use cat and egrep:
cat /path/to/file.conf | egrep -v "^\s*($|#)"
If you only need this once, you might ask a colleague, copy-paste from the Internet, or ask an LLM. If this is a frequent task, once you've learned and understood it, it’s potentially worth scripting.
In S&E, we wrote a small utility called catmeat
. It's like cat
, but it only shows the “meat” of a file:
#!/bin/bash
# Print help text if no arguments are given
if [ -z "$1" ];then
echo "No file/comment pattern specified, usage:"
echo "catmeat /path/to/file [DELIM]"
echo "eg."
echo "catmeat /etc/fstab #"
exit 0
fi
# Default to '#' if no delim is given
if [ -z "$2" ];then
DELIM="#"
else
DELIM="$2"
fi
# Perform the incantation and output to stdout
cat $1 | egrep -v "^\s*($|$DELIM)"
By encapsulating this basic task in a script, it can be quickly run by just invoking catmeat from the terminal:
catmeat /path/to/file.conf
An example: clobberpasta
Another common scenario for us is updating bespoke scripts or config files, especially in isolated environments where network transfers aren’t possible. When testing changes, especially in research or benchmarking workflows, it's essential to preserve a clean rollback path.
A typical safe update process might look something like the following:
- Make a backup of the original file
- Rename the backup file with a datestamp
- Move the copy to a uniform location
- Remove the original file
- Open a text editor interactively, waiting for a user to paste a new copy of the file
- Upon exiting the editor, set the permissions of the new file to match the original's
To streamline this workflow, some of our environments include a small script called clobberpasta
. It automates the process of “clobbering” a file and preparing for a pasted replacement, following the rough workflow above.
If you’re on one of these systems, try locating it with:
which clobberpasta
Then inspect the script to see how it works, or try using it directly and see what it does. It’s a great example of how small utilities can smooth out repeated admin tasks.
The Linux CLI can feel daunting at first, but once you get comfortable, it becomes a powerful tool for automation and efficiency. Look for opportunities in your day-to-day tasks to script and simplify. Ask yourself, what do you find yourself doing often? You don’t need to be a seasoned programmer to write useful scripts; just a few lines of Bash might save dozens of hours in aggregate.