Subscribe to our newsletter

For the latest in self-hosted news, software, and content delivered straight to your inbox every Friday

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

A collection of useful Linux commands shared in This Week in Self-Hosted

Command Line Corner

A collection of useful Linux commands shared in This Week in Self-Hosted

Alt + .

Press the . key while holding Alt to cycle through and reuse arguments from previous commands:

/$ docker compose up -d some-docker-container
/$ docker compose stop <Alt .>
/$ docker compose stop some-docker-container

>

Preface a filename with > to clear the contents of the file directly from the command line:

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

factor

Use the factor command to list prime factors for a specified number directly from the command line:

/$ factor 14
  14: 2 7
/$ factor 46
  46: 2 23

mkdir -p

Add the -p flag (--parents) to the mkdir command to create a directory of new folders in a single command instead of repeating it multiple times for each new folder.

Users can also utilize brace expansion with mkdir -p to simultaneously create multiple directories of folders with a similar structure.

/$ mkdir -p /this/week/in/self-hosted
/$ tree
  .
  |___this
      |___week
          |___in
              |___self-hosted
/$ mkdir -p this/{day,week,month}/in/self-hosted
/$ tree
  .
  |___this
      |___day
          |___in
              |___self-hosted
      |___week
          |___in
              |___self-hosted
      |___month
          |___in
              |___self-hosted

column

Use the column command to view the contents of a file in an organized table or column format with options for specifying delimiters, width, names, alignment, and more.

In the example below, column is being used to display the example.txt file using a comma as the delimiter for the table.

/$ cat example.txt
  Task,Completed (Y/N)
  Research,Y
  Setup,Y
  Deploy,N
  Analyze,N
/$ column example.txt -t -s ","
  Task       Completed (Y/N)
  Research   Y
  Setup      Y
  Deploy     N
  Analyze    N

Prefacing commands with <space>

Preface a command with a single space to omit it from being logged in the terminal's bash history:

/$ history
  1000 docker compose up
  1001 cd ~
/$ rm example.txt
/$ history
  1000 docker compose up
  1001 cd ~

rename

Use the rename command to easily rename multiple files (or file extensions) at the same time using patterns in the file names. Depending on your distribution, you may need to install rename (apt install rename) before using it.

/$ ls
  example-1.txt example-2.txt
/$ rename 's/example/test/' *.txt
/$ ls
  test-1.txt test-2.txt
/$ rename 's/.txt/.csv/' *.txt
/$ ls
  test-1.csv test-2.csv

cd -

Use the cd - command to easily navigate back to the previous directory when traversing folders:

/$ cd /some/path
/some/path$ cd /another/path
/another/path$ cd -
/some/path$ _

Ctrl + x + e

Ctrl + x + e is a useful shortcut for instantly firing up an editor from the command line. By default, the shortcut will open a blank editor – but its usefulness shines when entered along with a line of text, which will automatically appear in the first line of the editor after hitting Ctrl + x + e.

This is particularly handy when running complex commands that may need to be documented for future reference.

/$ _

GNU nano 7.2
_


/$ echo -e "correct\fhorse\fbattery\fstaple"

GNU nano 7.2
echo -e "correct\fhorse\fbattery\fstaple"_

cal

Use the cal command to display the current calendar month with the current date highlighted on the command line. The command can also be paired with flags for additional functionality: -y to show the entire year with the current date highlighted, -j to show the month using the Julian calendar, or with a two-digit month or four-digit year to specify a different point in time.

/$ cal

      February 2024
  Su Mo Tu We Th Fr Sa
               1  2  3
   4  5  6  7  8  9 10
  11 12 13 14 15 16 17
  18 19 20 21 22 23 24
  25 26 27 28 29

/$ cal 02 2023

      February 2023
  Su Mo Tu We Th Fr Sa
            1  2  3  4
   5  6  7  8  9 10 11
  12 13 14 15 16 17 18
  19 20 21 22 23 24 25
  26 27 28

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: