HowTo

http://www.panticz.de/find
http://www.panticz.de/rsync
http://www.panticz.de/sed
http://www.panticz.de/ssh
http://www.panticz.de/ip
http://www.panticz.de/apt
http://www.panticz.de/hardware

# remove multiple blanks and tabs
cat in.txt | sed "s/[ \t][ ]*/ /g" > out.txt

# remove blanks from beginning
cat in.txt | sed 's/^[ \t]*//' > out.txt

# Comparing content of two files
comm -12 <(sort FILE1.txt) <(sort FILE2.txt)

# Split file to DVD-R size
split -b 4400m -d file.dd.bz2 file.dd.bz2.

# Fix file permissions
find /media/images/ -type f -exec chmod 666 {} \;

# Extract RPM archiv
rpm2cpio FILENAME | cpio -i --make-directories

# Extract cue / bin image
sudo apt-get install -y bchunk
bchunk file_name.bin file_name.cue file_name.iso

# Get file atime, mtime, ctime
stat FILE

# Change mtime from a file
touch -d "2005-05-05 15:55:55" FILE

# Create selfextract archive under Linux
makeself.sh [-bzip2] DIR archiv.run "DESCRIPTION" COMMAND

# Create Linux software RAID
mdadm --create /dev/md2 --level=raid5 --raid-devices=4 --spare-devices=0 /dev/sdb4
/dev/sdc4 /dev/sdd4

# Wake On Lan (WOL)
wakeonlan 00:11:22:33:44:55

# Change root and start bash
chroot /mnt /bin/bash

# Enable / Disable swap
swapoff -a
swapon -a

# extract initrd.gz
gunzip < initrd.gz | cpio -i --make-directories

# extract initrd.lz
unlzma -c -S .lz ../initrd.lz | cpio -id

# compress initrd
find ./ | cpio -H newc -o > ../initrd
gzip ../initrd

# Extract *.deb
dpkg-deb -x file.deb /tmp

# Find package for a file
dpkg-query -S FILE_NAME

# Add script to a runlevel
update-rc.d apache2 defaults
sudo update-rc.d providername start 90 2 3 5 . stop 10 0 1 4 6 .

# disable sysv service / remove script from runlevel
update-rc.d -f avahi-daemon remove

# Most used commands
history | awk '{print $2}' | sort | uniq -c | sort -rn | head

# VNC on slow connection
xtightvncviewer -compresslevel 9 -quality 0 192.168.0.100
xvnc4viewer -ZlibLevel 9 -LowColourLevel 0 192.168.0.110

# Format partition as FAT16
mkdosfs -F 16 -n SDCARD /dev/sdd1

# Forcing kernel to use new partition table after fdisk
partprobe

# list blocking prozesses
lsof /mnt

# add a existing user to existing group
usermod -a -G GROUPNAME USERNAME

# allow user to administrate system (add to adm group)
usermod -a -G adm ${USER}

# Delete user from group
edit /etc/group and remove user name
or
id -nG USERNAME
usermod -G group1, group2, group3,... USERNAME
# test gpasswd

# Mount SSH
sshfs user@192.168.1.2:/media/images /mnt

# unmask
global: /etc/profile
echo "umask 0000" >> ~/.profile

# check for listening ports
netstat -anp | grep 1234
lsof -i | grep 1234

# dpkg install force-architecture
dpkg  --force-architecture -i *.deb

# display bandwidth usage
iftop

# log loadavg
echo "$(date) $(cat /proc/loadavg)" >> loadavg.log

# convert qcow2 to raw image
qemu-img convert -f qcow2 root.qcow2 -O raw root.raw

# losetup
losetup -a - list all used devices
losetup -d loop_device - delete loop
losetup -f -  print name of first unused loop device

# reconfigure keyboard / console
dpkg-reconfigure console-setup

# kill all prozess from a user
ps -u USERNAME |  awk '{print $1}' | xargs kill -9

# view nfs shares
showmount --exports 192.168.0.1

# connect with gnome nautilus to ssh
sftp://root@SERVER/SHARE

# disable monitor power save (disable DPMS)
xset -dpms

# disable console, x11 screensaver
xset s 0 0
xset s noblank
xset s off
xset -dpms
setterm -blank 0
setterm -powersave off
setterm -powerdown 0

# force umount
sudo umount -l -f /mnt/mountpoint

# convert nero cd image to iso
nrg2iso infile.nrg outfile.iso

# enable harddisk udma mode
hdparm -d1 /dev/hda

# remove multiple spaces from a string
cat x.txt | tr -s " "

# create uniqe file from two files
dos2unix adr_*.txt;  cat adr_hp.txt adr_sel.txt | sort | uniq > adr_uniq.txt

# copy files between hosts with SSH and tar
tar -cf - /some/file | ssh host.name tar -xf - -C /destination

