[Linux Commands] Cool Commands [Mini Usage Examples]

Introduction

Hello, this
is Infrastructure Wasshoi Man from the System Solutions Department.
Today I'll be introducing some cool Linux command usage and options!

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"

Isn't this cool?
It's a bit long, isn't it?

$ ps auxwwf | grep http[d]

It's like this.
Whoosh!
It's not like short = cool, though.

*By the way, "Ikashita" is not a dead word.
Words that are dead include "Ikeike" and "Batchugu."

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

systemctl enable --now


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

$ systemctl start httpd $ systemctl enable httpd

Putting this together

$ systemctl enable --now httpd

This is OK.
Whoosh! It's crispy and good.

File backup with brace expansion

Brace expansion is not a command but a bash feature, but it can also be used in some cool ways if used properly.
In fact, it's a feature that's like a treasure trove of cool ways to use it, but I'll just introduce one pattern.

#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 confusing, but
cp test.txt{,_$(date +%Y%m%d)} will be
expanded cp test.txt test.txt_20230114 (written on 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 rmed the file...
Don't give up. There might be a chance.

The rm command is a command that removes the link to the inode.
If all the links to the inode have not disappeared, that is, if there are still processes that have the file open, there is a chance.

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 pull it from "/proc/{process ID}/fd/".
In this case, what is the "cool command"?

I don't know,
but it's cool to get out of a pinch, right?

awk 1

This is often used when combining intermediate certificates.
When combining certificates with cat, there is no line break code at the end.

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

Doesn't it ever happen like this?

This is easy with awk.
Here's 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 is written as
awk 'pattern {action}' If the action is omitted, it will display records that match the pattern.
By using this and passing a pattern of "1 = true", it will match all lines and display them record by record (line by line), so that the records are output neatly line by line.

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, you can do your best again tomorrow.

So, I've written down some command usage tips that I personally hope will become popular. If
you have any tips like these, please let me know in secret. (Or write an article about them. I'll read them.)

thank you very much

If you found this article useful, please click [Like]!
12
Loading...
12 votes, average: 1.00 / 112
2,006
X Facebook Hatena Bookmark pocket

The person who wrote this article

About the author

Infrastructure Wasshoi Man

I work in the System Solutions Department.
I studied every day because I loved it, and luckily Beyond picked me up.
It's been nine years since I started thinking about switching from glasses to contact lenses.