The best Linux Shell Command series serialization

Source: Internet
Author: User
Tags key string free ssh

From: http://wowubuntu.com/linux_shell_1.html

The best Linux Shell Command series serialization (1)

9th

2010

# Author: Riku/
This article uses CC BY-NC-SA 2.5 protocol authorization, reprint please note this article link.

NOTE: With the consent of the author @ isspy, the site will repost the "Best B" from isspy from today
Linux Shell Command series serialization "article. I personally think that as a Linux User, mastering CLI commands is a basic capability. This serialization article provides a deeper level of content that can help you learn more useful content.

Original article: Linux Shell Command series serialization (I) Author: @ boypt

Editor's Note

This article is compiled from commandlinefu.com (it should be catonmat) series of articles top
Ten one-liners from commandlinefu 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.

Edit: @ boypt

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 "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 bash command 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 ttp: // 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}

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:

How to extract audio tracks from YouTube videos
Converting YouTube flash videos to a better format with 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 may find it boring to learn how shell works, and the so-called classic textbooks cannot be separated from advanced bash-scripting and bash guide for beginners. however, some tips similar to this article will never be included in this article. In this case, if there are many UNIX users in foreign countries, it will be very good to improve. Even a newbie, sometimes looking at other people's operations, can "steal" first hand, 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.