# read cd volume label
dd if=/dev/hdd bs=1 skip=32808 count=32 2> /dev/null | tr -d " "

# rebuild initrd
gzip -d miniroot.gz; mount miniroot /mnt/ -o loop; vi /mnt/linuxrc; gzip --best miniroot

# Fix slow SSH login
echo "UseDNS no" >>  /etc/ssh/sshd_config

# set hostname
echo myhost.local > /etc/hostname; /etc/init.d/hostname.sh start

# start xterm in Xorg session
cat < $HOME/.xsession
xterm
EOF

# change language temporary on command line
export LANG="en_US.UTF-8"

# fix broken package with apt-get (dpkg)
rm /var/lib/dpkg/info/PACKAGE_NAME*
dpkg –remove –force-depends –force-remove-reinstreq PACKAGE_NAME

# configure timezone
dpkg-reconfigure tzdata

# change password non interactive
echo "root:terceS" | chpasswd

# set user random password (to enable login)
echo "username:$(openssl rand -base64 32)" | chpasswd

# generate password
tr -dc "[:alnum:][:punct:]" < /dev/urandom | head -c 12; echo \n

# generate strong password
apg -a 1 -m 32

# MD5-Hash password
echo terceS | mkpasswd -s -H MD5

# delete user password
passwd -d 

# clean mbr
dd if=/dev/zero of=/dev/sdb bs=446 count=1

# create checksum
echo "foo" | md5sum

# create tmpfs
cat <> $TARGET/etc/fstab
tmpfs   /tmp   tmpfs   defaults   0   0
EOF

# display volume label
e2label /dev/sda1 

# change volume label
e2label /dev/sda1 newlabel
# or
tune2fs -L newlabel /dev/sda1

