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
Programmer DD
Essential Linux Commands: pgrep, pstree, bc, split, nl, ldd, col, lsof Explained

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
28719
pgrep 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 sshd

pstree

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
quit

split

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 xaf
File merging:
cat xa* > mysql.tar

nl

Similar to cat but adds line numbers.
nl rumenz.sh
     1  123
     2  345
     3  rumenz.com
     4  111
     5  222

ldd

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.txt

lsof

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 processes
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
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 contactadmin@besthub.devand we will review it promptly.

process managementSystem AdministrationBashcommand-line
Programmer DD
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

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.