top of page

Posts

Improving Ways to Use Linux Commands



I’m going to introduce Linux commands and options that are smartly used.

For example,


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

This one isn’t cool, it’s too long I think.


$ ps auxwwf | grep http[d]

This one is better. I wouldn’t say shorter is better, but it’s easier to read.


systemctl enable --now


This command and option are better because you can execute start and enable with systemctl at the same time.


$ systemctl start httpd
$ systemctl enable httpd

This can be turned into the following below.


$ systemctl enable --now httpd


File backup with brace expansion


Brace expansion is one kind of tool in bash, but if you know how to use this tool, it’s very useful.


I’m going to show you one example.


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

Just like the example above, if you make string A{,string B}, string A is output by itself first, and then expanded string B is output after.


It’s a little bit confusing but

cp test.txt{,_$(date +%Y%m%d)} is expanded to be cp test.txt test.txt_20230114


There are many ways of using brace expansion, you can compare the commands and the results, and study how to use it in an efficient way.


File recovery using the lsof


Oops! I accidentally ”rm”ed the file…

Don’t give up. You might still have a chance.


The rm commands a command to delete the link from inode.

If there is the link inode left, you might have a chance to get the file back.


The following is a demonstration.


$ 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)
 
# recover with cp command
$ cp /proc/92/fd/4 ./fuga.txt
 
# check
$ cat fuga.txt
hogehoge

The point is pulling up from “/proc/{process ID}/fd/”.


In this case, it is cool to have a life-saving solution, isn’t it?


awk 1


This one is often used to bind intermediate certifications.

Have you experienced that when you bind intermediate certification using cat, there’s no line break code that looks like the following?


—–BEGIN CERTIFICATE—–
Detail of certification of the server
—–END CERTIFICATE—–—–BEGIN CERTIFICATE—–
Detail of certification of the server
—–END CERTIFICATE—–

It’s very easy if you use the awk command. The following is a demonstration.


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

It’s so cool!

The awk can be represented by “awk ‘pattern {action}’”, and then when the action is omitted, it displays the record that matches the pattern.

This is used to match all rows by passing the pattern "1 = true" and displaying them row by row so that the output is neatly displayed one row at a time.


It's the same method of common SQL injection of "WHERE id='something' or 'A'='A''', it passes true.


Summary


If you know these tricks, that makes your life much easier!




This blog post is translated from a blog post written by Wataru Takeda on our Japanese website Beyond Co..


bottom of page