# clean ubuntu trash
sudo rm -rf ~/.local/share/Trash/files/*

# convert ISO-8859-1 to UTF-8
iconv --from-code=ISO-8859-1 --to-code=UTF-8 file.in > file.out file.in > file.out

# Extract Windows cab file
cabextract CAB_FILE_NAME.exe

# set recursive directory rights
find . -type f -exec chmod 644 {} \;

# extract pages from PDF file
pdftk IN.pdf cat 1-5 output OUT.pdf
pdftk IN1.pdf IN2.pdf output OUT.pdf
pdftk in.pdf multistamp stamp.pdf output out.pdf

# merge pdf sites to a single dokument
gs -q -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -dPDFSETTINGS=/ebook -sOutputFile=out.pdf *.pdf
gs -q -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sPAPERSIZE=a4 -dPDFSETTINGS=/ebook -dPDFFitPage -sOutputFile=OUT.pdf IN.pdf

# Convert PDF to JPGs
gs -dNOPAUSE -sDEVICE=jpeg -sOutputFile=image%d.jpg -dJPEGQ=94 -r72x72 -q INPUT_FILE.pdf -c quit

# convert JPGs to PDF
apt-get install imagemagick
convert *.jpg pictures.pdf

# Ubuntu german locale
locale-gen de_DE.UTF-8
echo 'LANG="de_DE"' > /etc/default/locale
cat <> ~/.bashrc
export LANG=de_DE.UTF-8
export LC_ALL=de_DE.UTF-8
EOF

# convert charcode
iconv --from-code=UTF-8 --to-code=ISO-8859-1 IN.txt > OUT.txt

# find duplicate files / images
fdupes -r -f -1 PHOTO_DIR > /tmp/duplicates.txt
mkdir duplicates1
cat /tmp/duplicates.txt | xargs  mv -i --target-directory ./duplicates1/

# disable Nvidia logo on Xorg start (/etc/X11/xorg.conf)
Section "Device"
	Option		"NoLogo"	"True"
EndSection

# Reload gnome panels
killall gnome-panel
#? killall gnome-panel nautilus

# SSH X11 forward
ssh -Y YOUR_SERVER -l YOUR_USER xclock

# Mirror a homepage with wget (http://wiki.ubuntuusers.de/wget)
wget  -m http://www.YOUR_DOMAIN.com --reject=pdf,jpg,gif,png,flv,m4v

# Join / combine flv files
mencoder -forceidx -of lavf -oac copy -ovc copy -o Output.flv File_1.flv File_2.flv File_3.flv File_4.flv File_5.flv

# format DVD-RW
dvd+rw-format -force /dev/cdrom

# update kernel partition table
apt-get install -y parted && partprobe

# sync files from webserver
wget -m -np -nH --cut-dirs=1 http://www.YOUR_DOMAIN.com/stsbox/ --reject="index*"#

# view disk UUID
blkid /dev/sda1

# mount ftp
sudo apt-get install -y curlftpfs
sudo curlftpfs USERNAME:PASSWORT@example.com /mnt/

# Extract or convert CUE/BIN files to ISO image
sudo apt-get install bchunk
bchunk FILE_IN.bin FILE_IN.cue FILE_OUT

# Split a file
split -d -b 10M archiv.tar split-archiv.tar.

# remove apache logs older then 1 year
find /var/log/apache2/ -type f -mtime +365 -exec rm {} \;

# configure limits
/etc/security/limits.conf

# change MAC address
ifconfig eth0 hw ether 00:11:22:33:44:55

# set systemwide default printer
lpadmin -d printer-name

# set user default printer
lpoptions -d printer-name

# Find the speed of your Ethernet card in Linux
sudo mii-tool eth0
cat /sys/class/net/eth0/speed
cat /sys/class/net/eth0/speed
sudo ethtool eth0

# Backup package list and install on another system (not tested yet)
dpkg --get-selections | grep -v deinstall > DPKG_LIST.txt
dpkg --clear-selections
dpkg --set-selections < DPKG_LIST.txt
apt-get install

# show file with netcat
while true; do { echo -e 'HTTP/1.1 200 OK\r\n'; cat FILE; } | nc -w 1 -l -p 80; sleep 1; done

# Fix german keyboard
setxkbmap -model pc105 -layout de -variant basic

# fix "Some index files failed to download. They have been ignored, or old ones used instead."
sudo rm -rf /var/lib/apt/lists/*
sudo rm -vf /var/lib/apt/lists/partial/*

# scan and convert to jpg
scanimage --format tiff --mode color -l 0 -t 0 -x 105 -y 74 --resolution 150  | convert - ${FILE}

# Extract strings from a binary
sudo apt-get install -y binutils
strings /usr/bin/passwd

# tar multicore / parallel compression (bzip)
sudo apt-get install -y pbzip2
tar -I pbzip2 -cf OUT.tar.bz2 /mnt/

# parallel compression (gzip)
sudo apt-get install -y pigz
tar -I pigz -cf OUT.tar.gz /mnt/

# user specific crontab
# list
crontab -l
# edit
crontab -e
# path
/var/spool/cron/crontabs/

# Exit code from previous commands
ls /foo | wc; echo ${PIPESTATUS[@]}

# add current directory to libraries path
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:.

# ionice
ionice -c 3 -p 1184	# set priority for process
ionice -p 1170		# view priority of a process

# find file bigger then 50 MB
find /home -type f -size +50M -exec ls -lh {} \;

# view my public ip
# https://major.io/icanhazip-com-faq/
curl icanhazip.com
curl checkip.dyndns.com

# file write protect
chattr +i /etc/shadow

# find softlinks
find . -type l -exec ls -ld {} \;

# find broken softlinks
find /home/ -type -type l -xtype l

# list deb package dependency
dpkg -I 

# send POST data from command line
curl --data "user=boo&action=insert" 

# list open ports
netstat -lpnt

# backup crontab
crontab -l > crontab.bkp

# clear crontab
crontab -r

# restore crontab
cat crontab.bkp | crontab -

# backup files only smaller than 10 MB
for DIR in .mozilla .ssh .thunderbird bin foo bar; do
    tar cjf ~/backup/$(date -I).${DIR#.}.tar.bz2 ~/${DIR} --exclude "*~" --exclude-from <(find ~/${DIR} -size +10M)
done

# get file last modification date
stat file

# extract specific file from tar archive
tar -xvf archive.tar 

# get installed package list from remote host
ssh root@REMOTE dpkg -l | grep ii | cut -d " "  -f3 | sort > /tmp/remote.out

# diff installed packages between hosts
diff <(ssh host1.example.com dpkg -l | grep ii | cut -d" " -f3) <(ssh host2.example.com dpkg -l | grep ii | cut -d" " -f3)

# kill all screen processes older then 1 day
killall --older-than 1d screen

# get DNS informations for a domain
dig ANY example.com

# escape string
s="a string escaped by \ from ${USER}"
echo $(printf '%q' "$s")

# show ssh key length
ssh-keygen -l -f ~/.ssh/id_rsa.pub

# download recursively http directory
wget --recursive --no-parent --reject "index.html*" http://www.example.com/dir/

# get CPU / system utilization
cat /proc/loadavg

# backup running system
EXCLUDES="--exclude=dev/* --exclude=proc/* --exclude=sys/* --exclude=tmp/* --exclude=var/log/*"
tar ${EXCLUDES} -cjf /tmp/$(hostname -A).$(date -I).tar.bz2 /

# remove file from tar archive
tar --delete -f archive.tar path/to/file.txt

# restore windows MBR
sudo dd if=/usr/lib/syslinux/mbr.bin of=/dev/sda

# myip
wget -q http://checkip.dyndns.com/ -O-

# convert Dos to Unix line break
tr -d '\r' < INPUT_FILE > OUTPUT_FILE

# configure default user login shell to bash
chsh -s /bin/bash ${USER}

# forward network traffic
iptables -t nat -A POSTROUTING -o  -j MASQUERADE
sysctl -w net.ipv4.ip_forward=1

# list CPU performance by core
mpstat -P ALL

# show how long a process has been running
ps -o etime= -p 123

# suspend from CLI
echo -n mem | sudo tee /sys/power/state
dbus-send --system --print-reply --dest="org.freedesktop.login1" /org/freedesktop/login1 org.freedesktop.login1.Manager.Suspend boolean:true
method return sender=:1.0 -> dest=:1.90 reply_serial=2

# list files / folder by size
du -sh * | sort -h
>
# sudo over SSH
echo "ls -l /root/.ssh/id_rsa*" | ssh host sudo bash
ssh -t host "sudo -s bash -c \"ls -l /root/.ssh/id_rsa*\""
 
# create bakup file
cp path/to/file{,.$(date -I)}
 
# clear cache
sudo sh -c 'echo 1 >/proc/sys/vm/drop_caches'
sudo sh -c 'echo 2 >/proc/sys/vm/drop_caches'
sudo sh -c 'echo 3 >/proc/sys/vm/drop_caches'
 
# wget with authentification
wget -q --user=foo --password=bar http://www.example.com -O -
 
# reduce swappiness
echo "vm.swappiness = 1" > /etc/sysctl.d/90-swap.conf
 
# listing swap usage by process
grep VmSwap /proc/*/status | grep -v " 0 kB"
 
