Success! Now Check Your Email

To complete Subscribe, click the confirmation link in your inbox. If it doesn’t arrive within 3 minutes, check your spam folder.

Ok, Thanks

Command Line Corner

An archive of lesser known but useful command line commands shared in our weekly newsletter

cut

Use the cut command to parse characters or sections from the lines of a file and write them to the standard output. Cut also supports multiple options for parsing – cut by byte (-b), character (-c), or delimiter (-d).

In the example below, we use the delimiter option (-d) to extract the second section (-f) after the comma in each line of the file.

/$ cat example.txt
  This,Week
  in,Self-Hosted

/$ cut -d "," -f 2 example.txt
  Week
  Self-Hosted

fold

Use the fold command to wrap each line of a file based on a specified number of characters when viewing the output from a display with a limited width. By default the command wraps lines every 80 characters, which can be configured with the -w flag (-w50 for 50 characters, etc.). Adding the -s flag also ensures words are not broken as the command runs.

/$ cat example.txt
This Week in Self-Hosted is a weekly newsletter published every Friday morning with a recap of the latest and greatest in self-hosted news, software, and content.

/$ fold example.txt
This Week in Self-Hosted is a weekly newsletter published every Friday morning w
ith a recap of the latest and greatest in self-hosted news, software, and conten
t.

/$ fold -w75 -s example.txt
This Week in Self-Hosted is a weekly newsletter published every Friday
morning with a recap of the latest and greatest in self-hosted news,
software, and content.

watch

Use the watch command to monitor changes to the output of another command over intervals of time. This is useful when monitoring things like script results, disk usage, and other tools that print a response as an output.

The command also comes with a number of helpful options including specifying an interval other than the default two seconds (-n), highlighting the differences between outputs (-d), or terminating the watch command when the output changes (-g). If not configuring the command with an option to automatically stop, use Ctrl + C to manually exit.

/$ watch date

Fri Feb 2 07:30:00 EST 2024
                 2

time

Prefacing a command with time outputs the amount of time it takes to execute. This is useful for measuring and optimizing the performance of scripts, downloads, and other tools.

The output of time consists of three values: the real time elapsed, the CPU time in user mode, and the CPU time in kernel mode.

/$ time python3 generate-rss.py
  real   0m8.210s
  user   0m1.779s
  sys    0m0.327s

yes

