[Linux Commands] Cool Commands [Mini Usage Examples]

Introduction

Hi, this
is Infrastructure Man from the System Solutions Department!
Today I'll be introducing some cool commands and options for Linux!

What does "cool" mean? This is largely a matter of personal taste, but from my perspective, I'd like to give a quick list of some things that will make you go "Wow! Wow! Whoosh!" when you see them

To give a brief example:

$ ps auxwwf | grep httpd | grep -v "grep"

This isn't very cool
, is it? It's a bit long.

$ ps auxwwf | grep http[d]

This is how it should be.
Woohoo!
Not that short necessarily equals cool, though

*By the way, "ikashita" (cool/stylish) is not an obsolete word.
It's words like "ikeike" (cool/enthusiastic) and "batchigu" (perfect/great) that are obsolete.

This is an article introducing some of my favorite commands, so please enjoy it as a way to relax

systemctl enable --now

awesome.
You can start and enable at the same time with systemctl.
is

$ systemctl start httpd $ systemctl enable httpd

Putting this together

$ systemctl enable --now httpd

That's perfect!
Woohoo! It's nice and crisp.

File backup with brace expansion

Brace expansion isn't a command, but a feature of bash, and if used correctly, it can have some really cool uses.
In fact, it's a treasure trove of cool uses, but I'll just introduce one example.

#Dated copy $ cp test.txt{,_$(date +%Y%m%d)} # .org copy $ cp test.txt{,.org}

In this way, if you enter String A{, String B}, String A will be output alone first, followed by the expanded String B

It's a bit complicated, but
`cp test.txt{,_$(date +%Y%m%d)}` expands to cp test.txt test.txt_20230114` `
(Date written: January 14, 2023).

Compare the commands and results, study how they work, and try creating your own cool braces

File recovery using lsof

Oh no, I accidentally deleted the file...
Don't give up. There might still be a chance.

The `rm` command removes links to inodes.
If the links to the inodes haven't completely disappeared, meaning there's still a process open that file, there's a chance it might work.

Below is a demo

$ echo "hogehoge" > hoge.txt $ less hoge.txt # Pause the less process Ctrl + z # Delete $ rm hoge.txt # Get the process ID $ lsof | grep "hoge.txt" less 92 takeda 4r REG 8,16 9 1994 /home/takeda/hoge.txt (deleted) # Restore with the cp command $ cp /proc/92/fd/4 ./fuga.txt # Confirm $ cat fuga.txt hogehoge

The key is to retrieve it from "/proc/{process ID}/fd/".
In this case, which command would be considered "cool"?

I don't know.
But getting out of a tight spot is pretty cool, right?

awk 1

This is often used when merging intermediate certificates.
When merging certificates with cat, there is no newline character on the last line.

—–BEGIN CERTIFICATE—– Server certificate file contents —–END CERTIFICATE—–—–BEGIN CERTIFICATE—– Intermediate certificate file contents —–END CERTIFICATE—–

Doesn't it ever happen like this?

Using awk makes it easy.
The following is a demo.

$ cat test.pem —–BEGIN CERTIFICATE—– Contents of the issued server certificate file —–END CERTIFICATE—– $ cat test.ca —–BEGIN CERTIFICATE—– Contents of the intermediate certificate file —–END CERTIFICATE—– $ awk 1 test.pem test.ca > test.chain $ cat test.chain —–BEGIN CERTIFICATE—– Contents of the issued server certificate file —–END CERTIFICATE—– —–BEGIN CERTIFICATE—– Contents of the intermediate certificate file —–END CERTIFICATE—–

That's cool!

awk isawk 'pattern {action}'written as
If the action is omitted, it displays the records that match the pattern.
By using this and passing a pattern of "1 = true", it matches all rows and displays them one record at a time (one row at a time), resulting in neat output of one line at a time.

This is the same technique as the famous SQL injection example "WHERE id='hoge' or 'A'='A'". Pass true. (Bad analogy?)

summary

Just a little bit of
cool command skills would be enough; if I could just use a few cool commands, I'd be able to keep going tomorrow too.

So, I've written down a few command usages that I personally think would be nice if they became popular. If
you know of any others, please let me know discreetly. (Or write an article about them; I'll read it.)

thank you very much

If you found this article helpful,please give it a "Like"!
12
Loading...
12 votes, average: 1.00 / 112
2,110
X Facebook Hatena Bookmark pocket

The person who wrote this article

About the author

Infrastructure Wasshoi Man

I belong to the Systems Solutions Department.
I was lucky enough to be hired by Beyond because I enjoyed studying every day.
It's been nine years since I started debating whether to switch from glasses to contact lenses.