Top B Linux shell command (a) __linux

Source: Internet
Author: User
Tags stdin python simplehttpserver

Note


This article compiles from the Commandlinefu.com series article top Ten one-liners from Commandlinefu explained. As a Web site recommended by the user as the most useful shell command, it records tens of thousands of shell commands, some of which are quite practical and interesting, and this article is going to count some of the highest-voting commands from which to draw and interpret them, hoping that the reader will benefit.

Introduction


As the most attractive and indispensable component of the UNIX system, the shell after decades of baptism has not only not been eliminated, but also become mature and stable, the reason, probably because it is a very solid adhesive, can be a large number of powerful components of any combination, always very good quickly to complete the user's task.


Some of this article's commands are likely to look "tricks", we have to admire the shell Daniel, but there are some details I will explore a little bit to explain, meet the interesting place I hope to be able to bo you smile.

1. Run the previous command in sudo

1 $ sudo!!

We should all know sudo, do not explain. But the usual situation is, after the execution of the command, the error was found to have forgotten sudo. At this time, novice users will: Press the arrow, press the left arrow, staring at the cursor back to the beginning, input sudo, carriage return; The master user on the egg set many, press ctrl-p, press ctrl-a, input sudo, enter.


Here is a description of this is the flying fairy level, right, directly sudo!!。


Of course, the effect of these solutions is exactly the same, but not the same, well, do not explain.


Two exclamation marks are actually a feature of bash, called event designators.!! In fact, the equivalent of!-1, quoting the previous command, of course, can also!-2,!-50. By default, bash records the most recent 500 commands that the user executes in the ~/.bash_history file, and the history command can display those commands.


More usage of the event reference character can be read in depth to the definitive Guide to Bash Command line History.

2. Files that share the current folder as HTTP

1 $ python-m Simplehttpserver

This command starts the Python simplehttpserver module, which is probably the simplest way to transfer files across platforms, considering that Python is installed by default in most Linux distributions.


After the command is executed, the HTTP service will be opened on native 8000 port, and a list of directories can be opened in the browser of other computers that have access to the ttp://ip:8000, click to download.

3. Save a root user file in a vim opened by a common user

1 : W! sudo tee%

This topic read tangled, in fact, is very common, often forget sudo directly with vim to edit the file in/etc, (but also not necessarily, Vim found that save the file can not save the time will be prompted), such as editor, save time only to find no permissions. The Curve method is to first save a temporary file, exit and then sudo CP back. But actually in VIM can complete this process directly, the command is so.


Consult Vim's documentation (input: Help:w) and refer to the command: w! {cmd}, let vim execute an external command {cmd}, and then pass the contents of the current buffer from stdin.


Tee is a gadget that saves stdin to a file.


and%, is vim among the name of a read-only register, always save the current edit file path.


So executing this command is the equivalent of modifying the current edited file from Vim outside to complete it.

4. Switch back to previous directory


$ CD-

It should be known to many people. Bar-Represents the path to the previous directory.


The CD is actually a shorthand for CD $OLDPWD, and bash's fixed variable $oldpwd always keeps the path to the previous directory.


In contrast, $PWD always keep the path to the current directory. These variables are quite useful when writing shell scripts.

5. Replace a phrase from the previous command

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.