How to list all TCP ports in Linux ?
In this post, we will check how to list all TCP ports in Linux.
Lets us quickly check the command and output:
[root@nglinux ~]# cat /etc/services | grep -Po '^\w+.*\d{1,5}/tcp' | head tcpmux 1/tcp rje 5/tcp echo 7/tcp discard 9/tcp systat 11/tcp daytime 13/tcp qotd 17/tcp msp 18/tcp chargen 19/tcp ftp-data 20/tcp [root@nglinux ~]# cat /etc/services | grep -Po '^\w+.*\d{1,5}/tcp' | wc -l 5374
Command Description
The command greps:
a. the exact matching output(-o)
b. using the text searched by perl command(-P)
c. ‘^\w+.*\d{1,5}/tcp’ is the perl command to grep a word(\w) followed by digits(\d) and /tcp.
In case your system has different file than /etc/services, you can use wildcards to get all files matching the pattern.
[root@nglinux ~]# cat /e*/s*s | grep -Po "^\w+.*\d{1,5}/tcp" | head tcpmux 1/tcp rje 5/tcp echo 7/tcp discard 9/tcp systat 11/tcp
I hope you liked the post. Please do comment below in case of any suggestions/queries.