for file in /proc/*/status ; do
    awk '/VmSwap|Name/{printf $2 " " $3}END{ print ""}' $file;
done | grep -E 'kB|mB' | sort -k2 -V
 
# parse jeson keys
cat /tmp/in.json | jshon -k
cat /tmp/in.json | jq -r 'keys[]'
 
# set date
date +%Y%m%d -s "20150430"
 
# date with year-mounth-day_hour:minute:second
date +%F_%H:%M:%S
 
# calculate quarter from calendar week
date -d "${YEAR}-01-01 +${MONTH} week" "+%q"
 
# filter
echo $file | sed 's/[^0-9]*//g' 
echo "${file//[!0-9]/}"
 
# use HTTP/HTTPS proxy from command line
export http_proxy=http://proxy.example.com:3128 wget http://www.example.com -O -
curl -H -x http://proxy.example.com:3128 'https://www.example.com/paht/index.php'
 
# get lines beginning with second line
cat /path/to/file | tail -n +2
 
# get total disk usage
df -h --total
 
# diff between remote files (over SSH)
diff <(ssh srv1.example.com cat /etc/fstab) <(ssh srv2.example.com cat /etc/fstab)
 
# /etc/fstab entry non failing / blocking
/media/usb_backup_1   /home/user1/backup   none   nofail,defaults,bind   0   0
 
 
# check platform
if [[ $(getconf LONG_BIT) = "64" ]]; then
    echo "64bit"
else
    echo "32bit"
fi
 
# tar
tar -T input_names.txt -cjf /path/to/archive.tar.bz2
 
tar -cvjf ansible.$(date -I).tar.bz2 --exclude='*.zip' --exclude=.git ansible
 
# extract xz file on the fly
wget -q https://www.example.com/file.tar.xz -O - | tar -C /tmp/ -Jx
 
# reduce pdf
gs -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/ebook -sOutputFile=out.pdf in1.pdf in2.pdf
 
# preserve links
gs -q -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sPAPERSIZE=a4 -dPDFSETTINGS=/ebook -dPDFFitPage -dPrinted=false -sOutputFile=OUT.pdf IN.pdf
 
# show current runlevel
who -r
 
# remove dmraid
sudo dmraid -x 
sudo dmraid -rE
 
# show user member groups
groups <USER>
 
# delete user from group
sudo deluser <USER> sudo
 
# TextMe:
openssl s_client -showcerts -connect www.example.com:443 > /tmp/cacert.pem
curl --cacert /tmp/cacert.pem https://mail.example.com/
 
# convert Ogg to MP3
sudo apt-get install ffmpeg
IFS=$(echo -en "\n\b")
for i in $(find -name "*.ogg"); do
    ffmpeg -i ${i} -c:a libmp3lame -ab 160k -map_metadata 0:s:0 "${i/ogg/mp3}"
done
 
# resice images
for i in *.jpg; do
    convert $i -pointsize 72 -quality 90 -resize 512x384 -unsharp 0x.5 ${i/.jpg/_small.jpg}
done
 
# restart / reload network configuration
sudo ifdown --exclude=lo -a && sudo ifup --exclude=lo -a
 
 
 
# cat file starting from 6 line
cat /tmp/file | tail -n +6
 
# list established connections
sudo netstat -nap | grep EST
 
# cat with ecsape
cat <<'EOF'> /tmp/testfile
test $foo
EOF
 
