Linux tutorials

This page contains different linux one-liner commands that will be useful in every day linux administrators work



List all users on server

sudo getent shadow | awk -F: '$2 ~ /^\$/ || $2 ~ /^!\$/ {print $1}'

Kill all proccesses that include keyword java using for loop
 for i in $(pkill  java); do kill -9 "$i"; done ; 
Let´s archive all files that are older than 360 days.
 find . -mtime +360 -name "*" -exec gzip {} \; 
List all kernels minus the one already in usage
 dpkg -l 'linux-*' | sed '/^ii/!d;/'"$(uname -r | sed "s/\(.*\)-\([^0-9]\+\)/\1/")"'/d;s/^[^ ]* [^ ]* \([^ ]*\).*/\1/;/[0-9]/!d' 
Find out server external IP
 dig +short 
List all sudo users
 grep -Po '^sudo.+:\K.*$' /etc/group 
List all files within folder and sort them by date and then append stdoutput to /root/results.txt
 find . -type f -printf "%-.22T+ %M %n %-8u %-8g %8s %Tx %.8TX %p\n" | sort | cut -f 2- -d ' ' > /root/results.txt 
Generate 32 random string
 cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 32 | head -n 1 
List all PIDs that java is using and use grep to find them
 ps aux | grep java | grep -v grep | awk '{print $2}' 
Generate SHA-512 hash using python
 python3 -c "from getpass import getpass; from crypt import *; \
p=getpass(); print('\n'+crypt(p, METHOD_SHA512)) \
if p==getpass('Please repeat: ') else print('\nFailed repeating.')" 
Find out top 15 proccesses using CPU
 ps ahux --sort=-c | awk 'NR<=15{printf"%s %6d %s\n",$3,$2,$11}' 
Find out procceses using most memory
 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 
Grep keyword using 8 threads and append it to /root/output.txt
 find . -type f | parallel -j+8 grep keyword > /root/output.txt 
Gunzip using multiple threads
 parallel -j+32 --gnu gunzip ::: *gz 
Get all IP addresses on the server
 ifconfig -a | grep -oP 'inet \K\S+' 
Find out how many cores does server have
 lscpu | egrep 'CPU\(s\)' 
Find out free IP-s in your subnet. In this example we inspect subnet
 sudo nmap -v -sn -n -oG - | awk '/Status: Down/{print $2}' 
Delete folders using parallel (multithread)
 parallel -j8 --no-notice rm -rf ::: dir1 dir2 dir3 
Delete files older than 7 days
 find /path -type f -mtime +7 -delete >/dev/null 2>&1 
List of packages that can be upgraded
 apt list --upgradable | tail +2 | cut -d/ -f1 
chown files faster using parallel
 find /path-maxdepth 1 -mindepth 1 -type d|parallel chown -R user:user 
Use parallel to find out folder size. “-j8” specifies how many threads will be used
 parallel -j8 du -hcs ::: renderd/ 
Use nmap to find out what ssl ciphers is server using
 nmap -T4 -F --script ssl-enum-ciphers <hostname> 
List all cronjobs used by all users
 for user in $(cut -f1 -d: /etc/passwd); do echo $user; crontab -u $user -l; done 
List processes by inode usage
 sudo find . -xdev -type f | cut -d "/" -f 2 | sort | uniq -c | sort -n 
Test your server download and upload speed via command line
 curl -s | python -