Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the ten command lines that save time for Linux administrators?

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

What are the ten command lines that save time for Linux administrators? for this question, this article introduces the corresponding analysis and solutions in detail, hoping to help more partners who want to solve this problem to find a simpler and easier way.

Although the Linux desktop has undergone dramatic changes over the past two decades (perhaps the most significant change is the Ubuntu Unity interface), the command line is still non-parallel based on the power that can be provided to experienced system administrators. Although most of the next ten tips are about Bash shell, all of them can be easily applied to other current shell.

1. Use a command to create and enter the directory

Creating and then entering a new directory is a very common task, and it seems that there should be shortcuts to executing both commands in shell. Although there are no shortcuts, you can add the following functions to your .bashrc file:

Mkcd () {mkdir $1cd $1}

Then run the source .bashrc to read the changes in memory, and use the mkcd command to complete two services:

Wjgilmore@ubuntu:~$ mkcd articleswjgilmore@ubuntu:~/articles $

two。 Go back to the previous directory

When you need to move from a complex directory and want to go back to the original directory, you can pass it all the way to the cd command, but the little-known cd command makes it trivial. This order shows the behavior:

Wjgilmore@ubuntu-laptop:~/Documents/techtarget_articles/ten_command_line_tricks/test2 $cdwjgilmore@ubuntu-laptop:~$ cd-~ / Documents/techtarget_articles/ten_command_line_tricks/test2 $wjgilmore@ubuntu-laptop:~/Documents/techtarget_articles/ten_command_line_tricks/test2 $

3. Create a catalog bookmark

Continue to follow the theme of directory interaction, and you will inevitably return to some directories over and over again. You can create bookmarks that allow you to quickly navigate to these directories by adding the path to the $CDPATH shell variant (in your .bashrc file):

CDPATH='.:/home/wjgilmore/books'

Once added, you can navigate directly to the bookmarks directory from anywhere in the operating system path by executing the following command:

$cd books

4. Skillfully edit the command line

How many times have you been tediously editing and executing a series of slightly different commands? For example, when I am busy creating PDF versions of different book chapters from the Markdown source, I usually execute the following command:

$pandoc-o html/chapter06.html chapters/chapter06.md-- template=templates/html.template

To also create chapter04.md source files, command-line beginners will soon get tired of using the up arrow to execute the (previous) command before retrieving in history, and then use the left arrow until all chapter06.md instances have been replaced with chapter04.md. There are many more efficient ways to perform this task. First, consider editing keyboard shortcuts using Bash's command line (two support modes: Emacs and vi), which allows you to quickly navigate to where you want to go:

Ctrl + a: go to the first line

Ctrl + e: go to the last line

Alt + f: one word forward

Alt + b: step back one word

The second and possibly more efficient method is to use command line substitution. The next command replaces the 06 found in the previous execution with 04:

$pandoc-o html/chapter06.html chapters/chapter06.md-- template=templates/html.template$!: gs/06/04pandoc-o html/chapter04.html chapters/chapter04.md-- template=templates/html.template

If you are accidentally using a GNOME terminal, the Alt key will not work as described, because the GNOME terminal has bound the Alt key to the toolbar command. You can also choose to use Shift + Alt as meta-keys, but this is a bit clumsy. Conversely, if you don't need toolbar command shortcuts, turn them off by navigating to Edit-> keyboard shortcuts. You can also turn off the enable menu access key option.

5. Save the long command for later use

When passing a list of tasks operated by a system administrator, you can enter a specific long command and be aware of omitting a step in the sequence before executing it. Instead of deleting the command, you can save it to history without adding the # key to the beginning of the command:

$# this is some ridiculously long command that I want to save

After pressing the Enter key, use the up arrow and you will see the saved command. To execute this command, simply remove the # key from the beginning of the line before execution.

6. Save input using command alias

The long list format (ls-l) of the ls command can be used frequently, but hyphens make it a bit impractical when typing in large quantities. You can use the alias command in .bashrc to create command aliases for long commands. In this example, the command alias dir is an alternative to ls-l.

Alias dir='ls-l'

7. Save more input by ignoring input errors

You are in the terminal area, firing from one directory to the next when copying, updating, and removing files at will. Or you are not in the terminal area, because the fingers move faster than the brain, and even the keyboard reaction time can be processed, which causes you to keep going back and correcting your typing errors. Add the following line to your .bashrc file, and shell will automatically correct all typing errors when identifying the file or path name.

Shopt-s cdspell

8. Open the application in the background

When patrolling the command line, you may need to perform another task, such as responding to an email. Of course, you can open GUI applications from the terminal the way you execute other commands, simply by calling their names. In this example, open Gimp:

$gimp

But doing so effectively ends your terminal session because the application opens in the foreground. If you are routinely opening a specific program from the command line, consider changing its default call in your .bashrc file:

Gimp () {command gimp "$@" &}

By reloading your .bashrc file (see source command), you will be able to call the Gimp application and pass it along with the name of any image file you want to open, with the added benefit of retaining terminal control.

9. Trade less for more

More commands are useful when quickly perusing the contents of a text file. Once the file is loaded on the page, you can use / to search for the file. The problem is not that once you find the desired line, it is impossible to navigate up and check what appears to be higher than the line. Fewer commands are not a disadvantage, it allows you to scroll up and down the text. The fewer commands you call in the same way, the more you get:

$less sometextfile.txt

10. Clean up your command line history

History command can easily become one of the most powerful tools at your disposal. But there is one time-saving command that is particularly worth mentioning: the $HISTIGNORE shell variable.

Over time, your history list will become very long. Use the $HISTIGNORE variable to drain records that you think are irrelevant:

$export $HISTIGNORE= "&: cd:exit:ls"

This causes all copied commands, cd, leave, and ls commands to be omitted from the history command.

Speed is the key to managing the command line, and these ten tips should help you start your command line management. If you want to share other tips with us, please contact me through the website.

This is the answer to the ten command line questions about saving time for Linux administrators. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel for more related knowledge.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report