Skip to content
Skip to the article
In Linux: 9 articles
Linux

Shell One-Liners

Handy shell commands for in-place text replacement, fixing SSH home directory permissions, and reading the running kernel's build config.

Updated
Applies to
  • Ubuntu 24.04
  • Bash 5
Tags
  • linux
  • bash
  • sed
  • ssh
  • kernel
Reading time
2 min

Small commands that come up again and again on Linux servers. Each section stands alone.

Replace Text in a File

Replace every occurrence of a string in a file, editing it in place. This example renames the database user in a SQL dump's DEFINER clauses before importing it on a server where the user has a different name:

sed -i "s/'old_db_user'@'localhost'/'new_db_user'@'localhost'/g" backup.sql

Dumps often quote identifiers with backticks instead. Single-quote the expression so the shell doesn't interpret them:

sed -i 's/`old_db_user`@`localhost`/`new_db_user`@`localhost`/g' backup.sql

Tip

Add a suffix to keep a backup of the original, as in sed -i.bak ..., which writes backup.sql.bak first. To preview changes without writing anything, drop -i and pipe the output to diff backup.sql -.

Replace Text Across Many Files

Run the same substitution on every matching file under a directory:

grep -rl 'old.example.com' <DIR> | xargs -d '\n' sed -i 's/old\.example\.com/new.example.com/g'

Escape dots and other regex characters in the search pattern, as shown, so . doesn't match any character.

Fix SSH Home Directory Permissions

OpenSSH refuses key-based logins (with StrictModes, which is on by default) when the home directory, .ssh, or authorized_keys is writable by anyone other than the owner. Tighten all three:

chmod go-w <HOME_DIR>
chmod 700 <HOME_DIR>/.ssh
chmod 600 <HOME_DIR>/.ssh/authorized_keys
chown -R <USER>:<USER> <HOME_DIR>/.ssh

This also applies to site users whose home directory is the web root, such as /var/www/example.com. If logins still fail, check journalctl -u ssh for bad ownership or modes messages.

Read the Running Kernel's Build Configuration

Check how the running kernel was built, for example which compiler version or which options were enabled:

zgrep -i gcc /proc/config.gz

/proc/config.gz exists only if the kernel was built with CONFIG_IKCONFIG_PROC. Ubuntu and Debian ship the config as a plain file instead:

grep -i gcc /boot/config-$(uname -r)

To check a single option:

grep CONFIG_ZSWAP /boot/config-$(uname -r)

Sources

This article is in the public domain (CC0 1.0), code samples included. Use it however helps you.