Tianwaifeixian-level Linux Shell Command

Source: Internet
Author: User
Tags key string free ssh
Tianwaifeixian-level Linux Shell Command

Time:
Source: wowubuntu.com

This article is compiled from commandlinefu.com (
Catonmat) series of articles

Top Ten one-liners fromcommandlinefu explained. As a website recommended by users to use the most useful shell commands, it records tens of thousands of various shell commands, many of which are quite practical and interesting. In this article, we need to count the top vote commands in detail, we hope that the readers can benefit from the detailed materials.

Introduction

As the most attractive and indispensable component in UNIX operating systems, shell has not been eliminated after dozens of trials, but has become more mature and stable, probably because it is a very solid adhesive, it can match a large number of powerful components at will, and can always complete user tasks very quickly.

Some of the commands in this article may seem like a "artifact", so we have to admire shell, but I will explore some details to explain them. I hope you can smile at some interesting points.

1. Run the previous command with 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. At this time, new users will: press the up arrow, press the left arrow, stare at the cursor back to the start, enter sudo, and press ENTER; Master users will be more, press Ctrl-P, press ctrl-a, enter sudo, and press Enter.

Here we will introduce this to the level of off-the-sky flying fairy, right, just sudo !!.

Of course, the effects of these solutions are the same, but they are different. Well, I don't want to explain them.

Two exclamation points are actually a feature of bash, called event designators ).!! 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.

 

For more information about event references, see the definitive guide to bashcommand line history.

2. share files in the current folder as HTTP

$ python -m SimpleHTTPServer

This command starts the simplehttpserver module of Python. Considering that python is installed by default in most Linux distributions, this command may be the simplest method for cross-platform file transfer.

After the command is executed, the HTTP service is enabled on port 8000 of the Local Machine. Open http: // ip: 8000 in the browser of another machine that can access the local machine to open a directory list. Then, click to download the directory.

3. 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. The curve method is to save a temporary file first, exit and then sudo CP back. However, in Vim, this process can be directly completed, and the command is like this.

Refer to the vim document (input: Help: W) and the command W will be mentioned! {Cmd}, let Vim execute an external command {cmd}, and then pass the content of the current buffer from stdin.

Tee is a small tool that saves stdin to files.

% Is the name of a read-only register in Vim, which stores the file path of the currently edited file.

Therefore, executing this command is equivalent to modifying the current edited file from outside vim.

4. switch back to the previous directory

$ cd -

Many people should know this. A horizontal pole represents the path of the previous directory.

In fact, CD-is short for CD $ oldpwd. Bash's fixed variable $ oldpwd always saves the path of the previous directory.

Relatively, $ PWD always saves the path of the current directory. These variables are quite useful when writing shell scripts.

5. replace a phrase in the previous command

$ ^foo^bar^

It is also an event nator. You can replace Foo in the previous command with 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.

The original style of this command should be as follows:

!!:s/foo/bar/

This document introduces you at the beginning !!, You should be familiar with the subsequent sections. The replacement operations of vim and sed are similar.

For more information about event references, see the definitive guide to bash command line history.

6. quickly back up a file

$ 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.

Braces are an arrangement in Bash. You can try this:

$ echo {a,b,c}{a,b,c}{a,b,c}

The output three sets are arranged in full order:

aaa aab aac aba abb abc aca acb acc
baa bab bac bba bbb bbc bca bcb bcc
caa cab cac cba cbb cbc cca ccb ccc

For more information about the set operations in shell, see "Set Operations in the Unix shell"

7. Password-free SSH login to the host

$ ssh-copy-id remote-machine

This command writes the current user's public key string to the remote host ~ /. Ssh/authorized_keys, so that the next time you log on using SSH, the remote host will directly perform identity verification based on the key string and no longer ask for the password. The premise is that your current user has generated a public key, which is not available by default. Run ssh-keygen first!

If this command is manually completed, it is as follows:

your-machine$ scp ~/.ssh/identity.pub remote-machine:
your-machine$ ssh remote-machine
remote-machine$ cat identity.pub >> ~/.ssh/authorized_keys

If you want to delete the key on the remote host, open authorized_keys, search for your username, and delete the line.

8. Capture Linux Desktop videos

$ ffmpeg -f x11grab -s wxga -r 25 -i :0.0 -sameq /tmp/out.mpg

We can see on some video websites how cool videos are on others' 3D desktops. This is usually the case. FFMPEG can directly decode the X11 graphics and convert them to the corresponding output format.

The common usage of FFmpeg is to output a file based on a bunch of parameters. The output file is usually put to the end. The following describes the parameters:

-F x11grab indicates the input type. Because the buffer zone of X11 is not a normal format for video file detection, you must specify FFMPEG to know how to obtain the input.

-S wxga sets the size of the captured area. Wxga is a standard statement of 1366*768. It can also be replaced by a-s 800*600 statement.

-R 25 sets the frame rate, that is, the number of captured images per second.

-I: 0.0: Set the input source. The local X value is 0.0 by default.

-Sameq maintains the same image quality as the input stream for later processing.

For other FFMPEG usage, refer to the following two articles:

  1. How to extract audio tracks from YouTube videos
  2. Converting YouTube flash videos to a better format
    FFmpeg

Postscript

Shell is a programming language, which may be a bit embarrassing. Although many people use shell every day, it has never been listed in the Tianyi programming language rankings, because many users do not realize that it is a language and can only be used as a tool to complete tasks well. It is a matter of course, just like the menus and buttons of GUI programs.

Understanding shell usually allows tasks to be completed in a matter of seconds, which distinguishes shell from C, Perl, and python. No one denies that the latter is more competent for more tasks, but they do it at different levels. Shell depends on a large number of system components for bonding calls, while the latter relies on various libraries. Each of them is good at different application fields. The analogy is that shell is concrete, it can easily bond some building components and become a solid high-rise building; but it is also adhesive, sticking glass windows, sticking books, sticking leather shoes, concrete is absolutely not suitable, shell is not good at some meticulous operations. For example, it does not support floating point operations, let alone graphic operations. But this does not prevent shell from helping us complete many heavy tasks.

Most beginners will find it boring to learn how shell works, and the so-called classic textbooks cannot be separated from advanced bash-scripting and bash guide forbeginners. however, some tips similar to this article will never be included in this article. This situation can be improved if there are many UNIX users in foreign countries. Even new users, sometimes looking at others' operations, can "steal the teacher, in fact, I want to improve this situation by compiling this series of articles.

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.