Bash Tricks
I spend some time using the terminal to do most of my chores and I love to share whatever tricks that makes life less stressful for me. Today I will be sharing with us a sed command trick that helps you output a command’s result on single lines and then also use the less command to scroll over the results and search for words of interest. The command is this:
sudo firewall-cmd — get-services | sed ‘s/[[:space:]]/\n/g’ | less
Let me breakdown this composite command so we can appreciate the usefulness of the sed and less commands here. The first part sudo firewall-cmd — get-services, gets the list of firewall-cmd services that can be enabled using their names, and the result would have been as seen below:
RH-Satellite-6 amanda-client amanda-k5-client bacula bacula-client bgp bitcoin bitcoin-rpc bitcoin-testnet bitcoin-testnet-rpc ceph ceph-mon cfengine condor-collector ctdb dhcp dhcpv6 dhcpv6-client dns docker-registry docker-swarm dropbox-lansync elasticsearch freeipa-ldap freeipa-ldaps freeipa-replication freeipa-trust ftp ganglia-client ganglia-master git high-availability http https imap imaps ipp ipp-client ipsec irc ircs iscsi-target kadmin kerberos kibana klogin kpasswd kprop kshell ldap ldaps libvirt libvirt-tls managesieve mdns minidlna mosh mountd ms-wbt mssql murmur mysql nfs nfs3 nrpe ntp openvpn ovirt-imageio ovirt-storageconsole ovirt-vmconsole pmcd pmproxy pmwebapi pmwebapis pop3 pop3s postgresql privoxy proxy-dhcp ptp pulseaudio puppetmaster quassel radius redis rpc-bind rsh rsyncd samba samba-client sane sip sips smtp smtp-submission smtps snmp snmptrap spideroak-lansync squid ssh synergy syslog syslog-tls telnet tftp tftp-client tinc tor-socks transmission-client vdsm vnc-server wbem-https xmpp-bosh xmpp-client xmpp-local xmpp-server zabbix-agent zabbix-server
Now this as first glance is super difficult to read and instantly pick out a service or services of interest with this the part sed ‘s/[[:space:]]/\n/g’ picks out every space and replaces each of them with a newline character and we get the following results:
…
snmptrap
spideroak-lansync
squid
ssh
synergy
syslog
syslog-tls
telnet
tftp
tftp-client
tinc
tor-socks
transmission-client
vdsm
vnc-server
wbem-https
xmpp-bosh
xmpp-client
xmpp-local
xmpp-server
zabbix-agent
zabbix-server
This is nice but we still need to spend some time finding what we are looking for. We now use less command to make scrolling easier and provide search capabilities and this is what the less part does. Remember those “|” are pipes that pass one command’s output into another's for use. And the final result is this “scrollable” and “searchable” output:
RH-Satellite-6
amanda-client
amanda-k5-client
bacula
bacula-client
bgp
bitcoin
bitcoin-rpc
bitcoin-testnet
bitcoin-testnet-rpc
ceph
ceph-mon
cfengine
condor-collector
ctdb
dhcp
dhcpv6
dhcpv6-client
dns
docker-registry
docker-swarm
dropbox-lansync
:
The terminal might seem scary at first but with time and some practice we can overcome and enjoy it more.