In Linux, the yes command outputs a string of characters until stopped ('y' by default if a string isn't provided). This is helpful when executing recurring tasks or writing scripts that include commands with prompts for user input.

And while this can be useful for those times you'd like to step away while a process runs, there's a reason many commands require manual user input – so be cautious when using yes to automate those you aren't familiar with.

/$ yes This Week in Self-Hosted
  This Week in Self-Hosted
  This Week in Self-Hosted
  This Week in Self-Hosted
  This Week in Self-Hosted
  ...
/$

/$ yes | bash docker_restart.sh
  Restart Docker service? y
/$

comm

Use the comm command to compare two sorted files line-by-line directly from the terminal. As shown below, the output of the command includes three columns – the first for unique lines from the first file, the second for unique lines from the second file, and the third for shared lines between the two files.

The command can also be used with various flags to alter its output (-1, -2, -3 to suppress the various columns, -output-delimiter to separate columns with a specified character, -check-order to ensure the files are sorted correctly).

/$ comm example-1.txt example-2.txt

                            This
                            Week
                            in
Self-Hosted
              Open-Source

tee

The tee command allows users to write the output of a command to a file while also viewing it in the terminal. In the example below, I'm viewing the contents of a file (cat example.txt) while also using tee to write the output of cat example.txt to another file (output.txt).

/$ cat example.txt | tee output.txt

This
Week
in
Self-Hosted

/$ cat output.txt

This
Week
in
Self-Hosted

z-commands

Certain popular commands (cat, grep, less, etc.) can be prefaced with the letter z to allow command line users to interact with gzip-compressed files without extracting them. This is helpful, for example, if you're reading compressed logs in a directory you don't have permissions to write/extract to.

/$ cat example.txt

This Week
in Self-Hosted

/$ gzip example.txt
/$ zcat example.txt.gz

This Week
in Self-Hosted

dos2unix

Occasionally, I'll struggle running commands against a text file only to discover it was created on Windows and includes carriage returns at the end of each line. To quickly convert a file to Unix-compatible line breaks (line feeds) from the command line, install and run dos2unix.

(Note that dos2unix probably won't be installed on your machine by default, so look up the installation instructions for your operating system before executing.)

/$ dos2unix this_week_in_self_hosted.txt
dos2unix: converting file this_week_in_self_hosted.txt to Unix format...

Ctrl + e

Use Ctrl + e to relocate your cursor to the end of the line when typing a long command. The shortcut is much quicker than holding an arrow key and easier than using the Home key as your fingers won't need to leave the default typing position.

/$ dcker co|mpose up ghost-blog
/$ dcker compose up ghost-blog|

Ctrl + a

When typing a long command, use Ctrl + a to relocate your cursor back to the beginning of the line if you find yourself needing to fix an error or typo. The shortcut is much quicker than holding an arrow key and easier than using the Home key as your fingers won't need to leave the default typing position.

/$ dcker compose up ghost-blog|
/$ |dcker compose up ghost-blog
/$ do|cker compose up ghost-blog

find -size +100M

The command find -size +100M finds all files in the working directory greater than or equal to 100MB. Adjust 100M to your liking if you'd like to search for files of a different size (for example, 1G for 1GB).

/$ find -size +100M
   ./this-week-in-self-hosted.db
   ./this-week-in-self-hosted.log

du -h --max-depth=1

The command du -h --max-depth=1 allows users to list the directories in a folder while displaying their sizes in a human-readable format. Breaking it down into individual pieces: du lists directories and sizes within the current directory, -h converts the displayed sizes to human-readable format, and --max-depth=1 defines what folder level the command runs (all subfolders will be displayed if omitted).

/$ du -h --max-depth=1
   16M ./This
   27M ./Week
   756k ./In
   1.1G ./Self-Hosted

> file.txt

Use the command > file.txt to flush the contents of a file from the command line (replacing file.txt with the name of your own file) – helpful for clearing old log files when troubleshooting via CLI.

/$ cat example.txt
   This Week in Self-Hosted 
/$ > example.txt
/$ cat example.txt
/$

!$

The characters !$ can be used as a shortcut to reuse or repeat the argument used in the most recent command. For instance, they can be used to easily navigate to a folder path that was recently created or modified.

/$ mkdir /this/week/in/self/hosted
/$ cd !$
    cd /this/week/in/self/hosted
/this/week/in/self/hosted$ 

who -b

The command who -b displays the date and time of the machine's last reboot:

$ who -b
    system boot 2023-10-27 05:34

echo "!!"

The command echo "!!" creates a script of the previously executed command – helpful for capturing complex commands that will need to be reused in the future.

$ rsync -av --delete /home/appdata /backup

# Capture the previous command in a file named 'selfhost.sh' in the current working directory
$ echo "!!" > selfhost.sh

# List the newly-created file to confirm it was created
$ ls
    selfhost.sh

# Open the file to confirm its contents
$ nano selfhost.sh
    rsync -av --delete /home/appdata /backup

# Use the script file to easily execute the saved command in the future
$ bash selfhost.sh

mkdir -p

Appending the -p flag (--parents) to the mkdir command allows users to create entire directory trees at once rather than using multiple mkdir commands to create nested folders:

mkdir -p personal/storage/photos

nl

Use the command nl to preface each line with a number when viewing the contents of a file. This is particularly helpful when trying to reference lines in a large file.

user@selfhst:/$ cat example
   This
   Week
   in
   Self-Hosted
user@selfhst:/$ nl example
   1  This
   2  Week
   3  in
   4  Self-Hosted

tac

Use the command tac (cat backwards) to view the contents of a file in reverse order. This is particularly helpful when viewing logs, as the most recent writes are typically appended to the end of the file.

user@selfhst:/$ cat example
   This
   Week
   in
   Self-Hosted
user@selfhst:/$ tac example
   Self-Hosted
   in
   Week
   This

Fixing errors with ^typo^correction

Use ^typo^correction to fix mistakes in previous commands without having to retype the entire command from scratch:

user@selfhst:/$ cd /home/usr/appdata
   -bash: cd: /home/usr/appdata: No such file or directory
user@selfhst:/$ ^usr^user
user@selfhst:/home/user/appdata$ _

Prefacing commands with <space>

For those with something to hide, prefacing commands with a single space in the command line prevents them from being recorded in the terminal's history:

$ ls -l
$  cd..
$ cd /
$ history

  10 ls -l
  11 cd /

curl ifconfig.me

Displays your network's external IP address in the terminal.

$ curl ifconfig.me

xx.xxx.xx.xxx

sudo!!

Prefaces the previously executed command with sudo to avoid having to retype the entire command a second time:

$ apt update

E: Could not open lock file ...

$ sudo !!

sudo apt update
[sudo] password for user: