Several common shell commands

Source: Internet
Author: User

How to implement the line content code to remove the file * * *:

If you only want to see the first 5 lines of the file, you can use the Head command,
such as: Head-5/etc/passwd

If you want to view the following 10 lines of the file, you can use the tail command,
such as: TAIL-10/ETC/PASSWD

Do you know how to view the middle section of a file? You can use the SED command
such as: Sed-n ' 5,10p '/etc/passwd so you can view only the 5th line of the file to line 10th.


How to find file code with a specific string:

For example, find files in the current directory that contain "the string want find ..." string:
$find. -type f-exec grep "The string you want find ..." {} ; -print

How to List directory tree code:

The following short shell program can list the directory tree, taking advantage of SED's powerful pattern-matching capabilities.
The directory tree forms the following:
.
'----SHELLP
'----Updates
'----wu-ftpd-2.4
| '----doc
| | '----examples
| '----src
| | '----Config
| | '----makefiles
| '----support
| | '----makefiles
| | '----mans
| '----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 '

How to implement the column content code to remove the file * * *:

We often encounter certain fields that need to be taken out of a field, such as/etc/password to separate fields by ":". Can be achieved by the cut command. For example, if we want to save the system account name to a specific file, we can:
Cut-d:-F 1/etc/passwd >/tmp/users
-D is used to define the separator, the default is the TAB key, and-F indicates which field to get.
Of course, you can also use the cut to get a few characters per line in the file, such as:
Cut-c3-5/etc/passwd
is the third to fifth character of each line in the output/etc/passwd file.
The-C and-f parameters can be followed by the following sub parameters:
n Nth characters or fields
N-from the first character or field to the end of the file
N-m from Nth to M characters or fields
-M from first to nth characters or fields

Implement bulk encryption code in VIM:

Password or can not take a space, regardless of, can encrypt the good, first so use.

============================================================
#!/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 ' $
echo "$ 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 "$ 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]$

The meaning code for a particular shell variable such as $@:

In the actual writing of the shell script, there are some special variables that are useful:
$# the number of arguments passed to the script

$* displays all parameters passed to the script in a single string. Unlike positional variables, this option argument can exceed 9

$$ the current process ID number of the script run

$! Process ID number of the last process running in the background

$@ and $ #相同, but use quotes and return each argument in quotation marks

$-Displays the current options used by the shell, the same as the SET command

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


How to get the results of a program to be directed both to the screen and to the file code:

Program_name |tee LogFile
The display of the program during execution is recorded to logfile and displayed to the standard output (screen) at the same time.

How to use SendMail to give the system all user Messenger code:

First, create an alias in the aliases file:
AllUser:: Include:/etc/mail/allusers
and execute newaliases to make it effective, and then list all users in/etc/mail/allusers, and you can use the following command:
Awk-f: ' $ > {print $} '/etc/passwd >/etc/mail/allusers

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

When making your own distributions, you often need to determine which library files a command needs to support to ensure that the specified commands are run reliably within a separate system.
Implemented through the LDD command in a Linux environment, executed at the console:
Ldd/bin/ls
You can get a list of related library files for the/bin/ls command.

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

The Host can be used to query the domain name, but it can get more information. The Host-t MX linux.com can query for Linux.com MX records and the name of the host that handles mail. Host-l Linux.com will return all the domain names registered under Linux.com. Host-a linux.com Displays all domain name information for this host.

How to stop multiple process code for a terminal:

Here's the script:
echo "System Current User"
echo "---------------"
who | awk ' {print $} '
echo "---------------"
echo "Enter the terminal number to kill the terminal:"
Read $TTY
  Kill-9 ${k}= ' Ps-t $TTY | grep [0-9] | awk ' {print '} '

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.