SAs the most attractive and indispensable component in UNIX operating systems, hell has not only not been eliminated after dozens of trials, but also become more mature and stable, probably because it is a very solid adhesive that can match a large number of powerful components at will, it can always complete user tasks very quickly.
1. Run the previous command on sudo.
$ sudo !!
We should all know sudo, not to explain. However, we usually find that we forget sudo only when an error is reported after the command is executed. In this case, press Ctrl-P, Ctrl-a, enter sudo, and press Enter.
Heavyweight command sudo !! The above problems can be solved in a very good way. Two exclamation points are actually a feature of bash, called the event quote .!! Actually! -1. You can also reference the previous command! -2 ,! -50. By default, Bash will ~ /. The bash_history file records the last 500 commands executed by the user. The history command can display these commands.
2. Save a Root User File in VIM opened by a common user
:w !sudo tee %
This topic is difficult to read. It is very common to forget sudo to directly use Vim to edit files in/etc, when Vim finds that the saved file cannot be saved, a prompt will be displayed.) After the file is edited, it will only find that the file has no permission when it is saved.
3. Switch to the previous directory.
$ cd -
4. Replace a phrase in the previous command
$ ^foo^bar^
When you need to re-run and debug a long command and test a parameter, using this command is more practical. However, most people will first choose to put forward the preceding command by pressing the up arrow, moving the cursor to modify a parameter is more intuitive, but the efficiency is not high enough to use the quote, and this method can be much simpler in the script.
5. Fast file backup
$ cp filename{,.bak}
This command copies the filename file to filename. Bak. You should have seen this usage in some complicated installation tutorials. The principle is that Bash expands braces. the filename {,. Bak} segment is expanded into filename. Bak and then passed to CP, so the BACKUP command is available.
To be continued...