# check HTML header
curl --head www.example.com
 
# send header
curl -vs -H "Host: example.com" "http://www.foo.com" >/dev/null
 
 
# test memcached
telnet memcached.example.com 11211
stats
quit
 
# TeamViewer QuickSupport on-the-fly (download and start)
wget -q http://download.teamviewer.com/download/teamviewer_qs.tar.gz -O - | tar -C /tmp/ -xz && /tmp/teamviewerqs/teamviewer
 
# get network range
whois $(dig +short whois-servers.net | tail -1) | grep NetRange
 
# list wifi device settings
rfkill list all
echo 0 > /sys/class/rfkill/rfkill0/hard
 
# configure NIC speed
apt-get install -y ethtool
ethtool -s eth1 speed 1000 duplex full
 
# get NIC hardware mac address
ethtool -P eth0
 
# UDP on iperf server
iperf -s [-u]
 
# UDP on ipfer client
iperf -c 192.168.254.1 -t 600 [-u] [-B 192.168.1.1]
 
# set hostname
sudo hostnamectl set-hostname www1
 
# dmesg follow and human readable
dmesg -wH
 
# print current runlevel
who -r
runlevel
 
# erase a DVD-RW via command line
umount /dev/sr0
wodim dev=/dev/sr0 blank=fast
 
# sort directory by size
du -sh * | sort -h
du -h | grep "^[0-9,]*[MGT]" | sort -h
 
# sort by nuber (human readable)
sort -V
sort --version-sort
 
# look screen from command line
gnome-screensaver-command -l
 
# mount filesystems bevore chroot environment
mount /dev/sda1 /mnt
cd /mnt
mount -t proc proc proc/
mount -t sysfs sys sys/
mount -o bind /dev dev/
chroot /mnt
 
# recovery mode kernel boot parameter
fsck.mode=force
fsck.repair=yes
break
 
# get count of running processes
pgrep -cf <service_name>
 
# monitor / restart service when not running
pgrep -f glassfish 1>/dev/null || service glassfish restart
 
# format output with awk
cat file.csv | awk -v OFS="\t" -F ";" '{printf "%s\t%s\t%s\t%.0f\t%8.0f\t\n", $1, $4, $2, $7/1024, $6}'
 
# start software raid
mdadm --assemble --scan -v
 
# install grub2
grub-install --no-floppy --root-directory=/mnt /dev/sda
 
Bash redirection
# stdout to file
programm > out.log
 
# stderr to file
programm 2> out.err
 
# stdout and stderr to file
programm &> out.log
 
# stdout to stderr
programm 1>&2
 
# stderr to stdout
programm 2>&1
 
# resize dos partition / bootdisk
fatresize -s 33M bios.img
dd if=bios.img of=bios_34m.img bs=1M count=34
 
# set systemwide proxy
echo "export http_proxy=http://proxy.example.com:3128" >> /etc/bashrc
 
# crop PDF
gs -o out.pdf -sDEVICE=pdfwrite -c "[/CropBox [20 20 80 1000] /PAGES pdfmark" -f in.pdf
 
# remove route and blank lines
cat <input_file> | grep -v '#' | grep '\S
 
# composer install (create vendor directory)
./composer install
 
# screen scroll UP
CTRL + a
ESC
UP
 
# reconnect screen
sudo screen -list
sudo screen -d -r root/2953.ubuntu-release-upgrade-screen-window
 
# show processes with high cpu / memory usage
ps -eo pid,ppid,cmd,%mem,%cpu --sort=%cpu --no-headers
top -b -o +%CPU | head
 
ps -eo size,pid,user,command --sort -size | awk '{ hr=$1/1024 ; printf("%13.2f Mb ",hr) } { for ( x=4 ; x<=NF ; x++ ) { printf("%s ",$x) } print "" }' |    cut -d "" -f2 | cut -d "-" -f1
 
# sub shell
( (sleep 10; date) > /tmp/q ) &
 
# create user
sudo useradd user1 -m -s /bin/bash
sudo usermod -a -G sudo user1
 
sudo adduser user2 --shell /bin/bash --group sudo
 
# add user to group sudo
sudo usermod -a -G sudo <username>
 
# check backup
sudo -u git bash -c "find /var/opt/gitlab/backups -name *_$(date +%Y_%m_%d)_gitlab_backup.tar -size +50M"
 
# ntp
apt-get install -y ntp
cat /etc/ntp.conf
ntpd -q -g
sudo ntpdate -u ntp.ubuntu.com 
hwclock --systohc
ntpq -p
 
# identify network device
sudo nmap -O -v 10.0.0.5
 
# reconnect to process / bash
reptyr <PID>
 
# KeePassX command
cmd://bash /foo/bar.sh {USERNAME} {PASSWORD}
 
