Making easy commands hard because..

dcbrown73

Well-Known Member
Joined
Jul 14, 2021
Messages
365
Reaction score
341
Credits
3,224
Making easy commands hard because we can learn something about Linux in the process!

Question:
How do we see how many days the Linux server has been up?

Easy Answer:
Code:
[email protected]:~ $ uptime
20:57:11 up 25 days, 23:07,  2 users,  load average: 0.00, 0.02, 0.00
Hard Answer:
Code:
[email protected]:~ $ cat /proc/uptime | awk '{print $1 / 86400}' | xargs printf "%.2f days\n"
25.97 days

What did we learn here? (#1)
We learned there is a file in this /proc filesystem called uptime. What the hell is /proc and why is it keeping track of my uptime?

The /proc filesystem is like an x-ray into the several parts of the Linux system! Many utilities you use to get the current state of your Linux system actually read the information from proc!

If you cat /proc/uptime it will return two numbers separated by a space.

Code:
[email protected]:~ $ cat /proc/uptime
2244412.34 8841072.57
[email protected]:~ $
The first number is the total amount of seconds the system has been up. Hence the fact that I divided it by 86,400 in the command I issued. There are 86,400 seconds in each day!
The second number is how many seconds the core system was idle since it has been up. Obviously, the second number should always been smaller than the first.

What did we learn here? (#2)
We learned that the awk command is one of the most powerful command line tools on the face of the planet for a budding young Linux Ninja.

As any command line Linux user knows. Some commands you run will give you all kinds of information you are not interested in. (for instance, how many seconds the core system has been idle! :oops: ) Sometimes, you need to filter that (in a French accent) gar-baaage. Awk to the rescue!

When you feed data to awk, one of the great features it has is the ability to grab and print out a specific piece of text that you're interested in. By default, the delimiter is a space and the variables to use are named with a $ followed by the index of the piece of data you want to print. In our case, we want the first one as it's uptime in seconds. So, we will tell awk to print $1. Though, we don't want to stop there. We want to use awk to do some math! So we print the data we want and then tell awk to also divide it by the total seconds in a day!
Code:
awk '{print $1 / 86400}'

The downside is it gives you a decimal with many numbers after the decimal point. Way more than you need!
Code:
[email protected]:~ $ cat /proc/uptime | awk '{print $1 / 86400}'
25.9842
[email protected]:~ $
Yeah, definitely need to trim that down a bit. Lets do this!

What did we learn here? (#3)

While the command line doesn't show you this. The printf command does not like you piping data to it! It prefers you provide the variables after you specify the format!

Example:
Code:
[email protected]:~ $ printf "%s is teaching dumb Linux command line tricks\n" Dave
Dave is teaching dumb Linux command line tricks
[email protected]:~ $

If you try to pipe Dave into printf, it ignores you like when you ask your wife when dinner will be on the table.

Code:
[email protected]:~ $ echo Dave | printf "%s is teaching dumb Linux command line tricks\n"
is teaching dumb Linux command line tricks
[email protected]:~ $

What did we learn here? (#4)

Well, then how was I able to pipe the seconds into printf?

Well, we learned that the xargs command is your friend!

xargs is what gave Superman his super powers! Due to the craziness of what xargs is, I will just quote from the man page.
xargs reads items from the standard input, delimited by blanks (which can be protected with double or single quotes or a backslash) or newlines, and executes the command (default is /bin/echo) one or more times with any initial-arguments followed by items read from standard input.

In layman's terms. It lets me perform magic! I run xargs and then put my command after it. Since I piped in the variable, it includes that variable that was piped in as if it was an argument pasted to it! Yeah, MAGIC!!!

Example: I will echo the path to my home directory to xargs running the command ls.
Code:
[email protected]:~ $ echo "/home/pi" | xargs ls
Desktop  Documents  Downloads  Go  Music  Pictures  Projects  Public  Templates  Videos  oldconffiles  python_games
[email protected]:~ $

That's some Linux Command Line Ninja stuff right there!

Now, lets put it all together and get how many days my pivpn server has been up!
Code:
[email protected]:~ $ cat /proc/uptime | awk '{print $1 / 86400}' | xargs printf "%.2f days\n"
25.99 days
[email protected]:~ $

Take Away
Yes, this was the hard way to get your Linux machine's uptime, but that wasn't the point. The point is learning the power that is at your finger tips while you are at the command line. There is a reason Microsoft has started to mimic and even implement Linux within their own environment. Have you ever try to do something like this at the DOS prompt? lol

I hope someone learns something here and it's not how to get your uptime the hard way. It's to open your mind to fun it can be just to figure things like this out. If your a puzzle guy or just love a challenge. The Linux command line is full of puzzles and challenges.

Enjoy!
 


wizardfromoz

Administrator
Staff member
Gold Supporter
Joined
Apr 30, 2017
Messages
8,660
Reaction score
7,607
Credits
35,522
I have only got time to read this today, that is, a month later, and I am pinning it because
  1. I think it is excellent and
  2. At time of reading it was about to trickle into page 2, and I don't want to see it lost
Avagudweegend, all

Wizard
 

Condobloke

Well-Known Member
Joined
Apr 30, 2017
Messages
6,130
Reaction score
5,106
Credits
38,853
Take Away
Yes, this was the hard way to get your Linux machine's uptime, but that wasn't the point. The point is learning the power that is at your finger tips while you are at the command line. There is a reason Microsoft has started to mimic and even implement Linux within their own environment. Have you ever try to do something like this at the DOS prompt? lol
tick.jpeg
 
MALIBAL Linux Laptops

Linux Laptops Custom Built for You
MALIBAL is an innovative computer manufacturer that produces high-performance, custom laptops for Linux.

For more info, visit: https://www.malibal.com

Members online


Latest posts

Top