Linux terminal custom commands (super practical skills)

What is recorded today is a skill to experience the operating experience of linux terminal.

The technique is described as: Any command in the linux terminal can be replaced with other strings . At the same time, you can also use .sh or python scripts to customize commands.

How to play specifically? Looking down gradually, there will be more and more surprises .

For example, I think the nvidia-smi for viewing the GPU running status is too long, and every input is troublesome. I want to change it to "mygpu".

The operation is as follows:

vim ~/.bashrc

Add a sentence at the end of the text:

alias mygpu='nvidia-smi'

Then :wq save and launch, and then source:

source ~/.bashrc

At this time, your Linux terminal will recognize the command'mygpu':

Ok, in the same way, we can change "watch -n 1 nvidia-smi" to "watchgpu":

In this way, the gpu information can be updated every second. Access complex instructions through simple names defined by yourself.

In addition, you can also change complex commands such as file count and file search into your own custom commands. This can be free to use imagination.

Is it the only way?

That's not the case, alias supports calling python scripts or .sh scripts.

I will give you a sample, you can refer to it:

1. Create a python script:

cd
mkdir sys_scripts
cd sys_scripts
vim cmd1.py

2. Edit the cmd1.py script:

for i in range(10):
    print(i)

Press esc, then :wq to save and exit.

3. Edit .bashrc in the same way and add the following line:

alias printnum='python ~/sys_scripts/cmd1.py'

Then source it. Then, enter printnum in the terminal to get the result

You can show your creativity in the preparation of python files.


to sum up

Using python scripts, we can achieve a complete degree of shell command customization.

Guess you like

Origin blog.csdn.net/leviopku/article/details/108087765