# mount samba share
sudo apt-get install -y cifs-utils
sudo mount -t cifs -o user=foo //backup.example.cmo/bar /mnt
 
# remove LSI / intel software raid
wipefs -a /dev/sda
 
# configure camera codec
apt-get install -y v4l-utils 
v4l2-ctl --list-formats
v4l2-ctl --set-fmt-video=width=640,height=480,pixelformat=0
 
# benchmark disc
bwm-ng -i disk
 
# reduce disk I/O
echo 1 > /proc/sys/vm/dirty_background_ratio
echo 80 > /proc/sys/vm/dirty_ratio
 
# renice rsync
sudo pgrep rsync | xargs ionice -c3 -p
sudo pgrep rsync | xargs renice -n 19 -p
 
# date
echo $(date +%Y-%m-%d\ %H:%M:%S)
 
# reboot with delay
shutdown -r +5 "Maintaince: Server will restart in 5 minutes."
 
# partitionize whole disk from command line
parted /dev/sdc -s mklabel msdos
parted /dev/sdc -s mkpart primary 2048s 100%
mkfs.vfat -n USB_DISK /dev/sdc1
 
# get MAC address for a interface
cat /sys/class/net/eth0/address
 
# Apache: Top file not found
cat /var/log/apache2/error.log | grep "File does not exist" | cut -d " " -f 13 | sort | uniq -c | sort -rn | egrep -v '.*1 '
 
# list physical disc usage
df -hT | sed -n '1p;/^\//p;'
 
# replace text recursively
find -type f -size -50k -exec sed -i 's|foo|bar|g' {} \;
 
# find and del dirs smaller then (not tested)
find . -maxdepth 1 -type d | 
    while read dir; do [ $(du -s "$dir") -le 102400 ] && rm -f "$dir"; done
 
# enable cron logging to dedicated file on ubuntu
sed -i 's|#cron|cron|g' /etc/rsyslog.d/50-default.conf
service rsyslog restart
 
# last
last -x reboot
last -x shutdown
 
# rename files in directory
rename 's/out/txt/' *.out
 
# rename file
mv /tmp/test{,.off}
 
# search in pdfs
find . -name '*.pdf' -exec sh -c 'pdftotext "{}" - | grep --with-filename --label="{}" --color "foo"' \;
 
# findest oldest access directory
find -type d -printf '%T+ %p\n' -not \( -path "./exclude_dir/*" -type d  -prune \) | sort | head -n 20
 
# create iso from directory content
genisoimage -R -o ../foo.iso *
 
# df formated output
df -h --output=size,used,avail,pcent /home
 
# rescan disk
echo "1" > /sys/class/block/sdX/device/rescan
 
# add user to group and reload group without logout
sudo usermod -a -G lxd ${USER}
su - ${USER}
 
# linux hard reset
echo s > /proc/sysrq-trigger
echo b > /proc/sysrq-trigger
 
# ext4 space reserved for root
tune2fs -l /dev/sdb1 | grep -i reserved
sudo tune2fs -m 0 /dev/sdb1
 
# get size for hidden directories
du -sh .{a..z}* 2>/dev/null | grep G
 
# convert textfile to UTF-8
iconv -f ISO-8859-1 IN.txt -t UTF-8 -o OUT.txt
 
# process uptime
ls -al /proc | grep 2719
 
# kill all apache
kill -9 $(pidof apache2)
pkill -9 apache2
 
# convert string to lower case
tr A-Z a-z < file_in > file_out
 
# list zip content
unzip -l archive.zip 
 
# zip: max compress, move file, ignore directory structure
DUMP=$(hostname).${DB}.$(date -I).sql
zip -9qmj ${DUMP%.2*}.sql.zip /tmp/${DUMP}
 
# parallel compress (keep the old file)
pbzip2 -k9 file1.in file2.in ...
 
# extract from URL
wget https://example.com/file.iso.bz2 -qO- | bzip2 -d > /path/file_out.iso
 
# yesterdays date
date -d yesterday +%Y-%m-%d
 
# show openend connections
netstat -tn grep :80
 
# disable root password
sudo passwd -l root
 
# inotifywait conf watch (single line)
while true; do inotifywait --quiet --monitor --event create,delete --exclude "[^c][^o][^n][^f]$" /tmp | /usr/sbin/nginx -t && /usr/sbin/service nginx reload; done
 
