Common Linux Shell Advanced section summary _linux Shell

Source: Internet
Author: User

How to calculate the number of files and directories in the current directory

  # ls-l * |grep "^-" |wc-l----to count files
  
  # ls-l * |grep ' ^d ' |wc-l-----to Count dir


How do I just column subdirectories?

  

Or

Alias sub = "Ls-f | grep/$ "(Linux)


  

Or

 Ls-ll | grep "^d" (Solaris)


How do I get the specific row contents out of 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:

 
 

This allows you to view only the 5th line to the 10th line of the file.

How to find a file 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 a directory tree

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 | | '--makefiles
  
  | '--support | | '
  
  --makefiles |
  
  | '--man
  
  | '--util
  
  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 do I get the specific column contents out of 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 get a specific number of characters per line in the file by cut, for example:

  Cut-c3-5/etc/passwd

  &NBSP
    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 child parameters:
  
    n nth characters or fields
&nbs p; 
    N from first character or field to end of file
  
    n-m from nth to M characters or fields
   
    m from first to nth characters or fields
   
    Implementing bulk encryption in Vim
   
    Password can still not take a space, regardless of, the encryption is 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 of specific shell variables 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 make the results of a program both directed to the screen and to files

 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 deliver mail to all users of the system

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 a related library file 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 for more information

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 processes on 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.