List Top Processes sorted by RAM or CPU Usage in Linux
The following command will show the list of top processes ordered by RAM and CPU use in descendant form (remove the pipeline and head if you want to see the full list):
# ps -eo pid,ppid,cmd,%mem,%cpu --sort=-%mem | head
Sample Output
PID PPID CMD %MEM %CPU 2591 2113 /usr/lib/firefox/firefox 7.3 43.5 2549 2520 /usr/lib/virtualbox/Virtual 3.4 8.2 2288 1 /home/gacanepa/.dropbox-dis 1.4 0.3 1889 1543 c:\TeamViewer\TeamViewer.ex 1.0 0.2 2113 1801 /usr/bin/cinnamon 0.9 3.5 2254 2252 python /usr/bin/linuxmint/m 0.3 0.0 2245 1801 nautilus -n 0.3 0.1 1645 1595 /usr/bin/X :0 -audit 0 -aut 0.3 2.5
Find Largest Directories in Linux
If you want to display the biggest directories in the current working directory, run:
[root@vpsforums var]# du -a | sort -n -r | head -n 5
Sample Output
[root@vpsforums var]# du -a | sort -n -r | head -n 5 456948 . 259528 ./lib 213612 ./lib/mysql 107080 ./cache 107040 ./cache/yum
Find Largest File in a folder
# find /home/ -type f -exec du -Sh {} + | sort -rh | head -n 5 OR # find /home/ -type f -printf "%s %p\n" | sort -rn | head -n 5
Sample Output
[root@vpsforums var]# find /var -type f -exec du -Sh {} + | sort -rh | head -n 5 77M /var/lib/mysql/ibdata1 48M /var/lib/mysql/ib_logfile1 48M /var/lib/mysql/ib_logfile0 36M /var/lib/rpm/Packages 31M /var/cache/yum/x86_64/7/updates/gen/primary_db.sqlite
List Number of open connections per ip
# netstat -ntu | awk '{print $5}' | cut -d: -f1 | sort | uniq -c | sort -n
Sample Output
[root@vpsforums var]# find /var -type f -exec du -Sh {} + | sort -rh | head -n 5 1 103.9.77.19 1 104.106.253.241 2 104.245.164.30 2 104.27.143.145 2 108.162.219.149 3 24.199.191.90 3 31.7.58.170 4 104.113.68.225 4 104.31.17.3 4 121.18.238.125 5 104.16.228.14 5 104.27.216.28 5 188.210.191.25 5 216.58.193.163 5 216.58.193.174 6 104.16.227.14 7 34.204.96.230 7 59.45.175.88 8 104.20.72.110 9 52.27.117.94 10 208.77.17.137 11 104.27.212.30 11 136.243.206.150 11 78.129.196.136 12 174.35.56.108 19 104.20.71.110 99 202.151.15.226 103 209.197.3.84
List processes by disk I/O usage
We need iotop for this purpose.
Install IOTOP
wget http://guichaz.free.fr/iotop/files/iotop-0.5.tar.gz tar zxfv iotop-0.5.tar.gz cd iotop-0.5 python setup.py install cd ../ rm -rfv iotop-0.5*
# iotop
Sample Output
In IOTop, the processes with most usage are shown to the top of the list.
Find out what program/process is listening on port/ip address
# netstat -nap | grep LISTEN
Sample Output tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 202479/sshd tcp 0 0 0.0.0.0:25 0.0.0.0:* LISTEN 322015/master tcp 0 0 127.0.0.1:6379 0.0.0.0:* LISTEN 2280/redis-server 1 tcp 0 0 127.0.0.1:3350 0.0.0.0:* LISTEN 53405/xrdp-sesman tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 517460/sendmail tcp 0 0 0.0.0.0:4190 0.0.0.0:* LISTEN 121190/dovecot tcp 0 0 127.0.0.1:3306 0.0.0.0:* LISTEN 340177/mysqld tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN 592436/rpcbind tcp 0 0 0.0.0.0:8085 0.0.0.0:* LISTEN 219820/./worldserve tcp 0 0 0.0.0.0:587 0.0.0.0:* LISTEN 365217/master tcp 0 0 127.0.0.1:587 0.0.0.0:* LISTEN 1046398/sendmail: M tcp 0 0 0.0.0.0:52951 0.0.0.0:* LISTEN 71834/(squid-1) tcp 0 0 0.0.0.0:443 0.0.0.0:* LISTEN 534376/apache2 tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN 53398/cupsd tcp 0 0 0.0.0.0:10013 0.0.0.0:* LISTEN 609144/mudhealthd tcp 0 0 0.0.0.0:8282 0.0.0.0:* LISTEN 1004877/tinyproxy tcp 0 0 0.0.0.0:21 0.0.0.0:* LISTEN 156656/vsftpd tcp 0 0 0.0.0.0:48221 0.0.0.0:* LISTEN 50542/apache2 tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 416871/apache2 tcp 0 0 0.0.0.0:9977 0.0.0.0:* LISTEN 946021/sshd tcp 0 0 127.0.0.2:53 0.0.0.0:* LISTEN 130588/named
In the above output, if take the first line, it tell us that a process named SSHD with PID 202479 is listening on port 22, tcp protocol, on all available system ip addresses
List processes using swap
# for file in /proc/*/status ; do awk '/VmSwap|Name/{printf $2 " " $3}END{ print ""}' $file; done | sort -k 2 -n -r
Sample Output mysqld 593432 kB worldserver 528624 kB mudflow 450760 kB mongod 448096 kB mysqld 447140 kB java 358780 kB systemd 301720 kB mysqld 273256 kB java 252704 kB node 225476 kB mysqld 138144 kB python 129964 kB mongod 120812 kB mysqld 116868 kB mysqld 116644 kB mysqld 104856 kB mysqld 104388 kB mysqld 100088 kB