Posted: . At: 11:16 AM. This was 3 years ago. Post ID: 14809
Page permalink. WordPress uses cookies, or tiny pieces of information stored on your computer, to verify who you are. There are cookies for logged in users and for commenters.
These cookies expire two weeks after they are set.


Some more very useful Linux tips for scripting fans.


How to get the total size of a folder using Linux scripting.

┌──[jason@192.168.1.2][~/Documents]
└──╼  ╼ $ du -ack -BM | sort -nr | head -n 1 | awk '{print $1}'
30255M

This shows the total size of a folder in megabytes easily. This would be very useful to have in a script to do something with the returned value.

Print the last used command in the bash shell.

┌──[jason@192.168.1.2][~/Documents]
└──╼  ╼ $ echo !!
echo finger -lmps $LOGNAME | sed -e "s/On/Logged in/g" | grep "since"

Return just the IP address of an interface with the command line.

┌──[jason@192.168.1.2][~/Documents]
└──╼  ╼ $ ip a | awk '/inet / { print $2 }' | sed -n 2p | cut -d "/" -f1
192.168.1.2

And this is how to get just your Internet-facing IP address with a simple command.

┌──[jason@192.168.1.2][~/Documents]
└──╼  ╼ $ dig @ns1-1.akamaitech.net ANY whoami.akamai.net +short
203.192.407.302

Return just the Interface name with this one-liner.

┌──[jason@192.168.1.2][~/Documents]
└──╼  ╼ $ ip a | awk '/: / { print $2 }' | sed -n 2p | cut -d ":" -f1
enp3s0

These commands should be very useful when placed in a script. Especially getting the interface name of the current network interface and the IP address. That could be very useful to someone.

These commands may be used to create a variable as well.

┌──[jason@192.168.1.2][~/Documents]
└──╼  ╼ $ IFACE=$(ip a | awk '/: / { print $2 }' | sed -n 2p | cut -d ":" -f1)

This will create a variable $IFACE containing the name of the current network interface.

Now that the interface name is in a variable, it may then be used in a script or simple command.

┌──[jason@192.168.1.2][~/Documents]
└──╼  ╼ $ sudo ethtool $IFACE
[sudo] password for jason: 
Settings for enp3s0:
	Supported ports: [ TP MII ]
	Supported link modes:   10baseT/Half 10baseT/Full 
	                        100baseT/Half 100baseT/Full 
	Supported pause frame use: No
	Supports auto-negotiation: Yes
	Supported FEC modes: Not reported
	Advertised link modes:  10baseT/Half 10baseT/Full 
	                        100baseT/Half 100baseT/Full 
	Advertised pause frame use: No
	Advertised auto-negotiation: Yes
	Advertised FEC modes: Not reported
	Speed: 10Mb/s
	Duplex: Half
	Port: MII
	PHYAD: 32
	Transceiver: internal
	Auto-negotiation: on
	Supports Wake-on: pumbg
	Wake-on: d
	Current message level: 0x00000007 (7)
			       drv probe link
	Link detected: no

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.