📜 ⬆️ ⬇️

We optimize the work process in the console

Everyone is used to editing text in text editors, notebooks, web forms, etc. In the process of typing, we use the usual arrows, the End and Home buttons, the more experienced ones hold down the Ctrl key and follow the arrows along the words (which, by the way, does not always work). And when switching to the console, we focus on the same rules, even without knowing that bash offers very convenient tools and key combinations that greatly simplify the work and minimize the number of operations to accomplish the task. In addition, bash has convenient tools for working with history, lots of different substitutions and other interesting functions. The most frequently used by me and any experienced administrator I will describe in this article.

In order for everything described by me to work, you don’t need to install anything, you don’t need to write scripts, configs and other add-ons: all these tools work in exactly the same way in most modern bash interpreters working on any UNIX system.
My pedagogical experience suggests that practice is the best way to understand, love and get used to new functions, so I didn’t draw bare tables, but made a story, and I recommend opening the console and trying everything you haven’t used before reading this article. by hands.

Cursor Operations

Out of habit, the Home button is used to move to the beginning of the line, and the End button is used to move to the end. But how comfortable is it to reach for these buttons when typing a command? Bash suggests not to break away from the alphanumeric keyboard and use the combination "Ctrl + A" instead of "Home" and "Ctrl + E" instead of "End". Sometimes, typing a long command, you remember that it was necessary to perform one more before it: in this case, the best way would be to move to "Ctrl + A" at the beginning of the line, add the "#" symbol (thus commenting out the command, but leaving it in history ), and pressing Enter, after which you can execute the necessary command, find a commented out command in the history, remove the comment character and execute it.
To clear the text before or after the cursor, use the keyboard shortcuts: “Ctrl + K” to delete the text from the current cursor position to the end of the line, and “Ctrl + U” to the beginning of the line. Finally, by “Ctrl + C” you can delete the entire line at once.
You can delete the words immediately: “Ctrl + W” deletes the word to the left of the cursor, “Alt + D” to the right. But deleted words are also placed in the buffer, the last deleted word can be inserted starting from the current cursor position using the “Ctrl + Y” combination.
Often, in a hurry, you can type 2 characters in the wrong sequence: in this case, it is convenient to use the key combination “Ctrl + T”, which is designed to replace the character to the cursor and the character under the cursor.
')
Story

Usually, knowledge of the bash history subsystems is limited to up and down arrows. In the work it is convenient to use the following features:

Moving through the file system

The “cd” command, as well as the auto addition by the “TAB” button, will not surprise anyone. But often you have to run around the entire file system, with the need to go back later. Here, the bash-based mechanism based on the stack model comes in handy, which is supported by the pushd and popd: pushd commands go to the target directory and add the absolute path of the previous directory to the stack, and popd chooses the previous path from the stack and goes to it. Example:

[vorb @ localhost ~] $ pushd / var / cache / urpmi / rpms /
/ var / cache / urpmi / rpms ~
[vorb @ localhost rpms] $ pushd /etc/urpmi/mediacfg.d/Cooker-2010.0-i586/
/etc/urpmi/mediacfg.d/Cooker-2010.0-i586 / var / cache / urpmi / rpms ~
[vorb @ localhost Cooker-2010.0-i586] $ popd
/ var / cache / urpmi / rpms ~
[vorb @ localhost rpms] $ popd
~
[vorb @ localhost ~] $

In this example, I first went to the directory with the urpmi cache, then to the directory with the urpmi configs, then went back to the directory with the cache, and home. One of the most common tasks solved using this mechanism is to go to the directory with configs, then to the directory with the cache, with logs, or anything else, and be able to quickly go back.
By the way, the “cd” command also has its tricks: “cd” without parameters will change the directory to the home, “cd ~ user” to the home directory of the user user.

In principle, this is all I wanted to talk about in this article. Of course, this is far from everything, and if there is a desire to develop knowledge in this direction, I recommend the book by Jerry Pick, Tim O'Reilly and Mike Lukidis “UNIX: Tools”. It describes not only bash, but also C shell, and ksh, and how much I read it - I always find something new for myself.
Successes!

Source: https://habr.com/ru/post/116838/


All Articles