Operations 8 min read
Essential Linux Commands: pgrep, pstree, bc, split, nl, ldd, col, lsof Explained
This guide introduces a collection of essential Linux command-line tools—including pgrep, pstree, bc, split, nl, ldd, col, and lsof—explaining their purposes, common options, and practical examples for process inspection, file manipulation, mathematical calculations, and system diagnostics.
Programmer DD
Programmer DD
pgrep
pgrep name starts with p, indicating it is related to processes; it lists process IDs.
Find SSH server PID
pgrep ssh
1529
28439
28442
28719pgrep prints each matching PID on a separate line; the -d option lets you specify a delimiter.
pgrep ssh -d " "
1529 28439 28442 28719 28810 28813-l option shows name and ID
pgrep -l ssh
1529 sshd
28439 sshd
28442 sshd
28719 sshdpstree
This command displays the process hierarchy as a tree.
systemd─┬─NetworkManager───2*[{NetworkManager}]
├─VGAuthService
├─abrt-watch-log
├─abrtd
├─agetty
├─atd
├─auditd───{auditd}
├─crond
├─dbus-daemon───{dbus-daemon}
├─firewalld───{firewalld}
├─gssproxy───5*[{gssproxy}]
├─irqbalance
├─java───28*[{java}]
├─lsmd
├─lvmetad
├─master─┬─pickup
│ └─qmgr
├─mcelog
├─nginx───nginx
├─ntpd
├─polkitd───6*[{polkitd}]
├─redis-server───4*[{redis-server}]
├─rngd
├─rpcbind
├─rsyslogd───2*[{rsyslogd}]
├─smartd
├─sshd─┬─sshd───sshd───sftp-server
│ └─sshd─┬─bash───pstree
│ └─sftp-server
├─systemd-journal
├─systemd-logind
├─systemd-udevd
├─tuned───4*[{tuned}]
└─vmtoolsd───2*[{vmtoolsd}]bc
This command provides high-precision arithmetic, such as square-root calculations. Example script (sqrt) uses bc.
#!/bin/bash
if [ $# -ne 1 ]
then
echo 'Usage: sqrt number'
exit 1
else
echo -e "sqrt($1)
quit
" | bc -q -i
fi ./sqrt.sh 123.00
sqrt(123.00)
11.09
quitsplit
Splits a large file into smaller pieces.
split -b 100m mysql.tar
ls -lh
-rw-r--r--. 1 root root 517M Jun 21 22:35 mysql.tar
-rw-r--r--. 1 root root 100M Jun 21 22:35 xaa
-rw-r--r--. 1 root root 100M Jun 21 22:35 xab
-rw-r--r--. 1 root root 100M Jun 21 22:35 xac
-rw-r--r--. 1 root root 100M Jun 21 22:35 xad
-rw-r--r--. 1 root root 100M Jun 21 22:35 xae
-rw-r--r--. 1 root root 17M Jun 21 22:35 xafFile merging:
cat xa* > mysql.tarnl
Similar to cat but adds line numbers.
nl rumenz.sh
1 123
2 345
3 rumenz.com
4 111
5 222ldd
Shows the shared libraries required by an executable.
ldd /usr/bin/ls
linux-vdso.so.1 => (0x00007ffdb51ba000)
libselinux.so.1 => /lib64/libselinux.so.1 (0x00007f4518ead000)
libcap.so.2 => /lib64/libcap.so.2 (0x00007f4518ca8000)
libacl.so.1 => /lib64/libacl.so.1 (0x00007f4518a9f000)
libc.so.6 => /lib64/libc.so.6 (0x00007f45186d1000)
libpcre.so.1 => /lib64/libpcre.so.1 (0x00007f451846f000)
libdl.so.2 => /lib64/libdl.so.2 (0x00007f451826b000)
/lib64/ld-linux-x86-64.so.2 (0x00007f45190d4000)
libattr.so.1 => /lib64/libattr.so.1 (0x00007f4518066000)
libpthread.so.0 => /lib64/libpthread.so.0 (0x00007f4517e4a000)col
Converts man pages to plain text.
PAGER=cat
man ls | col -b > ls.txtlsof
Lists open files and the processes using them.
lsof rumenz.txt # show processes that opened rumenz.txt
lsof -c nginx # show files opened by processes whose name starts with nginx
lsof -p 1234 # list files opened by PID 1234
lsof -g gname/gid # show processes belonging to group name or gid
lsof -u uname/uid # show processes belonging to user name or uid
lsof +d /usr/local/ # show files opened under directory
lsof +D /usr/local/ # recursively search directory (slower)
lsof -d 4 # show processes using file descriptor 4
lsof -i # show network-related processesOriginal Source
Signed-in readers can open the original source through BestHub's protected redirect.
Republication Notice
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Written by
Programmer DD
A tinkering programmer and author of "Spring Cloud Microservices in Action"
0 followers
Reader feedback
How this landed with the community
Rate this article
Was this worth your time?
Discussion
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
