System information commands =========================== (*) #su Show only errors and warnings: `dmesg --level=err,warn` (*) View dmesg output in human readable format: `dmesg -T` (*) Get an audio notification if a new device is attached to your computer: `dmesg -tW -l notice | gawk '{ if ($4 == "Attached") { system("echo New device attached | espeak") } }` (*) Dmesg: follow/wait for new kernel messages: `dmesg -w` (*) The proper way to read kernel messages in realtime.: `dmesg -wx` (*) Query graphics card: `lspci -nnk | grep -i VGA -A2` (*) Query sound card: `lspci -nnk | grep -i audio -A2` (*) Quick and dirty hardware summary: `(printf "\nCPU\n\n"; lscpu; printf "\nMEMORY\n\n"; free -h; printf "\nDISKS\n\n"; lsblk; printf "\nPCI\n\n"; lspci; printf "\nUSB\n\n"; lsusb; printf "\nNETWORK\n\n"; ifconfig) | less` (*) Percental CPU scaled load average: `printf "System load (1m/5m/15m): "; for l in 1 2 3 ; do printf "%.1f%s" "$(( $(cat /proc/loadavg | cut -f $l -d " ") * 100 / $(nproc) ))" "% "; done; printf "\n"` `finddevs.sh` (poor man's lsusb): {{{sh #!/bin/sh for sysdevpath in $(find /sys/bus/usb/devices/usb*/ -name dev); do ( syspath="${sysdevpath%/dev}" devname="$(udevadm info -q name -p $syspath)" [[ "$devname" == "bus/"* ]] && exit eval "$(udevadm info -q property --export -p $syspath)" [[ -z "$ID_SERIAL" ]] && exit echo "/dev/$devname - $ID_SERIAL" ) done }}} (*) Summarize the size of current directory on disk in a human-readable format: `du -sh` (*) See free disk space in a human readable format: `df -h` (*) Currently mounted filesystems in nice layout: `mount | column -t` (*) Get the top 10 largest files ordered by size descending, starting from the current folder, recursively: `find . -printf '%s %p\n'| sort -nr | head -10` (*) Find 10 largest folders: `du -hsx * | sort -rh | head -10` (*) List of commands you use most often: `history | awk '{a[$2]++}END{for(i in a){print a[i] " " i}}' | sort -rn | head` (*) List of commands you use most often: `history | awk '{print $2}' | sort | uniq -c | sort -rn | head` (*) Query my external IP address: `curl -4 https://icanhazip.com` (*) #su List processes that are actively using a port: `netstat -tulpn | grep LISTEN` (*) List currently running processes: `ps auxww` (*) List all process of current user (full info): `ps --user NAME -F` (*) Show most memory intensive process: `ps axch -o cmd:15,%mem --sort=-%mem` (*) Show most CPU intensive process: `ps axch -o cmd:15,%cpu --sort=-%cpuw` (*) Show systemctl failed units: `systemctl --failed` (*) Show the status of a unit: `systemctl status NAMEOFUNIT` (*) Show all installed services: `systemctl list-unit-files --state=enabled --no-pager` (*) #su Flash an image onto a USB drive using cat: `cat path/to/archlinux-version-x86_64.iso > /dev/sdx` (*) #su Flash an image onto a USB drive using cp: `cp path/to/archlinux-version-x86_64.iso /dev/sdx` (*) #su Flash an image onto a USB drive using dd: `dd bs=4M if=path/to/archlinux-version-x86_64.iso of=/dev/sdx status=progress oflag=sync` (*) #su Flash an image onto a USB drive using tee: `tee < path/to/archlinux-version-x86_64.iso > /dev/sdx` (*) #su Mount an ISO: `mount -o loop /path/to/image.iso /media/mountpoint` (*) #su Rip an ISO: run `isosize -x /dev/sr0` to determine sector count and size, then run `dd if=/dev/sr0 of=discimage.iso bs=SECTOR_SIZE count=SECTOR_COUNT status=progress` (*) List input devices: `xinput list` (e.g. to see Touchpad input on a laptop) (*) Disable touchpad (and possibly add to `.xprofile`): `xinput disable 'SynPS/2 Synaptics TouchPad'` (*) List all running processes: `ps aux` (*) List all running processes including the full command string: `ps auxww` (*) Search for a process that matches a string: `ps aux | grep string` (*) List all processes of the current user in extra full format: `ps --user $(id -u) -F` (*) List all processes of the current user as a tree: `ps --user $(id -u) f` (*) Get the parent PID of a process: `ps -o ppid= -p pid` (*) Sort processes by memory consumption: `ps --sort size` (*) List all of the signals kill can send: `kill -l` (*) Hang up process: `kill -1 process_id` (*) Send interrupt to process: `kill -2 process_id` (*) Immediately terminate a process: `kill -9 process_id` (*) Hang up all processes that match a name: `pkill -9 "process_name"` Fundamental Commands ==================== (*) Invert matching lines with `-v`: `grep -v "roses" poem.txt` (*) TODO look ahead/behind with `-A`, `-B`, `-C` switches (*) TODO only keep what grep found with `-o` (*) Find (grep) files with oldpattern and replace with newpattern: `grep /path/to/search -rl -e "oldpattern" | xargs sed -i "s/oldpattern/newpattern/g"` (*) Find (grep) strings in files, in current directory, recursively (-r), printing line numbers (-n): `grep "STRING" -rnw .` (*) Find all files in current directory exclude `.wine` and `.git` directories: `find . -type f \! \( -path '*/\.wine/*' -o -path '*/\.git/*' \)` (*) Find recently accessed files: `find . -type f -print0 | xargs -0 stat --format '%Y :%y %n' | sort -nr | cut -d: -f2- | head` (*) Example. Find files and pipe into xargs (filenames with spaces): `find . -type f -print0 | xargs -0 sxiv -t` (*) Listing today’s files only: `find directory_path -maxdepth 1 -daystart -mtime -1` (*) Find ASCII files and extract IP addresses: `find . -type f -exec grep -Iq . {} \; -exec grep -oE "(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)" {} /dev/null \;` (*) Find out which directory uses most inodes - list total sum of directoryname existing on filesystem: `find /etc -xdev -printf '%h\n' | sort | uniq -c | sort -k 1 -n` (*) Find all executable files across the entire tree: `find -executable -type f` (*) Replace recursive in folder with sed: `find -type f -exec sed -i 's/my big String/newString/g' {} +` (*) Show contents of all git objects in a git repo: `find .git/objects/ -type f \| sed 's/\.git\/objects\/\///' | sed 's/\///g' | xargs -n1 -I% echo echo "%" \$\(git cat-file -p "%"\) \0 | xargs -n1 -0 sh -c` (*) Find dupe files by checking md5sum: `find /glftpd/site/archive -type f|grep '([0-9]\{1,9\})\.[^.]\+$'|parallel -n1 -j200% md5sum ::: |awk 'x[$1]++ { print $2 " :::"}'|sed 's/^/Dupe: /g'|sed 's,Dupe,\x1B[31m&\x1B[0m,'` (*) Find and remove old backup files: `find /home/ -name bk_all_dbProdSlave_\* -mtime +2 -exec rm -f {} \;` (*) Find and remove old compressed backup files: `find /home -type f \( -name "*.sql.gz" -o -name "*.tar.gz" -mtime +10 \) -exec rm -rf {} \;` (*) Shows space used by each directory of the root filesystem excluding mountpoints/external filesystems (and sort the output): `find / -maxdepth 1 -mindepth 1 -type d \! -empty \! -exec mountpoint -q {} \; -exec du -xsh {} + | sort -h` (*) Shows space used by each directory of the root filesystem excluding mountpoints/external filesystems (and sort the output): `find / -maxdepth 1 -mindepth 1 -type d -exec du -skx {} \; | sort -n` (*) Create an uncompressed tar file of each child directory of the current working directory: `find . -maxdepth 1 -mindepth 1 -type d -exec tar cvf {}.tar {} \;` (*) Tar and bz2 a set of folders as individual files: `find . -maxdepth 1 -type d -name '*screenflow' -exec tar jcvf {}.tar.bz2 {} \;` (*) Shows space used by each directory of the root filesystem excluding mountpoints/external filesystems (and sort the output): `find / -maxdepth 1 -type d | xargs -I {} sh -c "mountpoint -q {} || du -sk {}" | sort -n` (*) Zgrep across multiple files: `find . -name "file-pattern*.gz" -exec zgrep -H 'pattern' {} \;` (*) Code to check if a module is used in python code: `find . -name "*.ipynb" -exec grep -l "symspellpy" {} \;` (*) Delete all files by extension: `find / -name "*.jpg" -delete` (*) Check if the same table name exist across different databases: `find . -name "withdrownblocks.frm" | sort -u | awk -F'/' '{print $3}' | wc -l` (*) Count the total amount of hours of your music collection: `find . -print0 | xargs -0 -P 40 -n 1 sh -c 'ffmpeg -i "$1" 2>&1 | grep "Duration:" | cut -d " " -f 4 | sed "s/.$//" | tr "." ":"' - | awk -F ':' '{ sum1+=$1; sum2+=$2; sum3+=$3; sum4+=$4 } END { printf "%.0f:%.0f:%.0f.%.0f\n", sum1, sum2, sum3, sum4 }'` (*) Graphical tree of sub-directories with files: `find . -print | sed -e 's;[^/]*/;|-- ;g;s;-- |; |;g'` (*) Moving large number of files: `find /source/directory -mindepth 1 -maxdepth 1 -name '*' -print0 | xargs -0 mv -t /target/directory;` (*) List only empty directories and delete safely (=ask for each): `find . -type d -empty -exec rm -i -R {} \;` (*) Find ASCII files and extract IP addresses: `find . -type f -exec grep -Iq . {} \; -exec grep -oE "(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)" {} /dev/null \;` (*) Remove scripts tags from *.html and *.htm files under the current directory: `find ./ -type f \( -iname '*.html' -or -iname '*.htm' \) -exec sed -i '/