# diff loop
DIR=/etc/libvirt/qemu
for FILE in $(ls -1 ${DIR}/*.xml); do
    CMD="diff ${FILE} ${FILE}.$(date -I)"
    echo "${CMD}"
    ${CMD}
done
 
# diff exclude
diff --exclude=direxclude -r dir1 dir2
 
# display list of system calls
strace <app>
 
# replace duplicates with hardlinks
rdfind -makehardlinks true .
 
# show user default shell
getent passwd ${LOGNAME} | cut -d: -f7
 
# change disk label
# FAT
sudo fatlabel /dev/sdxN my_disk_1
 
# exFAT:
sudo exfatlabel /dev/sdxN my_disk_1
 
# NTFS
sudo ntfslabel /dev/sdxN my_disk_1
 
# ext2/3/4
sudo e2label /dev/sdxN my_disk_1
 
# BTRFS
sudo btrfs filesystem label /dev/sdxN my_disk_1
 
# get external / public IP
curl ifconfig.me
curl ipinfo.io/ip
curl 116.202.55.106
 
# check file size
[[ $(find /media/backup/ -size +100M -name bkp_$(date -I)_*.dmp.bz2 ) ]] && echo true || echo false
 
# find mime-type for a file
file --mime-type file.sh
 
# change date
touch -d 20180101 file1
 
# kill all defunct
ps -ef | grep defunct | grep -v grep | cut -b8-20 | xargs kill -9
 
# reload profile without relogin
su - $USER
 
# get file look
sudo fuser -v /var/cache/debconf/config.dat
 
# get linked libraries
ldd -d -r $(which wget)
 
# remove string from string
echo "my foo bar" | sed -e s/foo//
 
# copy output to one line
your_command | paste -sd, -
 
# remove last liste from file
cat /tmp/file.in | sed '$ d' > /tmp/file.out
 
# sync time
systemctl stop ntpd
ntpdate ntp.ubuntu.com
systemctl start ntpd
systemctl enable ntpd
systemctl status ntpd
 
# generate mac address
openssl rand -hex 6 | sed 's/\(..\)/\1:/g; s/:$//'
 
# get script path
echo $(dirname $(readlink -f "$0"))
 
# create 7zip encrypted backup
7z a  -p -mhe=on '-xr!*.jpg' '-xr!OLD' ~/backup/bkp.$(date -I).7z ~/dir1/* ~/.ssh
 
# print duplicate lines
cat FILE| uniq -d
 
# remount root read only
mount -f -o remount,ro /
 
# Remove all characters except of alphanumeric and "-":
echo "a b-1_2" | sed "s/[^[:alnum:]-]//g"
 
# Print unicode char
ctrl + shift + u 
2665
enter
 
# remount / force read only mount of root filesystem
umount -a
echo u > /proc/sysrq-trigger
mount -f -o remount,ro /dev/sda1
 
# change filesystem UUID
tune2fs -U 2efc22cb-98a4-448d-8fbe-a895462703c0 /dev/sdc1
 
# force XFS filesystem check
force fsck.xfs to run xfs_repair at boot time by creating a /forcefsck file or booting the system with "fsck.mode=force" on the kernel command line
 
# set initcwnd
ip route change default via 10.0.1.254 dev eth0 onlink initcwnd 32
 
# join three files
join -e- -a1 -a2 -o 0 1.2 2.2 file1 file2 | join -e- -a1 -a2 -o 0 1.2 1.3 2.2 - file3 | column -t

Sequence
https://linuxhint.com/bash_range/

# get sequence every 5
echo {50..100..5}
 
# decrementing sequence
seq 10 -2 1
 
# leading zeros
seq -w 1 12
 
# separator
seq -s"," 10
 
# formated
seq -f "%f" 3 0.8 6
seq -f "2019-01-%02g" 31
 
# multiple sequence
echo www{{1..3},{10..13}}-{dev,stage}
 
# debug dhcp
dhcping -s 10.0.0.253 -h fa:11:22:33:44:55
 
# configure keyboard language
loadkeys de
 
# umount luks
dmsetup remove /dev/mapper/luks-xxx
 
# get NVMe data
nvme list --output-format=json | jq -r .Devices[].ModelNumber
 
# umount recursively
umount  /mnt -R
 
# show reboots
last | egrep "down|boot"
 
# check trim support (when DISC-MAX != 0B)
lsblk -D /dev/sda | grep 2B
 
# dd with progress
dd if=/dev/sdc of=/dev/sdd bs=4096 status=progress
 
# sum lines together
paste -s -d+ infile | bc
 
# force root filesystem check
echo 1 >/sys/block/sda/sda1/ro
mount -f -o remount,ro /mount/point
fsck.ext4 /dev/sda1
 
# truncate file
truncate --size=1G out.log
 
# write to file as root
cat << EOF | sudo tee /etc/file.conf
...
EOF
 
# directory to iso
genisoimage -o output_image.iso directory_name
 
# show host IP addresses
hostname -I
 
# show process uptime
ps -o lstart ${PID}
 
# debug network
while true; do
  ip a && iptables -S | tee "/tmp/iptables_$(date -Is)"
  sleep 1
done
 
# load check
dstat -cdn -D sda -N eno1 -C total --top-cpu --top-io --top-mem -f 10
nethogs
nload eno1 -o 10000 -t 1000
 
# show my extenal / outgoing IP
curl https://ipinfo.io/ip
 
# get internal kernel device name for all block devices
lsblk -np --output KNAME | tac
lsblk -np --output KNAME -J
 
# edit sudoers file
visudo -f /etc/sudoers.d/nagios
 
# copy disc with dd
nice -n 19 ionice -c 3 dd if=/dev/sdc of=/dev/sdd bs=16384 status=progress
 
# get realpath of symlink
readlink -f /path/to/link
 
# change vfat partition label
mlabel -i /dev/sdd1 ::usb32gb
 
# get boot parameter
cat /proc/cmdline | sed -e 's/^.*BOOTIF=//' -e 's/ .*$//'
 
# Manuall compress big files
find /srv/log/*/202*/0{1..3}/ -type f -size +128M | xargs gzip
 
# gzip to specific directory / file
gzip -c file_in > /paht/to/file_out.gz
 
# get real interface mac address
ethtool -P eth0
 
# mount by label / partlabel
mount PARTLABEL=esp /mnt
mount LABEL=esp /mnt
 
# set command timeout
timeout 5 ping google.com
 
# output 10 chars
cat /tmp/input.txt  | cut -c -10
 
# show disk statistics
iostat -x /dev/nvme*
 
# test UDP port
nc -z -v -u <host> <port>
 
# unlock LUCS disk
sudo udisksctl unlock -b /dev/sdb1
sudo mount /dev/dm-0 /mnt/
 
# lock LUCS disk
sudo udisksctl unmount -b /mnt
sudo udisksctl lock -b /dev/sdb1
 
# LUCS v2
sudo apt-get install -y cryptsetup
 
# decrypt volume
sudo cryptsetup luksOpen /dev/sdb1 luks1
sudo mount /dev/mapper/luks1 /mnt
 
# lock volume
sudo umount /mnt
sudo cryptsetup luksClose luks1
 
# test port 80 (webserver with nc)
apt install -y netcat-traditional
while true ; do nc -l -p 80 -c 'echo -e "HTTP/1.1 200 OK\n\n $(date)"'; done
 
# ftp
cat ~/.netrc 
machine 192.168.0.10
login ftp_user_1
password xxxxxxx
 
ftp 192.168.0.10 <<EOF
binary
put my-file-$(date -I).zip 
quit
EOF
 
# get exit status from process with pipe
mvn clean install | tee $logfile
echo ${PIPESTATUS[0]}
 
# Pressure Stall Information
watch -n1 tail /proc/pressure/*
sudo apt install -y atop
 
# show block size
lsblk -t /dev/sda
 
# get filesystem UUID
lsblk -o UUID -n /dev/sdb1
 
# set timezone
timedatectl list-timezones
sudo timedatectl set-timezone Europe/Berlin
 
# copy softlink
cp -P /path/to/softlink /targe/dir/
 
# get MAC address for interface
ethtool -i eth0
grep PCI_SLOT_NAME /sys/class/net/*/device/uevent
 
ls -ladtu --full-time $(find /srv/ /opt/ -name foo)
 
# get smallest disk
lsblk --include 8 --include 259 --nodeps --noheadings --sort size --output NAME | head -1
 
# copy disk GPT partition scheme
sgdisk --replicate=/dev/target /dev/source
 
# get ext4 partition UUID
sudo blkid | grep UUID
 
# set ext4 partition UUID
umount /dev/sdb1
tune2fs -U random /dev/sdb1
 
# USB device reset
sudo usbreset 0d88:0101
echo "${USER} ALL = NOPASSWD: /usr/bin/usbreset" | sudo tee /etc/sudoers.d/usbreset
 
# list by size
du -sh -- *  | sort -rh  # Files and directories, or
du -sh -- */ | sort -rh  # Directories only

Ad-hoc HTTP server
https://github.com/weihanglo/sfz

URL=https://github.com/weihanglo/sfz/releases/download/v0.3.0/sfz-v0.3.0-x86_64-unknown-linux-gnu.tar.gz
 
wget ${URL} -qO- | tar -xz -C /tmp
/tmp/sfz -a -L -b 0.0.0.0 -p 8080
 
# Create HTTP server with python
python -m http.server <PORT> --directory <FILE PATH>

Port test

# TCP port test
nc -l -p 8080 # server
nc ${SERVER_IP} 8080 # client
 
# UDP port test
nc -u -l -p 8080 # server
nc -u ${SERVER_IP} 8080 # client

Tmux

# split window horizontaly
ctrl+b
shift+"
 
# split window verticaly
ctrl+b
shift+%
 
# multiplex commands
ctrl+b
shift+:
set synchronize-panes
#set synchronize-panes off
 
# Show double line in file
sort <file> | uniq -c

Links
https://how.wtf/articles.html