[Linux commands] Cool commands [Mini usage examples]
table of contents
Introduction
Hello,
I'm Infrastructure Wasshoi Man from the System Solutions Department.This
time I'll be introducing cool command usage and options around Linux!
What does "it's cool" mean? This really depends on your personal tastes, but from my Takeda perspective, I would like to be able to easily point out a few things that make you go, "Oh! I'm doing it! Huh!" when you see them.
To give a brief example,
$ ps auxwwf | grep httpd | grep -v "grep"
I wonder if this is cool.
It's a bit long, isn't it?
$ ps auxwwf | grep http[d]
It's like this after all.
Huh!
It doesn't necessarily mean that it's short = cool though.
*By the way, "Ikishita" is not a dead word.
The ones that are dead are things like ``Ikeike'' and ``Batchgoo.''
This is an article that introduces my personal favorite commands, but please enjoy it as much as a breather.
systemctl enable --now
This is crazy.
You can use systemctl to run start and enable at the same time.
In other words
$ systemctl start httpd $ systemctl enable httpd
put this together
$ systemctl enable --now httpd
This is OK.
Huh! It feels crispy and good.
File backup with brace expansion
Brace expansion is not a command, but a feature of bash, but if used properly, it can be used in some cool ways.
In other words, it's like a treasure trove of cool ways to use it, but I'll show you just one pattern.
#Copy with date $ cp test.txt{,_$(date +%Y%m%d)} #Copy with .org $ cp test.txt{,.org}
In this way, if you write string A{, string B}, first string A will be output alone, and then the expanded string B will be output.
It's confusing, but
cp test.txt{,_$(date +%Y%m%d)}
is
expanded cp test.txt test.txt_20230114
(Written date and time: January 14, 2023)
Compare commands and results, study how they move, and develop your own cool braces.
File recovery using lsof
Oh no, I rm'd the file...
don't give up. There might be one chance.
The rm command removes links to inodes.
If all the links to the inode have not disappeared, that is, if there are still processes opening the file, there is a chance.
Below is a demo.
$ echo "hogehoge" > hoge.txt $ less hoge.txt # less Pause process Ctrl + z # Delete $ rm hoge.txt # Get process ID $ lsof | grep "hoge.txt" less 92 takeda 4r REG 8, 16 9 1994 /home/takeda/hoge.txt (deleted) # Restore with cp command $ cp /proc/92/fd/4 ./fuga.txt # Confirm $ cat fuga.txt hogehoge
The key is to pull it out from "/proc/{process ID}/fd/".
In this case, what is the "cool command"?
I don't understand.
But wouldn't it be cool to be able to get through a pinch?
awk 1
This is often used when combining intermediate certificates.
When combining certificates with cat, there is no line feed code on the last line.
—–BEGIN CERTIFICATE—– Contents of the server certificate file —–END CERTIFICATE—–—–BEGIN CERTIFICATE—– Contents of the intermediate certificate file —–END CERTIFICATE—–
Do you ever feel like this?
It's easy using awk.
Below 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—– Intermediate certificate File description contents —–END CERTIFICATE—–
It's so cool!
awk is written as
awk 'pattern {action}'
And if the action is omitted, records that match the pattern will be displayed.
By using this and passing the pattern ``1 = true'' to match all lines and displaying it record by record (one line at a time), it will be output neatly 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 example?)
summary
just a few
cool commands, I'd be able to do my best again tomorrow.
So, I wrote down some ways to use commands that I personally think would become popular.
If you have something like that, please let me know. (Or write an article. I'll read it.)
thank you very much.