[Shell advanced] The most bull B Linux shell Command (ii)

Source: Internet
Author: User
Tags diff stdin ssh port

1. Use your favorite editor to tap the command

command <ctrl-x ctrl-e>

After the command has been knocked out, press <ctrl-x ctrl-e>, will open an editor you specify (such as Vim, through the environment variable $editor specified), which is the command you just entered, and then love how to edit it, especially those parameters of the complex program, For example, Mencoder/ffmpeg, a command frequently 3, 4 lines, to modify the parameters, this method is the most appropriate, save the exit after the automatic execution of the program.

This is actually the function of the ReadLine Library, which by default, bash uses the command line operation of Emacs mode, <ctrl-x ctrl-e> is a binding that calls this feature. If you are accustomed to using VI mode, pressing <ESC v> can do the same.

If you like other editors, you can put a command such as export Editor=nano inside the ~/.BASHRC.

Another way to modify a command is to use the FC command (Fix command) to open the previous command in the editor. Our first series mentioned a ^foo^bar^ command that can be implemented with FC: Fc-s Foo=bar.

2. Empty or create a file

> File.txt

> in the shell is the standard output redirect, that is, the command line output (the previous command) to a file, but there is no "front command", the output is empty, so it is overwritten (or created) into an empty file.

Some scripts are written as: >file.txt, because: is the empty function that bash has by default.

Simply creating a file can also be used as a timestamp for modifying the file, but if the file does not exist, it is created automatically $touch File.txt,touch.

3. Create a port forwarding channel with SSH

ssh-n-l2001:remotehost:80 [email protected]

This command opens the 2001 port on this machine, and the request to the native 2001 port passes through Somemachine as a springboard and goes to port 80 of remotehost.

The implementation effect is similar to the term reverse proxy, which is actually port forwarding, noting that the above description involves 3 hosts, but of course somemachine can become localhost.

This command is abstract, but sometimes useful, for example, because of the well-known reasons that the domestic IP port 80 is not available, or the company's firewall only to the outside to open the SSH port, need to access the internal server A Web application, and the need to access some of the services that restrict the source IP, You can use this method.

To give a concrete example, run:

ssh-f-n-l 0.0.0.0:443:twitter.com:443 shell.cjb.netssh-f-n-l 0.0.0.0:80:twitter.com:80 shell.cjb.net

Then add 127.0.0.1 twitter.com inside the/etc/hosts, OK, the rest you know.

Of course, usually do this function of the reverse proxy, you should use squid, nginx and so on, ssh even if it is a lightweight try it!

4. Resetting the terminal

Reset

If you have tried to accidentally cat a binary file, it is likely that the entire terminal will be silly, may not be wrapped, can not be echoed, a large heap of garbled, such as, at this time type reset return, regardless of whether the command is displayed, you can return to normal.

In fact, the reset command only outputs some special characters, and we look at the implementation of the simplest reset program in BusyBox:

printf ("\033c\033 (k\033[j\033[0m\033[?25h");

The output of these characters is of special interest to the shell:

\033c: "Esc C"-Send reset command; \033 (k: "ESC (k"-character mapping for overloaded terminal; \033[J: "Esc [J"-empty terminal content; \033[0m: "Esc [0 M"-Initialize character display properties; 33[?25h: "ESC [? H "-Make the cursor visible;

The character display property is often used to set the color of the printed characters, etc., refer to this blog post .

5. Execute an order at midnight

echo cmd | at midnight

That is, at this component, usually in the same way as Cron, but at is mainly used for timed one-time tasks, and cron timed periodic tasks.

At the parameters of a more humane, like English grammar, you can tomorrow, next week and the like, Detailed View manual man at.

6. Remote Transfer Microphone Voice

dd if=/dev/dsp | ssh [email protected] DD OF=/DEV/DSP

Yes, the function of a megaphone is realized.

/DEV/DSP is the Linux sound card file mapping (Digital Signal proccessor), from which reading data is the recording, the inside to write data is playing, quite simple!

DD is a commonly used data copy program, and if not specified, the stdin/stdout is used to transmit it directly.

If you don't have a remote host, you can try this:

DD IF=/DEV/DSP OF=/DEV/DSP

Directly play back the microphone sound, just a little delay.

However, if there are other programs using the sound card, this method does not work, because the general sound card does not allow the simultaneous processing of multiple audio streams, you can borrow ALSA component tools, Arecord and Aplay:

arecord | ssh [email protected] Aplay

Local replay is:

Arecord | Aplay

If you want to scare people:

cat/dev/urandom | ssh [email protected] Aplay

7. Mapping a Memory directory

mount-t tmpfs-o size=1024m tmpfs/mnt/ram

This command opens a 1G of memory for use as a directory. But rest assured, if there is no file, it will not take up memory, how much to occupy.

However, in general there is no need to manually mount, because most distributions will be in the fstab reserved A memory directory, mounted in the/DEV/SHM, directly use;

The most common use is to use memory space to put the Firefox configuration, can make the slow FF much faster, see ShellEx's blog: use Tmpfs to let Firefox in memory , and later improvements: Use Tmpfs to make Firefox fly in memory II, where the script mentioned comes from speeding up Firefox with Tmpfs and automatic rsync.

That tattered linuxqq can also use this method to reduce the problem caused by a lot of disk IO.

8. Compare remote files with local files with diff

ssh [email protected] cat/path/to/remotefile | diff/path/to/localfile-

The usual usage of diff is to read two files from a parameter, while the inside of the command is read from stdin.

The use of SSH makes web development a lot less cumbersome, and for example SSHFS, can be freed from the edit-upload-edit-upload manual loop.

9. View the process that occupies the port in the system

NETSTAT-TULNP

Netstat is one of the most commonly used tools for viewing Linux network systems, and this parameter can be memorized:

-T: displaying TCP link information

-u: Show UDP link Information

-L: Port showing listening status

-N: Direct IP display, no name conversion

-P: Display the corresponding process PID and name (to root)

You can use the Lsof tool if you want to see more detailed usage of information about sockets.

[Shell advanced] (GO) The most bull B Linux shell Command (ii)

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.