Several common shell commands

Source: Internet
Author: User
Tags mx record

How to extract the line Content Code specified by the file:

If you only want to view the first five lines of the file, you can use the head command,
For example: Head-5/etc/passwd

If you want to view the last 10 lines of the file, you can use the tail command,
For example: tail-10/etc/passwd

Do you know how to view the middle part of the file? You can use the SED command
For example, sed-n'5, 10p'/etc/passwd allows you to view only rows 5th to 10th of the file.

How to find the file code containing a specific string:

For example, find the file containing the "the string you want find..." string in the current directory:
$ Find.-Type F-exec grep "the string you want find..." {};-print

How to list the directory tree code:

The following short shell program can list the directory tree, making full use of SED's powerful pattern matching capabilities.
The directory tree format is as follows:
.
'---- Shellp
'---- Updates
'---- Wu-ftpd-2.4
| '---- Doc
| '---- Examples
| '---- SRC
| '---- Config
| '---- Makefiles
| '---- Support
| '---- Makefiles
| '---- Man
| '---- Util
The script is as follows:
#! /Bin/sh
# Dtree: Usage: dtree [any directory]
Dir =$ {1 :-.}
(CD $ dir; PWD)
Find $ Dir-type D-print | sort-f | sed-e "s, ^ $1,"-e "/^ $/D"-e "s, [^/] */([^/] *) $, '---- 1, "-e" s, [^/] */, |, G"

Code for how to retrieve the column content specified by the file:

We often encounter some specific fields in the file that need to retrieve the field, for example,/etc/password is separated. You can use the cut command. For example, if you want to save the system account name to a specific file, you can:
Cut-D:-F 1/etc/passwd>/tmp/users
-D is used to define the delimiter. The default value is the tab key.-F indicates the field to be obtained.
Of course, you can also use cut to get the *** characters per line in the file, for example:
Cut-c3-5/etc/passwd
It is the third to fifth character of each line in the output/etc/passwd file.
The-C and-F parameters can be described as follows:
N nth character or field
N-from the first character or field to the end of the file
N-M from N to M characters or fields
-M from the first to the nth character or field

Implement batch encryption code in VIM:

There is still no space in the password. No matter what it is, it is good to encrypt it. Use it first.

========================================================== ================================
#! /Bin/bash
# Encrypt file with vim

If (test $ #-lt 2) then
Echo usage: decrypt password filename
Else
Vim-e-s-c ": set key = $1"-C': wq' $2
Echo "$2 encrypted ."
Fi
========================================================== ================================
[Weeder @ smth weeder] $ for file in *. txt; do encrypt test $ file; done
Test2.txt encrypted.
Test4.txt encrypted.
Test9.txt encrypted.
Kick.txt encrypted.
Echo "$2 encrypted ."
Fi
[Weeder @ smth weeder] $ for file in *. txt; do encrypt test $ file; done
Test2.txt encrypted.
Test4.txt encrypted.
Test9.txt encrypted.
Kick.txt encrypted.
Too_old.txt encrypted.
Too_old_again.txt encrypted.
Bg5.txt encrypted.
[Weeder @ smth weeder] $

$ @ Refers to the meaning code of specific shell variables:

In the actual writing of shell scripts, some special variables are very useful:
$ # Number of parameters passed to the script

$ * Display all parameters passed to the script with a single string. Different from location variables, this option can have more than 9 Parameters

$ ID of the current process running the script

$! ID of the last process running in the background

$ @ Is the same as $ #, but it is enclosed by quotation marks and each parameter is returned in quotation marks.

$-Display the current options used by shell, which have the same functions as the SET command.

$? Displays the exit status of the last command. 0 indicates no error, and any other value indicates an error.

How to direct the execution result of a program to the screen and file code at the same time:

Program_name | tee logfile
In this way, the display during program execution is recorded in the logfile and displayed to the standard output (screen ).

How to use sendmail to send code to all users in the system:

First, create an alias in the aliases file:
Alluser: include:/etc/mail/allusers
Execute newaliases to make it take effect, and then list all users in/etc/mail/allusers. You can use the following command:
Awk-F: '$3> 100 {print $1}'/etc/passwd>/etc/mail/allusers

How to find the related library file code of a command:

When creating your own release, you often need to determine which database files are supported by a command to ensure that the specified command can run reliably in an independent system.
Run the LDD command in Linux and run the following command on the console:
LDD/bin/ls
You can get the list of related library files of the/bin/LS command.

How to use the host command to obtain more information code:

Host
It can be used to query domain names, but it can get more information. Host-T MX
Linux.com can query the MX record of linux.com and the Host Name of the mail. Host-l
Linux.com returns all domain names registered under linux.com. Host-A linux.com displays all domain names of the host.

How to stop multiple processes on the terminal:

The following is a script:
Echo "current user of the system"
Echo "---------------"
Who | awk '{print $2 }'
Echo "---------------"
Echo "Enter the terminal number of the terminal to be killed :"
Read $ tty
Kill-9 $ {k} = 'ps-T $ tty | grep [0-9] | awk '{print $1 }''

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.