Shell

October 4, 2019 Linux 6 minutes, 7 seconds

This setting allows you to enter shell commands quite efficient. You just enter the first couple of letters of a coomand and press page up/down. The shell tries to auto complete them based on your previous commands.

In /etc/inputrc

# alternate mappings for "page up" and "page down" to search the history
"\e[5~": history-search-backward
"\e[6~": history-search-forward

systemctl restart nscd

echo "line 1
line 2" >> multiple

or

cat <<EOT >> multiple
line 1
line 2
EOT

wget

--no-check-certificate

curl

--insecure

useradd -p $(openssl passwd -1 $PASS) $USER

echo "password" | sha1sum

usermod -a -G group user

chown --reference=otherfile newFile
chmod --reference=otherfile newFile
chown -h USER:GROUP SYMLINK

chmod g+s /path/to/parent

Auth

# Auth with username password and store cookie information
curl -c cookies.txt -X POST -d 'username=admin&password=admin' https://example.com/login
# Use cookie auth information to post data from json file
curl -b cookies.txt -d @data.json -H "Content-Type: application/json" -X POST https://example.com/rest
# Use auth token
curl -H "X-Auth-Token: <Token ID>" https://example.com/login

GET

curl -H "Accept:application/json" https://httpbin.org/get

POST

curl \
-h "Content-type: application/json" \
-X POST \
-d '{"title": "Test Title", "note": "Test note"}' \
https://httpbin.org/

gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=OPTION -dNOPAUSE -dQUIET -dBATCH -sOutputFile=output.pdf input.pdf

common options for dPDFSETTINGS are:

  • dPDFSETTINGS=/screen lower quality, smaller size.
  • dPDFSETTINGS=/ebook for better quality, but slightly larger pdfs.
  • dPDFSETTINGS=/prepress output similar to Acrobat Distiller "Prepress Optimized" setting
  • dPDFSETTINGS=/printer selects output similar to the Acrobat Distiller "Print Optimized" setting
  • dPDFSETTINGS=/default selects output intended to be useful across a wide variety of uses, possibly at the expense of a larger output file

(command1 &) && (command2 &)

ps -o pid,user,%mem,command ax | sort -b -k3 -r

su - targetuser -s /bin/bash -c "/bin/echo hello world" 

grep -v -F -x -f

gs -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -sOutputFile=merged.pdf *.pdf

pdfjam --nup 2x3 --frame true --noautoscale false --delta "0.2cm 0.3cm" --scale 0.95

sudo dmidecode | grep BIOS

echo $(curl -ss http://ipecho.net/plain)

route -n | awk '($1 == "0.0.0.0") { print $NF }'

ip addr show eth0 | grep "inet\b" | awk '{print $2}' | cut -d/ -f1
ifconfig eth0 | grep "inet addr" | cut -d ':' -f 2 | cut -d ' ' -f 1

# dpkg
dpkg-query -L <PACKAGE_NAME>
dpkg-deb -c <FILE-NAME>
apt-file list <PACKAGE_NAME> (package not installed)

# Pacman
pacman -Ql  <PACKAGE_NAME>

# RPM
rpm -ql <PACKAGE_NAME>

Basics e.g. modelnumber, manufactorer

dmidecode -t1

Ram slots

dmidecode -t memory

from here

  • zsh always sources '~/.zshenv'.
  • Interactive shells source ~/.zshrc
  • Login shells source ~/.zprofile and ~/.zlogin

  1. Interactive

    Interactive means that the commands are run with user-interaction from keyboard, so the shell can prompt the user to enter input.

  2. Non-interactive

    The shell is probably run from an automated process so it can't assume if can request input or that someone will see the output.

  3. Login

    Means that the shell is run as part of the login of the user to the system. Typically used to do any configuration that a user needs/wants to establish his work-environment.

  4. Non-login

    Any other shell run by the user after logging on, or which is run by any automated process which is not coupled to a logged in user.

ps axo user:20,pid,pcpu,pmem,vsz,rss,tty,stat,start,time,comm

wget \
     --recursive \
     --no-clobber \
     --page-requisites \
     --html-extension \
     --convert-links \
     --restrict-file-names=windows \
     --domains example.com \
     --no-parent \
         www.example.com/home/wiki/

zip -rP PASSWORD file.zip folder/

groupadd GROUP && useradd -g|-G GROUP user
  • lower g when group exists
  • optional -M for no home directory

php -m

/etc/php/php.ini

type ALIAS

convert -density 384 NAME.svg -define icon:auto-resize NAME.ico

I want to match not just only the program but its arguments (here for entr)

ps ax | grep "xelatex main.tex" | grep -v grep | awk '{print "kill -9 " $1}' | sh

This command recursively syncs two folders with advanced matching options I am running for syncing my documents to my eInk Reader.

rsync -rv \
--omit-dir-times \ # ignore timestamps
--prune-empty-dirs \ # don't create emtpy directories
--delete \ # also delete files at the destination
--exclude='dir1' --exclude='dir2' \ # exclude dir1 and dir2
--include='*/' \ # but include all other directories
--exclude='.*' \ # exclude all hidden files
--include='*.pdf' --include='*.epub' \ # include .pdf and .epub files
--exclude='*' \ # exclude all the rest
-e "ssh -p8022" \ # copy over ssh
source \ # source directory to sync
user@host:/destination # target directory on remote

  1. with builtin methods
    watch -n 3 'kill -USR1 $(pgrep "^dd$")'
  2. with package pv
    dd if=/dev/urandom | pv | dd of=/dev/null

    use pv -s 4G for ETA, here approximately 4G

  3. with new status
    dd if=/dev/urandom of=/dev/null status=progress

instead of

grep string1 file1 > result1
grept string1 file2 > result
diff result1 result2 

use <()

diff <(grep string1 file1) <(grep string1 file2)

ndb (network device block) module and qemu-nbd command is required!

#!/bin/bash

for i in "$@"
do

case $i in
    -m|--mount)
        echo "modprobe nbd, creating device and mounting"
        sudo rmmod nbd
        sudo modprobe nbd max_part=63
        sudo qemu-nbd -c /dev/nbd0 '~/path/to/disk.vmdk'
        echo "Device created"
        if [[ -f /dev/nbd0p1 ]]
        then
            sudo mount /dev/nbd0p1 ~/mounts/tmp
        else
            sudo partprobe /dev/nbd0
            sudo mount /dev/nbd0p1 ~/mounts/tmp
        fi
        echo "Mounted at ~/mounts/tmp"
        shift
        ;;
    -u|--umount)
        echo "Unmounting, deleting device and removing nbd module"
        sudo umount ~/mounts/tmp
        sudo qemu-nbd -d /dev/nbd0
        sudo rmmod nbd
        shift
        echo "Done"
        ;;
    \?)
        echo "invalid option"
esac

done