Spool Five

Shell Commands

Aug 8, 2021

This is a note on some of the commands I have found useful and want to remember.

Setting the default shell

List all available shells, then set the default shell with chsh

cat /etc/shells

# Example output:
# /bin/bash
# /bin/csh
# /bin/dash
# /bin/ksh
# /bin/sh
# /bin/tcsh
# /bin/zsh

chsh -s /bin/zsh

popd and pushd

Exclamation mark !

A very useful command! I mainly just use the first two points below a lot.

tail

Useful for long log files, etc.

# Show last 8 lines in file:
    tail -n 8 path/to/file

# Print a file from a specific line number:
    tail -n +8 path/to/file

# Show last `count` lines in a file and refresh every =seconds" seconds:
    tail -n count -s seconds -f path/to/file

Find & Replace across multiple files

grep -rl "target_string" . | xargs sed -i 's/target_string/new_string/g'

What is happening:

grep -rl: search recursively, and only print the files that contain “old_string” xargs: take the output of the grep command and make it the input of the next command (ie, the sed command) sed -i ‘s/old_string/new_string/g’: search and replace, within each file, old_string by new_string

Alternative: I used this to replace all the footer email links in my blog directory. Seemed to work perfectly. The section included with find makes sure to skip the git directory.

find . \( -type d -name .git -prune \) -o -type f -print0 | \
    xargs -0 sed -i 's/target_string/new_string/g'

less - Exclude Lines from View

Once your file is open in less (or journalctl) press the following keys:

Ampersand opens the pattern matching mode, exclamation mark tells less to exclude the following part, and then you enter your search term.

shuf

Shuffle lines

shuf /path/file

shuf --input-range=1-100

# Random number between 1 and 100
shuf --input-range=1-100 | head -1

sort

sorts alphabetically, use -n flag for numeric sorting.

For example:

sort /path/file

# Sort using only unique lines
sort -u /path/file

# Sort a file in place (overwrite)
sort -o /path/file /path/file

uniq

Print unique lines.

Processes

Some of my most used commands

Disk Usage

Random Note