Professional Documents
Culture Documents
Really Useful Linux Commands-151211164352
Really Useful Linux Commands-151211164352
Really Useful Linux Commands-151211164352
I hope you find these are useful as I have. I’ll periodically add to the list, so check back
occasionally.
CONTENTS
Admin Commands .................................................................................................................... 2
GIT/JIRA.................................................................................................................................. 10
Images..................................................................................................................................... 10
Misc Commands ..................................................................................................................... 11
Networking .............................................................................................................................. 12
Windows.................................................................................................................................. 13
Java ......................................................................................................................................... 13
VituralBox ................................................................................................................................ 13
Apache .................................................................................................................................... 13
Encryption ............................................................................................................................... 14
Curl and Such ......................................................................................................................... 15
MySQL .................................................................................................................................... 17
Ports ........................................................................................................................................ 19
File........................................................................................................................................... 21
Comics .................................................................................................................................... 26
Other Fun ................................................................................................................................ 26
Page 2
Admin Commands
# netstat with group by (ip adress)
netstat -ntu | awk ' $5 ~ /^[0-9]/ {print $5}' | cut -d: -f1 | sort |
uniq -c | sort -n
# Reboot
shutdown now -r or reboot
# Copy your SSH public key on a remote machine for passwordless login
- the easy way
ssh-copy-id username@hostname
# Instantly load bash history of one shell into another running shell
$ history -a #in one shell , and $ history -r #in another running
shell
# useless load
cat /dev/urandom | gzip -9 > /dev/null &
# geoip information
GeoipLookUp(){ curl -A "Mozilla/5.0" -s
"http://www.geody.com/geoip.php?ip=$1" | grep "^IP.*$1" | html2text; }
date -d09/19/1966+1000000000sec
# Flush DNS
sudo /etc/init.d/dns-clean
# ssh copy
cat ~/.ssh/id_rsa.pub | ssh deployer@xxxxx -p 52201 'cat >>
~/.ssh/authorized_keys'
# Should I be sleeping?
[ $(date +"%H") -lt 7 ] && echo you should probably be sleeping...
# Calculates the date 2 weeks ago from Saturday the specified format.
date -d '2 weeks ago Saturday' +%Y-%m-%d
GIT/JIRA
# Grab all JIRA ticket numbers (e.g. ABC-123) mentioned in commits
added in feature branch off of master
git log master...feature-a | grep -o -E '\b([A-Z]+)-[0-9]+\b' | sort |
uniq
Images
# Determine an image's dimensions
identify -format "%wx%h" /path/to/image.jpg
# Resolution of a image
identify -format "%[fx:w]x%[fx:h]" logo:
# Create a favicon
convert -colors 256 -resize 16x16 face.jpg face.ppm && ppmtowinicon -
output favicon.ico face.ppm
Page 11
Misc Commands
# Press Any Key to Continue
echo -n "Press any key to continue..." && read
Networking
# A list of IPs (only) that are online in a specific subnet.
nmap -sP 192.168.1.0/24 | awk "/^Host/"'{ print $3 }' |nawk -F'[()]' '{print $2}'
# Regex to reliably search a file for valid IP addresses (and external IP addresses)
grep -Eoa "\b(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]?)\b" Filetosearch.txt
# regex to match an ip
echo 254.003.032.3 | grep -P '^((25[0-4]|2[0-4]\d|[01]?[\d]?[1-9])\.){3}(25[0-4]|2[0-
4]\d|[01]?[\d]?[1-9])$'
# The NMAP command you can use scan for the Conficker virus on your LAN
nmap -PN -T4 -p139,445 -n -v --script=smb-check-vulns --script-args safe=1 192.168.0.1-254
Windows
# ps for windows
wmic process list IO
Java
# Count threads of a jvm process
ps uH p <PID_OF_U_PROCESS> | wc -l
VituralBox
# Launch a VirtualBox virtual machine
VBoxManage startvm "name"
Apache
# Know which modules are loaded on an Apache server
apache2 -t -D DUMP_MODULES
Encryption
# GUID generator
guid(){ lynx -nonumbers -dump http://www.famkruithof.net/uuid/uuidgen | grep "\w\{8\}-" | tr -d ' ';
}
# Base64 decode
echo Y29tbWFuZGxpbmUuZnUgcm9ja3MK | base64 -d
# Generate MD5 of string and output only the hash checksum in a readable format
echo -n "String to MD5" | md5sum | sed -e 's/[0-9a-f]\{2\}/& /g' -e 's/ -//'
# It decripts all pgp files in a selection folder and move the output into a file.
for x in *.pgp do `cat /file_with_the_passphrase.dat|(gpg --batch --no-tty --yes --passphrase-fd=0
--decrypt `basename $x`; ) > 'dump_content.dat'` done;
# grab all commandlinefu shell functions into a single file, suitable for sourcing.
export QQ=$(mktemp -d);(cd $QQ; curl -s -O
http://www.commandlinefu.com/commands/browse/sort-by-votes/plaintext/[0-2400:25];for i in
$(perl -ne 'print "$1\n" if( /^(\w+\(\))/ )' *|sort -u);do grep -h -m1 -B1 $i *; done)|grep -v '^--' >
clf.sh;rm -r $QQ
# geoip information
geo(){ curl -s "http://www.geody.com/geoip.php?ip=$(dig +short $1)"| sed
'/^IP:/!d;s/<[^>][^>]*>//g'; }
MySQL
# Get column names in MySQL
mysql -u <user> --password=<password> -e "SHOW COLUMNS FROM <table>"
<database> | awk '{print $1}' | tr "\n" "," | sed 's/,$//g'
# mysql DB size
mysql -u root -pPasswort -e 'select
table_schema,round(sum(data_length+index_length)/1024/1024,4) from
information_schema.tables group by table_schema;'
Ports
# Forensic tool to find hidden processes and ports
unhide (proc|sys|brute)
lsof -i :80
# Port Knocking!
knock <host> 3000 4000 5000 && ssh -p <port> user@host && knock <host> 5000
4000 3000
# Lists all listening ports together with the PID of the associated process
lsof -Pan -i tcp -i udp
# Lists all listening ports together with the PID of the associated process
netstat -tunlp
# start a tunnel from some machine's port 80 to your local post 2001
ssh -N -L2001:localhost:80 somemachine
File
# get size of a file
du -hs file-name
# Recursively search a directory tree for all .php .inc .html .htm .css .js
files for a certain string
find . -type f \( -name "*.js" -o -name "*.php" -o -name "*.inc" -o -name
"*.html" -o -name "*.htm" -o -name "*.css" \) -exec grep -il 'searchString'
{} \;
# find names of files ending in *log that have both foo and bar
grep -l bar *.log | xargs grep -l foo
# ls only directories
ls -ad */
# Find the 10 lusers winners of the "I take up the most disk space" award
du -sh /home/*|sort -rh|head -n 10
# List only those files that has all uppercase letters in their names (e.g.
README)
ls | grep '^[A-Z0-9]*$'
# Find files that have been modified on your system in the past 60 minutes
sudo find / -mmin 60 -type f
# ls not pattern
ls !(*.gz)
# Remove blank lines from a file using grep and save output to new file
grep . filename > newfilename
Page 23
# Empty a file
truncate -s0 file
# Recursively find top 20 largest files (> 1MB) sort human readable format
find . -mount -type f -printf "%k %p\n" | sort -rg | cut -d \ -f 2- | xargs
-I {} du -sh {} | less
# Recursively search and replace old with new string, inside every instance
of filename.ext
find . -type f -name filename.exe -exec sed -i "s/oldstring/oldstring/g" {}
+;
# Print all open regular files sorted by the number of file handles open to
each.
lsof -a -d 1-99 -Fn / | grep ^n | cut -b2- | sort | uniq -c | sort -n
# underscore to camelCase
echo "hello_world" | sed -r 's/([a-z]+)_([a-z])([a-z]+)/\1\U\2\L\3/'
# Find all the files more than 10MB, sort in descending order of size and
record the output of filenames and size in a text file.
find . -size +10240k -exec ls -l {} \; | awk '{ print $5,"",$9 }'|sort -rn >
message.out
# Archive all files that have not been modified in the last days
find /protocollo/paflow -type f -mtime +5 | xargs tar -cvf /var/dump-
protocollo/`date '+%d%m%Y'_archive.tar`
Comics
# Use curl on Windows to bulk-download the Savitabhabhi Comic Strip (for
Adults)
for /L %%x in (1,1,16) do mkdir %%x & curl -R -e http://www.kirtu.com -o
%%x/#1.jpg http://www.kirtu.com/toon/content/sb%x/english/sb%x_en_[001-
070].jpg
Other Fun
# Rickroll your users when they sudo (doesn't work if ssh to MGTI but
really funny if you set it to play "I'm looking at porn over here!"
very loudly...
Also. Must be in correct directory...find .bash_aliases
echo "alias sudo=\"aplay annoyingsoundfile.ogg\"" >> .bash_aliases