Unix

Frequently Forgotten Useful Unix Commands

Linux-specific

Check which system running on a Linux machine

cat /etc/os-release

Use uname to print system information

$ uname -a
Linux kiewit 5.15.76+ #1597 Fri Nov 4 12:11:43 GMT 2022 armv6l GNU/Linux

Mounting an HFS device in linux

Attached HFS formatted disk

~$ lsblk -o KNAME,TYPE,SIZE,MODEL

sdi       disk     1.8T Portable        
sdi1      part     1.8T 

Need hfsprogs...

sudo apt-get install hfsprogs

Then mount the drive:

sudo mount -t hfsplus -o force,rw /dev/sdXY /media/mntpoint

made mount point /media/usb

sudo mount -t hfsplus -o force,ro /dev/sdi1 /media/usb

Copied data to /dev/is3 and unmounted drive.

sudo umount /media/usb

KERNEL

lsmod list kernel modules installed (Linux) dmesg print or control the kernel ring buffer ("kernel log buffer")

Disk info

du
du -h --summarize *

Git

git
git pull
git commit -m TEXT
git push
git add FILENAME

git log shows the commit logs - useful!

login and getty

getty

start a getty login on the serial port (Linux)

sudo systemctl enable getty@ttyAMA0.service

sudo systemctl start getty@ttyAMA0.service

For a teleprinter, use the following /etc/systemd/system/getty@ttyAMA0.service.d/override.conf

$ cat override.conf 
[Service]
Type=simple
ExecStart=
ExecStart=-/sbin/agetty -i --nohostname --autologin EMF --noclear ttyAMA0 110 tty33

login through screen (macOS, see DECWRITER log)

screen /dev/tty.usbserial-FTFXS2KI 300
C-\ : exec ::: /usr/libexec/getty dw2

macos

suppress the bash login warning on macOS

export BASH_SILENCE_DEPRECATION_WARNING=1

Images

Strip the EXIF data from an image:

mogrify -strip *.JPG

Network

ifconfig configure a network interface

ipconfig?

ip a network interface information (Debian)

SERIAL stty settings for teleprinter to Raspberry Pi 2 (retrolab) $ cat ~/bin/setstty

  1. !/bin/bash
  2. stty lcase

stty ofill cr1 nl1 cols 79 stty cstopb parenb -parodd cs7 stty iuclc iexten stty -iutf8 stty brkint
 SFTP get -r * recursively get files get -p preserve modification times, access times, and modes from the original files transferred.

SSH set up ssh-key on a machine:

	ssh-keygen -t rsa
	no passphrase? less secure

copy to the target machine:

	ssh-copy-id target.machine.hostname

USB lsusb list USB devices (Linux)

TEXT Add spaces at the beginning of each line in a text file: sed 's/./ /'

cut -c 1-10 FILE cut columns of a file paste merge corresponding or subsequent lines of files column

rev reverse lines of a file

expand convert tabs to spaces

fold -w nn fold long lines for finite width output device fold -s -w nn fold long lines for finite width output device, break at spaces

remove trailing space

HEXDUMP

Note that hd = hexdump -C (canonical)

One byte per line:

	hexdump -v -e '/1 "%02X\n"' FILE

8 hex bytes per line:

	hexdump -e '8/1 "%02X ""\n"' FILE

alias hda='hexdump -e '\"%3_ax:"" "'\ -e '\16/1 "%02X "'\ -e '\"\n"'\' alias hdna='hexdump -e '\16/1 "%02X "'\ -e '\"\n"'\'

FUN TRICKS

speak a weather report:

	curl -s https://tgftp.nws.noaa.gov/data/forecasts/zone/pa/paz071.txt  | sed '1,14d;s/^\.//' | say

	curl -s https://tgftp.nws.noaa.gov/data/forecasts/zone/ca/caz006.txt | sed '1,22d;s/^\.//;s/\$\$//' | say 

piping

piping a command to another command that accepts stdin.

	#!/bin/bash
	[ $# -ge 1 -a -f "$1" ] && input="$1" || input="-"
	cat $input

[ $# -ge 1 -a -f "$1" ] - If at least one command line argument ($# -ge 1) AND (-a operator) the first argument is a file (-f tests if "$1" is a file) then the test result is true.

example:

	date | myscript

I use this for the command "tc", which tweets a command's output from the prompt.

wget

Mirroring a website

wget --mirror --convert-links --html-extension --wait=2 -o log http://website.org

wget --mirror --convert-links --html-extension --wait=2 -o log http://website.org

--mirror

    Turn on options suitable for mirroring. This option turns on 
    recursion and time-stamping, sets infinite recursion depth,
    and keeps FTP directory listings. It is currently equivalent to 
    ‘-r -N -l inf --no-remove-listing’. 

--convert-links

    After the download is complete, convert the links in the document
    to make them suitable for local viewing.

--html-extension

-o foo

    write "log" output to a file named "foo"

--wait=seconds

    Wait the specified number of seconds between the retrievals.
    Use of this option is recommended, as it lightens the server load 
    by making the requests less frequent.

Spotlight

When spotlight dies:

	killall Spotlight

USERS

sudo usermod -aG sudo USERNAME Add users to sudo group. (To get this to work, I had to use su and sudo together.)

RENAME FILES

for f in fgh*; do mv "$f" $(echo "$f" | sed 's/^fgh/jkl/g'); done

pr Format an ASCII printout Good for file listings on daisy wheel printers...

print a program listing

fold -w 131 script.js | pr -h "script.js" | lp -d qume

May want to use the -F option to replace CRs with form feeds.

The qume printer can choke on HT characters. Expand these with the 'expand' command:

grep OR script.js | tr ',' '\n' | pr -4 -w 157 -e -h "when-machines-think vocabulary" | expand | lp -d qume

enscript

default media for enscript is A4. It can be changed with the -m flag. enscript configuration file is: /usr/local/Cellar/enscript/1.6.6_1/etc/enscript.cfg

$ enscript --list-media known media: name width height llx lly urx ury


Letterdj 612 792 24 40 588 768 A4dj 595 842 24 50 571 818 Letter 612 792 38 24 574 768 Legal 612 1008 24 24 588 984 A5 420 595 24 24 396 571 A4 595 842 24 24 571 818 A3 842 1190 24 24 818 1166

I added Wide to ~/.enscriptrc:

Media: Wide 1071 792 54 0 1071 792

There are 72 postscript points / inch

use:

enscript -c -B -M Wide -f Courier12 FILENAME.TXT