GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — knowledge/unix.md at master · AndCollectiom/knowledge

Expand file tree

/

unix.md

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
256 lines (212 loc) · 5.83 KB
/

unix.md

Copy path

File metadata and controls

256 lines (212 loc) · 5.83 KB
Raw
Copy raw file
Download raw file
Outline
Edit and raw actions

sh

Shell scripts and tools.

man pages

  1   User Commands
  2   System Calls
  3   C Library Functions
  4   Devices and Special Files
  5   File Formats and Conventions
  6   Games et. Al.
  7   Miscellanea
  8   System Administration tools and Deamons

File testing in sh

-b filename     block special file
-c filename     special character file
-d dirname      check for directory existence
-e filename     check for file existence
-f filename     check for regular file existence not a directory
-G filename     check if file exists and is owned by effective group ID.
-g filename     true if file exists and is set-group-id.
-k filename     sticky bit
-L filename     symbolic link
-O filename     true if file exists and is owned by the effective user id.
-r filename     check if file is a readable
-S filename     check if file is socket
-s filename     check if file is nonzero size
-u filename     check if file set-user-id bit is set
-w filename     check if file is writable
-x filename     check if file is executable
-z <string> ... true if the length of the string is non-zero

example

#!/bin/bash
file=./file
if [ ! -e "$file" ]; then
  echo "File does not exist"
else
  echo "File exists"
fi

Pipe stdout to multiple commands

$ cat file.txt | tee >(pbcopy) >(do_stuff) >(do_more_stuff) | grep errors

Find and replace in multiple files

$ ag -l <pattern> | xargs sed -i '' -E 's/<old>/<new>/g'

Delete a range of lines

$ cat file.txt | sed -e '1,2d'

Manipulate columns with awk

$ cat file.txt | awk '{$3=$1; gsub(/0[12345]_/, "", $3); $2="|"}{print}'
  • source

Check for value, fill in if it doesn't exist

$ screen_width=${COLUMNS:-$(tput cols)}

Connect to ssh server

ssh -i <path/to/file> <name>@<ip>

or with a ~/.ssh/configfile

ssh <Host>

list all open files for user

lsof -u <ownername>

named pipes / inter-shell communication

Named pipes are cool to create background processes with that can be addressed by name to do stuff with. It creates a physical file on the system that can be used from any shell process. The code below passes the output of pipe to cat, which then writes to out.txt. When passing in a command to pip (ls -la in this case), it pops back out at the other side.

$ mkfifo pipe
$ cat < pipe > output.ext
$ ls -la > pipe

follow logs as they grow

$ tail -r <file>

execute a command in npm module dir

npm ex <module name> <command> ... execute a command in the module dir

dig

DNS lookup utility

# example
$ dig @127.0.0.1 -p 5000 something.foo +short
1.1.1.1

vifm

Vim like file manager. Useful to do bulk directory operations. Offers different views on files.

smxi/sgfxi

System / gfx configuration tool.

  • website

refind

boot manager

  • website

lspci

list all pci devices

  • man page

lsblk

List all available devices. Useful to determine how to partition.

chroot

Can be used to repair machines that have lost root access / are unbootable for other reasons. Live cd's ftw! Also used to reset the pid of a tree of processes, a commonly known technique used with docker.

  • arch wiki

manage audio players

$ playerctl

pipe stderr to stdout

# bash
$ <command> 2>&1 /dev/null

# POSIX sh
$ <command> >/dev/null 2>&1

print multiline string

cat << EOF
  oh my, such nice text
EOF

detect if script is sourced

if [ "$_" = "$0" ]
  then echo 'yup, script is directly called'
  else echo 'nope, script is not directly called'
fi

Switch statement

case $1 in
  "")         usage; exit 1 ;;
  -h|--help)  usage; exit ;;
  -l|--link)  link "$@" ;;
  *)          readonly name=$1 ;;
esac

Format text to be <80 chars

$ fmt -80

Create random file name

$ echo $RANDOM

Math in shell

In shell it's not recommended to use expr or bc, instead use:

foo=5
res=$((foo - 2 / 3))

Prepend zeroes / add leading zeroes

printf "%02d" 4
# => 04

Shell traps

Cleaning up after yourself is essential in programming. The shell trap command listens to a POSIX signal, and then runs a command. It's event-driven programming in Shell!

warning: trap cannot process SIGKILL, as is the nature of the signal. Use kill -9 as a last resort.

Here's an example of trap in action:

readonly TMP_FILE='/tmp/bar.file'

# delete the file if the program is prematurely ended
trap "rm $TMP_FILE; exit" SIGHUP SIGINT SIGTERM
pr $1 > "$TMP_FILE"

source

Renaming files

Renaming is made easy using the rename(1) command. Just plop in a regex and the renaming is done for you:

# Change foo to bar in matching filenames
$ rename 's/foo/bar/' *.txt

# Convert to lower case
$ rename -c *.txt

# Replace whitespace with underscores
$ rename 's/\s+/_/g' *.txt

# No action, just show what renames would occur
$ rename -n 's/foo/bar/' *.txt

Init

The init process is the heart of the OS. It schedules everything that happens on top of the kernel.

See Also

  • cleaning an arch installation
  • the art of the cli
  • awesome shell
  • unofficial bash strict mode
  • uselessness of cat
  • stronger shell
  • the bash guide
  • the art of the command line
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.